too-many-lists/bad-stack/basic-operations #722
Replies: 6 comments 8 replies
-
clone 用起来简单难 需要修改 |
Beta Was this translation helpful? Give feedback.
-
为 |
Beta Was this translation helpful? Give feedback.
-
pop 也可以这样实现, pub fn pop(&mut self) -> Option<i32> {
let result;
match &mut self.head {
Link::Empty => {
result = None;
}
Link::More(node) => {
result = Some(node.elem);
self.head = std::mem::replace(&mut node.next, Link::Empty);
}
}
} 也可以这样实现, 多种方式加强理解学习。 pub fn pop(&mut self) -> Option<i32> {
let result;
match self.head {
Link::Empty => {
result = None;
}
Link::More(ref mut node) => {
result = Some(node.elem);
self.head = std::mem::replace(&mut node.next, Link::Empty);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
“试图将借用的值 self 中的 head 字段的所有权转移给 next ,在 Rust 中这是不被允许的” 对于这段话我不太理解,在 StackOverflow 找到了如下解释:
因为编译器无法检查 |
Beta Was this translation helpful? Give feedback.
-
Link->Option type Link = Option<Box> |
Beta Was this translation helpful? Give feedback.
-
too-many-lists/bad-stack/basic-operations
https://course.rs/too-many-lists/bad-stack/basic-operations.html
Beta Was this translation helpful? Give feedback.
All reactions