簡體   English   中英

Rust:錯誤[E0495]:由於需求沖突,無法為 autoref 推斷合適的生命周期

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

這是最小的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM