我是 Rust 的新手,在阅读 Rust "Too many lists" series 的第 6.1 章时,我不明白关于自引用的段落:
“我们刚刚犯了一个主要的 Rust 罪:我们在自己内部存储了一个对自己的引用。不知何故,我们设法说服 Rust 这在我们的 push 和 pop 实现中完全有意义(我很震惊我们这样做是合理的。我相信原因是 Rust 还不能通过 push 和 pop 来判断引用是对我们自己的——或者更确切地说,Rust 根本没有这个概念。对自己的引用失败只是一种紧急行为.”
作者说了一些关于在我们自己内部存储对我们自己的引用,但没有在代码中指出发生这种情况的地方。这很混乱。 sb能再解释一下吗?
完整代码如下:
pub struct List<'a, T> {
head: Link<T>,
tail: Option<&'a mut Node<T>>, // NEW!
}
type Link<T> = Option<Box<Node<T>>>;
struct Node<T> {
elem: T,
next: Link<T>,
}
impl<'a, T> List<'a, T> {
pub fn new() -> Self {
List {
head: None,
tail: None,
}
}
pub fn push(&'a mut self, elem: T) {
let new_tail = Box::new(Node {
elem: elem,
// When you push onto the tail, your next is always None
next: None,
});
// Put the box in the right place, and then grab a reference to its Node
let new_tail = match self.tail.take() {
Some(old_tail) => {
// If the old tail existed, update it to point to the new tail
old_tail.next = Some(new_tail);
old_tail.next.as_mut().map(|node| &mut **node)
}
None => {
// Otherwise, update the head to point to it
self.head = Some(new_tail);
self.head.as_mut().map(|node| &mut **node)
}
};
// self.tail = new_tail;
}
pub fn pop(&'a mut self) -> Option<T> {
// Grab the list's current head
self.head.take().map(|head| {
let head = *head;
self.head = head.next;
// If we're out of `head`, make sure to set the tail to `None`.
if self.head.is_none() {
self.tail = None;
}
head.elem
})
}
}