简体   繁体   English

预期绑定生命周期参数,在尝试传递选项时找到具体生命周期<fnonce></fnonce>

[英]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.

相关问题 预期的约束寿命参数,找到具体的寿命 - Expected bound lifetime parameter, found concrete lifetime 如何解决:预期的混凝土寿命,但找到了约束寿命参数 - How to fix: expected concrete lifetime, but found bound lifetime parameter StreamExt.scan() 方法上的“预期绑定生命周期参数,找到具体生命周期” - “expected bound lifetime parameter, found concrete lifetime” on StreamExt .scan() method 预期的约束寿命参数,找到的混凝土寿命[E0271] - Expected bound lifetime parameter, found concrete lifetime [E0271] 预期的混凝土寿命,在结构中存储fn时找到约束寿命参数 - Expected concrete lifetime, found bound lifetime parameter when storing a fn in a struct 锈蚀寿命误差预期具体寿命但发现约束寿命 - Rust lifetime error expected concrete lifetime but found bound lifetime 函数引用:预期的约束生命周期参数,找到具体的生命周期[E0271] - Function references: expected bound lifetime parameter , found concrete lifetime [E0271] 从闭包填充集合时,键入不匹配“绑定生命周期参数”与“具体生命周期” - Type mismatch “bound lifetime parameter” vs “concrete lifetime” when filling a collection from a closure 当使用参数的生命周期作为特征参数时,“预期的关联类型,找到`u32`” - “Expected associated type, found `u32`” when using the lifetime of a parameter as trait parameter in where bound 尝试调用泛型函数时出现“预期的绑定生命周期参数”错误 - “expected bound lifetime parameter” error when attempting to call a generic function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM