语法问题 function returns function not clear
Syntax issue function returns function not clear
我对此感到困惑。
如果以下有效:
fn func_exit() -> bool {
println!("hi");
true
}
fn locate_func() -> fn() -> bool {
func_exit
}
为什么下面这些语法会抛出错误?
fn locate_func1<F: Fn() -> bool>() -> F {
func_exit
}
fn locate_func2<F>() -> F where F:Fn() -> bool {
func_exit
}
我是 Rust 新手,我不清楚以下错误消息:
error[E0308]: mismatched types
--> src/main.rs:44:9
|
43 | fn locate_func1<F: Fn() -> bool>() -> F {
| - this type parameter - expected `F` because of return type
44 | func_exit
| ^^^^^^^^^ expected type parameter `F`, found fn item
|
= note: expected type parameter `F`
found fn item `fn() -> bool {func_exit}`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
error[E0308]: mismatched types
--> src/main.rs:47:9
|
46 | fn locate_func2<F>() -> F where F:Fn() -> bool {
| - - expected `F` because of return type
| |
| this type parameter
47 | func_exit
| ^^^^^^^^^ expected type parameter `F`, found fn item
|
= note: expected type parameter `F`
found fn item `fn() -> bool {func_exit}`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
感谢一些帮助。
谢谢
fn locate_func() -> fn() -> bool {
func_exit
}
locate_func
return 指向函数的指针 returning bool(即 fn() -> bool
)。
fn locate_func1<F: Fn() -> bool>() -> F {
func_exit
}
locate_func1
表示 caller 可以指定任何 F
满足边界 Fn() -> bool
,它会 return F
。但是显然func_exit
不一定是调用者指定的类型
locate_func2
有同样的问题,只是用 where
-Notation.
您可能想要的可能是以下内容:
fn locate_func3() -> impl Fn() -> bool {
func_exit
}
它说:locate_func3
returns 实现绑定 Fn() -> bool
的东西(并且 func_exit
这样做,所以它可以在那里 returned ).
错误很奇怪,但是......你不能像这样 return 一个有界值,因为有界类型是由调用者决定的,调用者没有意义决定 return 类型是什么 除非 return 类型是输入类型的结果(例如,恒等函数就是一个简单的例子)。这里不是这种情况。
如果你想 return 一些通用的东西,你需要某种 特征对象 (例如,将函数框起来,然后 return 一个框)或 impl
(它仍然具有具体的 return 值,但将其隐藏在 API 级别)。
后者效率更高但只允许 returning 单个具体类型,前者效率较低但允许你 return 例如不同的闭包。
我对此感到困惑。 如果以下有效:
fn func_exit() -> bool {
println!("hi");
true
}
fn locate_func() -> fn() -> bool {
func_exit
}
为什么下面这些语法会抛出错误?
fn locate_func1<F: Fn() -> bool>() -> F {
func_exit
}
fn locate_func2<F>() -> F where F:Fn() -> bool {
func_exit
}
我是 Rust 新手,我不清楚以下错误消息:
error[E0308]: mismatched types
--> src/main.rs:44:9
|
43 | fn locate_func1<F: Fn() -> bool>() -> F {
| - this type parameter - expected `F` because of return type
44 | func_exit
| ^^^^^^^^^ expected type parameter `F`, found fn item
|
= note: expected type parameter `F`
found fn item `fn() -> bool {func_exit}`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
error[E0308]: mismatched types
--> src/main.rs:47:9
|
46 | fn locate_func2<F>() -> F where F:Fn() -> bool {
| - - expected `F` because of return type
| |
| this type parameter
47 | func_exit
| ^^^^^^^^^ expected type parameter `F`, found fn item
|
= note: expected type parameter `F`
found fn item `fn() -> bool {func_exit}`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
感谢一些帮助。 谢谢
fn locate_func() -> fn() -> bool {
func_exit
}
locate_func
return 指向函数的指针 returning bool(即 fn() -> bool
)。
fn locate_func1<F: Fn() -> bool>() -> F {
func_exit
}
locate_func1
表示 caller 可以指定任何 F
满足边界 Fn() -> bool
,它会 return F
。但是显然func_exit
不一定是调用者指定的类型
locate_func2
有同样的问题,只是用 where
-Notation.
您可能想要的可能是以下内容:
fn locate_func3() -> impl Fn() -> bool {
func_exit
}
它说:locate_func3
returns 实现绑定 Fn() -> bool
的东西(并且 func_exit
这样做,所以它可以在那里 returned ).
错误很奇怪,但是......你不能像这样 return 一个有界值,因为有界类型是由调用者决定的,调用者没有意义决定 return 类型是什么 除非 return 类型是输入类型的结果(例如,恒等函数就是一个简单的例子)。这里不是这种情况。
如果你想 return 一些通用的东西,你需要某种 特征对象 (例如,将函数框起来,然后 return 一个框)或 impl
(它仍然具有具体的 return 值,但将其隐藏在 API 级别)。
后者效率更高但只允许 returning 单个具体类型,前者效率较低但允许你 return 例如不同的闭包。