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

标签 rust lifetime

这是最少的代码:

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
        })
    }
}

据我了解,应该没有问题。我做了很多搜索,但没有办法。

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;
   |                      ^^^^^^^^^^^^^^^^^^^^

最佳答案

我们可以将您的代码重写为:

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
        }
    }
}

您可以看到 boxed_node 生命在函数结束时结束,因此您无法返回指向它的引用链接。

解决方案是引用框而不是选项的引用:

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
        }
    }
}

您还可以删除 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
        }
    }
}

关于Rust:错误 [E0495]:由于需求冲突,无法推断出 autoref 的适当生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57036513/

相关文章:

reference - 为什么 Rust 需要明确的生命周期?

rust - 如何读取bevy中的鼠标运动?

iterator - 如何使用生命周期嵌套可变访问?

rust - 想办法解决 "...does not live long enough"

rust - Vec::iter() 转换为借用 Option

rust - 如何处理多个嵌套的工作空间根?

rust - 特征对象,并将 n 个字节读入向量

rust - 类型别名上缺少生命周期说明符 [E0106]

reference - 如何使用 `AsRef` 参数?

string - 从 `&' 中获取比当前函数更长生命周期的 str` `String`