MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / push

Method push

code/chapter03/lvec.rs:50–67  ·  view source on GitHub ↗
(&mut self, elem: T)

Source from the content-addressed store, hash-verified

48 }
49
50 fn push(&mut self, elem: T) {
51 let node = Node::new(elem);
52 if self.is_empty() {
53 self.head = Some(Box::new(node));
54 } else {
55 let mut curr = self.head.as_mut().unwrap();
56
57 // 找到链表最后一个节点
58 for _i in 0..self.size-1 {
59 curr = curr.next.as_mut().unwrap();
60 }
61
62 // 在最后一个节点后插入新数据
63 curr.next = Some(Box::new(node));
64 }
65
66 self.size += 1;
67 }
68
69 // 栈末尾加入数据
70 fn append(&mut self, other: &mut Self) {

Callers 2

appendMethod · 0.45
mainFunction · 0.45

Calls 1

is_emptyMethod · 0.45

Tested by

no test coverage detected