简体   繁体   English

由于递归结构中的冲突要求,无法推断出适当的生命周期

[英]Cannot infer an appropriate lifetime due to conflicting requirements in a recursive struct

When I try to compile this code: 当我尝试编译此代码时:

pub struct Context<'a> {
    pub outer: Option<&'a mut Context<'a>>,
}

impl<'a> Context<'a> {
    pub fn inside(outer: &'a mut Context) -> Context<'a> {
        Context { outer: Some(outer) }
    }
}

I'm getting this error: 我收到这个错误:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
 --> src/main.rs:7:9
  |
7 |         Context { outer: Some(outer) }
  |         ^^^^^^^
  |
note: first, the lifetime cannot outlive the lifetime 'a as defined on the impl at 5:1...
 --> src/main.rs:5:1
  |
5 | / impl<'a> Context<'a> {
6 | |     pub fn inside(outer: &'a mut Context) -> Context<'a> {
7 | |         Context { outer: Some(outer) }
8 | |     }
9 | | }
  | |_^
note: ...so that expression is assignable (expected Context<'a>, found Context<'_>)
 --> src/main.rs:7:9
  |
7 |         Context { outer: Some(outer) }
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the method body at 6:5...
 --> src/main.rs:6:5
  |
6 | /     pub fn inside(outer: &'a mut Context) -> Context<'a> {
7 | |         Context { outer: Some(outer) }
8 | |     }
  | |_____^
note: ...so that expression is assignable (expected &mut Context<'_>, found &mut Context<'_>)
 --> src/main.rs:7:31
  |
7 |         Context { outer: Some(outer) }
  |                               ^^^^^

Why this is happening? 为什么会这样?

This is because you haven't met the obligations that you require. 这是因为您没有履行所需的义务。

Due to lifetime elision, your code is equivalent to: 由于终身省略,您的代码相当于:

pub fn inside<'b>(outer: &'a mut Context<'b>) -> Context<'a>

Change your code to 将您的代码更改为

pub fn inside(outer: &'a mut Context<'a>) -> Context<'a>

And it will compile. 它会编译。

暂无
暂无

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

相关问题 由于需求冲突,无法推断出合适的生命周期 - Cannot infer an appropriate lifetime due to conflicting requirements 将结构转换为具有生存期的特征得到“由于需求冲突,无法为生存期参数&#39;a&#39;推断适当的生存期” - casting struct to trait with lifetime got “cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements” 由于对由具有 Serde Deserialize 的枚举组成的结构的要求相互冲突,因此无法为生命周期参数“de”推断合适的生命周期 - cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements for struct made of enums with Serde Deserialize 尝试将具有生命周期的返回值设置为结构时,由于存在冲突的要求,无法推断出 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 的适当生命周期” - Creating a recursive list of references errors with "cannot infer an appropriate lifetime for autoref due to conflicting requirements" 递归关闭作为函数参数“由于需求冲突而无法推断出适当的寿命” - Recursive closure as function parameter “cannot infer an appropriate lifetime due to conflicting requirements” 由于需求冲突,结构中的访问引用无法推断借用表达式的适当生存期 - access reference in struct causes cannot infer an appropriate lifetime for borrow expression due to conflicting requirements 匿名函数 - 由于需求冲突,无法推断出合适的生命周期 - Anonymous function - 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM