[英]Expected bound lifetime parameter, found concrete lifetime when trying to pass an Option<FnOnce>
In the code below, I'm trying to pass an Option<FnOnce(&mut Thing)>
to the higher order function invoke_me_maybe()
.在下面的代码中,我试图将
Option<FnOnce(&mut Thing)>
传递给更高阶的 function invoke_me_maybe()
。 The function being passed will be invoked if it is present, and not invoked otherwise.传递的 function 如果存在则将被调用,否则不会被调用。
The Option<FnOnce(&mut Thing)>
are constructed using as_some()
from additional trait methods on booleans, copied the boolinator crate. Option<FnOnce(&mut Thing)>
是使用as_some()
从布尔值的附加特征方法构造的,复制了boolinator crate。
struct Thing{}
fn invoke_me_maybe<F: FnOnce(&mut Thing)>(t: &mut Thing, opt_f: Option<F>) {
if let Some(f) = opt_f {
f(t);
}
}
trait BoolOption {
fn as_some<T>(self, some: T) -> Option<T>;
}
impl BoolOption for bool {
fn as_some<T>(self, some: T) -> Option<T> {
if self { Some(some) } else { None }
}
}
pub fn main() {
let mut thing = Thing{};
invoke_me_maybe(&mut thing, true.as_some(|t| {}));
}
The invoke_me_maybe()
function does not keep opt_f
beyond the end of the function, so we should not need to wrap the function in a Box or anything like that. invoke_me_maybe()
function 不会将opt_f
的末尾之外,因此我们不需要将 ZC1C425268E68385D1AB5074C17A94 中的任何东西包装起来。
The error produced is as follows:产生的错误如下:
error[E0631]: type mismatch in closure arguments
--> src/main.rs:21:33
|
3 | fn invoke_me_maybe<F: FnOnce(&mut Thing)>(t: &mut Thing, opt_f: Option<F>) {
| --------------- ------------------ required by this bound in `invoke_me_maybe`
...
21 | invoke_me_maybe(&mut thing, true.as_some(|t| {}));
| ^^^^^^^^^^^^^---^^^^
| | |
| | found signature of `fn(_) -> _`
| expected signature of `for<'r> fn(&'r mut Thing) -> _`
error[E0271]: type mismatch resolving `for<'r> <[closure@src/main.rs:21:46: 21:52] as std::ops::FnOnce<(&'r mut Thing,)>>::Output == ()`
--> src/main.rs:21:5
|
3 | fn invoke_me_maybe<F: FnOnce(&mut Thing)>(t: &mut Thing, opt_f: Option<F>) {
| --------------- ------------------ required by this bound in `invoke_me_maybe`
...
21 | invoke_me_maybe(&mut thing, true.as_some(|t| {}));
| ^^^^^^^^^^^^^^^ expected bound lifetime parameter, found concrete lifetime
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0271, E0631.
For more information about an error, try `rustc --explain E0271`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
I'm probably missing some explicit lifetime parameters or something like that, but I can't figure it out.我可能缺少一些明确的生命周期参数或类似的东西,但我无法弄清楚。 Doesn't
fn(_) -> _
already match with for<'r> fn(&'r mut Thing) -> _
? fn(_) -> _
不是已经与for<'r> fn(&'r mut Thing) -> _
匹配了吗?
Using closures as higher order functions is tricky.使用闭包作为高阶函数是很棘手的。 I've noticed that explicitly writing the type of the arguments usually helps.
我注意到明确编写 arguments 的类型通常会有所帮助。 In your case it does the trick :
在您的情况下,它可以解决问题:
invoke_me_maybe(&mut thing, true.as_some(|t: &mut Thing| {}));
The problem seems to be that invoke_me_maybe
takes a generic argument with a lot of possibilities, while |t| {}
问题似乎是
invoke_me_maybe
采用了具有很多可能性的通用参数,而|t| {}
|t| {}
may mean anything, and the compiler is not able to match both. |t| {}
可能意味着任何东西,编译器无法同时匹配两者。 Adding the type annotation helps in this case.在这种情况下,添加类型注释会有所帮助。
Personally I consider this a compiler bug, but I've been wrong before...我个人认为这是一个编译器错误,但我之前错了......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.