简体   繁体   English

Rust:由于要求冲突,无法推断 autoref 的适当生命周期

[英]Rust: cannot infer an appropriate lifetime for autoref due to conflicting requirements

Minimal reproducible code:最小的可重现代码:

trait VecExt<T, R> {
    fn map(&self, f: impl Fn(&T) -> R) -> Vec<R>;
}

impl<T, R> VecExt<T, R> for Vec<T> {
    fn map(&self, f: impl Fn(&T) -> R) -> Vec<R> {
        self.iter().map(f).collect()
    }
}

struct A {
    a: Vec<Option<String>>
}

impl A {
    fn foo(&self) -> Vec<Option<&String>> {
        self.a.map(|o| o.as_ref())
    }
}

Getting error message:收到错误信息:

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements

When using self.a.iter().map(|o| o.as_ref()).collect() , it compiles.当使用self.a.iter().map(|o| o.as_ref()).collect()时,它会编译。

I've already read the explain document, but I still don't know why this error occurs and how to solve it.我已经阅读了说明文档,但我仍然不知道为什么会出现这个错误以及如何解决它。

When the map call in A.foo is replaced with the code from the map function, it compiles, which indicates that the issue is with the trait definition not constraining the lifetime of the argument passed to the closure.当 A.foo 中的map调用被map A.foo中的代码替换时,它会编译,这表明问题出在传递给闭包的特征定义的参数上。

In the case of fn map(&self, f: impl Fn(&T) -> R) -> Vec<R> , the lifetime of the reference passed as &T in impl Fn(&T) -> R must live for at least as long as the &self argument passed into it.fn map(&self, f: impl Fn(&T) -> R) -> Vec<R>的情况下,在impl Fn(&T) -> R中作为&T传递的引用的生命周期必须至少为只要&self参数传递给它。 The following changes should make it compile以下更改应使其编译

trait VecExt<'a, T, R> where T: 'a {
    fn map(&'a self, f: impl Fn(&'a T) -> R) -> Vec<R>;
}

impl<'a, T, R> VecExt<'a, T, R> for Vec<T> where T: 'a {
    fn map(&'a self, f: impl Fn(&'a T) -> R) -> Vec<R> {
        self.iter().map(f).collect()
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于需求冲突,无法为 autoref 推断适当的生命周期 - Cannot infer an appropriate lifetime for autoref due to conflicting requirements Rust:错误[E0495]:由于需求冲突,无法为 autoref 推断合适的生命周期 - Rust: error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements 尝试将具有生命周期的返回值设置为结构时,由于存在冲突的要求,无法推断出 autoref 的适当生命周期 - Cannot infer an appropriate lifetime for autoref due to conflicting requirements when tried to set a return value with lifetime to a struct `由于需求冲突,无法为 autoref 推断适当的生命周期`,但由于特征定义约束,无法更改任何内容 - `cannot infer an appropriate lifetime for autoref due to conflicting requirements` but can't change anything due to trait definition constraints 创建引用错误的递归列表“由于要求冲突,无法推断 autoref 的适当生命周期” - Creating a recursive list of references errors with "cannot infer an appropriate lifetime for autoref due to conflicting requirements" 错误:由于需求冲突,无法推断autoref的适当生命周期[E0495] - error: cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495] 由于需求冲突,无法推断出合适的生命周期 - Cannot infer an appropriate lifetime due to conflicting requirements 尝试实现迭代器:由于需求冲突而无法推断出适当的生存期 - Trying to implement an iterator: cannot infer an appropriate lifetime due to conflicting requirements 由于需求冲突,无法推断出自动强制的适当寿命 - cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements 由于需求冲突,无法为借用表达式推断出适当的生命周期 - cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM