too-many-lists/persistent-stack/layout #892
Replies: 10 comments 12 replies
-
求 iterMut 实现,已投降 - -! |
Beta Was this translation helpful? Give feedback.
-
感觉先去看一遍《通过例子学Rust》再来看这个例子比较好,现在看的有点懵 |
Beta Was this translation helpful? Give feedback.
-
pub fn prepend(&self, elem: T) -> List { |
Beta Was this translation helpful? Give feedback.
-
这里做个小分析,纯粹是个人填下之前没有完全理解的坑; self.head.as_ref() => Option<&Rc<Node> map desugaring => and_then desugaring => 嵌套的Option应该是由于map返回的类型为LInk /* Option<Rc<Node>>*/; 本质上是符合官方对两个方法的定义: |
Beta Was this translation helpful? Give feedback.
-
未经充分测试,简单测试是通过的 IntoIter
IterMut
|
Beta Was this translation helpful? Give feedback.
-
我有个疑问:为什么tail方法移除元素时,要用clone()方法?这不是会让强引用数+1吗?是和drop方法有关吗?
当list中的所有节点tail之后,list2中的7-6-5-3-2-1的强引用依次是:1-1-1-2-2-2 |
Beta Was this translation helpful? Give feedback.
-
这个开头提出的数据布局我居然都没操作出来 |
Beta Was this translation helpful? Give feedback.
-
对列表进行迭代时,将 pub struct Iter<'a, T> {
next: Option<&'a Rc<Node<T>>>,
} |
Beta Was this translation helpful? Give feedback.
-
看了一下原文,
|
Beta Was this translation helpful? Give feedback.
-
为什么新链表一定是不可变的,然后push 和 pop 没有意义呢?可以按照push pop的方式写啊,像下面这样: pub fn prepend(&mut self, elem: T) {
let new_node = Rc::new(Node {
elem: elem,
next: self.head.take(),
});
self.head = Some(new_node);
} let mut list = List::new();
list.prepend(1);
list.prepend(2);
list.prepend(3); 测试可以通过的 |
Beta Was this translation helpful? Give feedback.
-
too-many-lists/persistent-stack/layout
https://course.rs/too-many-lists/persistent-stack/layout.html
Beta Was this translation helpful? Give feedback.
All reactions