]> git.proxmox.com Git - rustc.git/blame - src/doc/nomicon/src/vec-push-pop.md
New upstream version 1.31.0~beta.4+dfsg1
[rustc.git] / src / doc / nomicon / src / vec-push-pop.md
CommitLineData
8bb4bdeb 1# Push and Pop
c1a9b12d
SL
2
3Alright. We can initialize. We can allocate. Let's actually implement some
4functionality! Let's start with `push`. All it needs to do is check if we're
5full to grow, unconditionally write to the next index, and then increment our
6length.
7
8To do the write we have to be careful not to evaluate the memory we want to write
9to. At worst, it's truly uninitialized memory from the allocator. At best it's the
10bits of some old value we popped off. Either way, we can't just index to the memory
11and dereference it, because that will evaluate the memory as a valid instance of
12T. Worse, `foo[idx] = x` will try to call `drop` on the old value of `foo[idx]`!
13
14The correct way to do this is with `ptr::write`, which just blindly overwrites the
15target address with the bits of the value we provide. No evaluation involved.
16
17For `push`, if the old len (before push was called) is 0, then we want to write
18to the 0th index. So we should offset by the old len.
19
20```rust,ignore
21pub fn push(&mut self, elem: T) {
22 if self.len == self.cap { self.grow(); }
23
24 unsafe {
25 ptr::write(self.ptr.offset(self.len as isize), elem);
26 }
27
28 // Can't fail, we'll OOM first.
29 self.len += 1;
30}
31```
32
33Easy! How about `pop`? Although this time the index we want to access is
34initialized, Rust won't just let us dereference the location of memory to move
35the value out, because that would leave the memory uninitialized! For this we
36need `ptr::read`, which just copies out the bits from the target address and
b039eaaf 37interprets it as a value of type T. This will leave the memory at this address
c1a9b12d
SL
38logically uninitialized, even though there is in fact a perfectly good instance
39of T there.
40
41For `pop`, if the old len is 1, we want to read out of the 0th index. So we
42should offset by the new len.
43
44```rust,ignore
45pub fn pop(&mut self) -> Option<T> {
46 if self.len == 0 {
47 None
48 } else {
49 self.len -= 1;
50 unsafe {
51 Some(ptr::read(self.ptr.offset(self.len as isize)))
52 }
53 }
54}
55```