Prep: Ownership — 02r¶
Session: Thu Sep 3, 1h45 · Exercises: 02r_ownership · Prep time: ~25 min · Lecture: Week 2 · Ownership and Borrowing
What you will build¶
A physical page allocator modeled by hand from a Vec<usize> of free page numbers and move semantics. There is no & yet, so each piece takes the whole free list by value and hands it back in a tuple: build the list at "boot," hand a page out like kalloc, take one back like kfree, and return a sentinel page number that can never be real when the list runs dry. One small piece shows that a String given to a function must be handed back before the caller can use it. The given tests check that no page is handed out twice, that a returned page becomes free again, and that page numbers survive being passed around while the list does not.
Concepts you need¶
- One owner; drop at the closing brace — Week 2 · No
free(), and no collector · Rust for Systems: The rule - What a move is: three words copied, old name dead — Week 2 · A move copies the handle, not the buffer · Rust for Systems: Moving
- Moves across a call; returning a value to give it back — Week 2 · Moving through a call, and handing it back · Rust for Systems:
E0382, use after move Copytypes: page numbers copy, the list moves — Week 2 ·Copytypes stay put, What survives a move · Rust for Systems:Copytypes do not move- Drop is where
free()went — Week 2 · Nofree(), and no collector · Rust for Systems: Drop Vecbasics, tuples, shadowing — Week 2 · Moving through a call, and handing it back, Problem 1: What survives a move · Rust for Systems: Three ways to hold a run of values- Reading E0382 and the missing-
muterror — Week 2 · A move copies the handle, not the buffer, The errors you will actually hit · Rust for Systems: Common compiler errors
Read before class¶
| What | Time |
|---|---|
Week 2 · This week, then Thursday · 02r Ownership and moves, all four sections |
7 min |
| Week 2 · For the exam: What survives a move. Not needed today; on Midterm 1 | 1 min |
| Week 2 · The errors you will actually hit, the E0382 and E0596 rows | 1 min |
| Rust for Systems: Ownership and moves | 6 min |
| Rust for Systems: Three ways to hold a run of values | 3 min |
Mental model¶
A function that takes an owned value, changes it, and gives it back, plus a number riding along:
fn stamp(mut msg: String, n: u32) -> (String, u32) {
msg.push('!'); // legal only because the parameter says `mut`
(msg, n + 1) // hand the String back
}
let msg = String::from("boot");
let n = 7;
let (msg, total) = stamp(msg, n); // moved in, moved back out into a new `msg` (shadowing)
assert_eq!(n, 7); // `u32` is Copy: the original survives
assert_eq!((msg.as_str(), total), ("boot!", 8));
While stamp runs it is the only owner of that String. The kernel's allocator needs that: while "hand out a page" runs it owns the whole free list, so the page it returns cannot still be on it. Ownership is that invariant, checked by the compiler.
Check yourself¶
- After
let b = a;wherea: String, what happens at run time, and what at compile time?Answer
Run time: three machine words (pointer, length, capacity) are copied; the heap buffer is untouched. Compile time:ais marked dead; naming it again is E0382, a use-after-free caught early. - Which of these are
Copy:usize,bool,String,Vec<usize>,(usize, usize)?Answer
usize,bool, and the tuple; notStringorVec<usize>. The line is not size but resources: a type with cleanup (Drop) cannot beCopy, or cleanup would run once per copy. - A function takes a
Vec<usize>by value and calls.pushon it; the caller needs the list afterward. Without&, what must it do?Answer
Writemutbefore the parameter name (or.pushis E0596), and return theVec, usually in a tuple. The caller rebinds withlet (list, x) = f(list);, shadowing the deadlist.
What "done" looks like¶
oslings run is green, then oslings submit before you leave. Not green? Submit anyway — the tests that pass earn their share — then finish it at a make-up session (office hours, on the class network) for three quarters, or anywhere else for half, and submit again.
If you finish early¶
Work Week 2 · Problem 1: What survives a move and Problem 2: Copy or not on paper (Midterm 1 material). Rustlings: 06_move_semantics, then 05_vecs and the tuple exercises in 04_primitive_types. 100 Exercises To Learn Rust: chapter 3, the Ownership, Stack, Heap, and Destructors sections; chapter 4, Copy and Drop. Then start Friday's prep page, Prep: Borrowing and Lifetimes, on borrowing: the fix for every "return it so the caller keeps it" line.