简体   繁体   English

Rust:错误[E0495]:由于需求冲突,无法为 autoref 推断合适的生命周期

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

This is the minimal code:这是最小的代码:

struct Node<T> {
    item: T,
    next: Link<T>,
}

type Link<T> = Option<Box<Node<T>>>;

pub struct IterMut<'a, T>(&'a mut Link<T>);

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;
    fn next(&mut self) -> Option<Self::Item> {
        self.0.as_mut().map(|boxed_node| {
            self.0 = &mut boxed_node.next;
            &mut boxed_node.item
        })
    }
}

As far as I understand, there should be no problem.据我了解,应该没有问题。 I have done a lot of searching, but no way.我已经做了很多搜索,但没有办法。

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src/lib.rs:13:16
   |
13 |         self.0.as_mut().map(|boxed_node| {
   |                ^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 12:5...
  --> src/lib.rs:12:5
   |
12 | /     fn next(&mut self) -> Option<Self::Item> {
13 | |         self.0.as_mut().map(|boxed_node| {
14 | |             self.0 = &mut boxed_node.next;
15 | |             &mut boxed_node.item
16 | |         })
17 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/lib.rs:13:9
   |
13 |         self.0.as_mut().map(|boxed_node| {
   |         ^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 10:6...
  --> src/lib.rs:10:6
   |
10 | impl<'a, T> Iterator for IterMut<'a, T> {
   |      ^^
note: ...so that reference does not outlive borrowed content
  --> src/lib.rs:14:22
   |
14 |             self.0 = &mut boxed_node.next;
   |                      ^^^^^^^^^^^^^^^^^^^^

We can rewrite your code as:我们可以将您的代码重写为:

struct Node<T> {
    item: T,
    next: Link<T>,
}

type Link<T> = Option<Box<Node<T>>>;

pub struct IterMut<'a, T>(&'a mut Link<T>);

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;
    fn next(&mut self) -> Option<Self::Item> {
        if let Some(boxed_node) = self.0 {
            self.0 = &mut boxed_node.next;
            Some(&mut boxed_node.item)
        }
        else {
            None
        }
    }
}

You can see that boxed_node life end at the end of the function so you can't return a reference link to it.您可以在函数的末尾看到boxed_node生命周期结束,因此您无法返回指向它的引用链接。

The solution is to take a reference of the box and not a reference to the option:解决方案是引用框而不是引用选项:

struct Node<T> {
    item: T,
    next: Link<T>,
}

type Link<T> = Option<Box<Node<T>>>;

pub struct IterMut<'a, T>(Option<&'a mut Box<Node<T>>>);

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;
    fn next(&mut self) -> Option<Self::Item> {
        if let Some(boxed_node) = self.0.take() {
            self.0 = boxed_node.next.as_mut();
            Some(&mut boxed_node.item)
        }
        else {
            None
        }
    }
}

You can also remove the Box :您还可以删除Box

struct Node<T> {
    item: T,
    next: Link<T>,
}

type Link<T> = Option<Box<Node<T>>>;

pub struct IterMut<'a, T>(Option<&'a mut Node<T>>);

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;
    fn next(&mut self) -> Option<Self::Item> {
        if let Some(boxed_node) = self.0.take() {
            self.0 = boxed_node.next.as_mut().map(AsMut::as_mut);
            Some(&mut boxed_node.item)
        }
        else {
            None
        }
    }
}

暂无
暂无

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

相关问题 错误:由于需求冲突,无法推断autoref的适当生命周期[E0495] - error: cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495] Rust:错误[E0495]:由于闭包中的冲突要求,无法为自动拒绝推断适当的生命周期 - Rust: error[E0495]: cannot infer an appropriate lifetime for autorefdue to conflicting requirements in closure Rust:由于要求冲突,无法推断 autoref 的适当生命周期 - Rust: cannot infer an appropriate lifetime for autoref due to conflicting requirements 由于需求冲突,无法为 autoref 推断适当的生命周期 - Cannot infer an appropriate lifetime for autoref due to conflicting requirements 我如何解决导致错误的闭包[E0495]:无法推断适当的生存期 - How do I work around closures causing error[E0495]: cannot infer an appropriate lifetime 尝试将具有生命周期的返回值设置为结构时,由于存在冲突的要求,无法推断出 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 无法为结构内具有相同生命周期的多个引用推断生命参数的适当生命周期 [E0495] - cannot infer an appropriate lifetime for lifetime parameter with multiple references with the same lifetime inside a struct [E0495] 创建引用错误的递归列表“由于要求冲突,无法推断 autoref 的适当生命周期” - Creating a recursive list of references errors with "cannot infer an appropriate lifetime for autoref due to conflicting requirements" 由于需求冲突,无法推断出合适的生命周期 - Cannot infer an appropriate lifetime due to conflicting requirements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM