Activity 27: Confidence check, and passing sheets
Name:
This is not a quiz. You don't answer anything. Just read each line, mark how you feel about it, take a picture for Gradescope, and keep the paper for studying.
Mark one box per row:
- Got it. I could do this right now, on paper, with no help
- Shaky. I would get there, but slowly, or I would second-guess it.
- Study it. I need to go back to this before Friday.
Everything here is from Lectures 17 through 26.
Part 1: Memory, pointers, ownership
| Could you do this? | Got it | Shaky | Study it | |
|---|---|---|---|---|
| 1.1 | Say what a stack frame is, when it appears, and when it is thrown away | |||
| 1.2 | Say what a { } block inside a function does to a variable declared inside it | |||
| 1.3 | Say why the stack is fast, and what the heap can do that the stack can't | |||
| 1.4 | Say what a String on the stack is made of, and where its letters are | |||
| 1.5 | Say what a reference has that a raw pointer doesn't, and why only one of them needs unsafe | |||
| 1.6 | Say what unsafe is a promise about | |||
| 1.7 | List the seven memory bugs, and say which ones safe Rust turns into a compile error | |||
| 1.8 | Say the rule for which types copy and which types move, and where your own struct falls | |||
| 1.9 | Say what happens to the heap when a String moves, and when it is cloned | |||
| 1.10 | List the four steps a full Vec takes on a push | |||
| 1.11 | Say what Box::new(7) puts where |
Part 2: Strings, borrowing, tests
| Could you do this? | Got it | Shaky | Study it | |
|---|---|---|---|---|
| 2.1 | Tell String, &String, and &str apart, and say where each one's arrow ends | |||
| 2.2 | Say why s.len() and s.chars().count() can differ, and why s[0] is refused | |||
| 2.3 | Say what a byte-range slice does when it ends in the middle of a character | |||
| 2.4 | State the two borrowing rules, and say which C bug each one prevents | |||
| 2.5 | Say where a borrow ends, and why the owner's own name counts as a writer | |||
| 2.6 | Say what each of &T, &mut T, and T as a parameter costs the caller | |||
| 2.7 | Say what .iter_mut() hands you that .iter() doesn't, and what you do with it in the loop | |||
| 2.8 | Read fn f<'a>(x: &'a str) -> &'a str out loud and say what the 'a promises | |||
| 2.9 | Say what 'static means and which values have it | |||
| 2.10 | Write a test: the attribute, the function, an assert with a message | |||
| 2.11 | Tell a happy-path test from an edge-case test, and name three edges worth testing |
Part 3: Read the code
Don't answer these fully. Decide whether you could.
3.1 What does this print?
#![allow(unused)] fn main() { let a = String::from("go"); let mut b = a.clone(); b.push_str("!"); println!("{} {}", a, b); }
3.2 Does this compile? If so, what does it print?
#![allow(unused)] fn main() { let s = String::from("hi"); let a = &s; let b = &s; println!("{} {} {}", a, b, s.len()); }
3.3 Could you draw the stack and the heap after these three lines, with a type on every variable?
#![allow(unused)] fn main() { let v = vec![5, 6]; let b = Box::new(9); let r = &v; }
3.4 Does this compile? If not, which rule is broken, and on which line?
#![allow(unused)] fn main() { let mut x = 5; let a = &mut x; let b = &mut x; *a += 1; *b += 1; }
| Got it | Shaky | Study it | |
|---|---|---|---|
| 3.1 | |||
| 3.2 | |||
| 3.3 | |||
| 3.4 |
Part 4: Write it from nothing
On paper without notes. Could you do it?
| Could you write this? | Got it | Shaky | Study it | |
|---|---|---|---|---|
| 4.1 | A function that takes a &[i32] and gives back the largest number as Some(n), or None for an empty slice | |||
| 4.2 | A function that takes a &str and gives back a String that is three copies of the &str in a row | |||
| 4.3 | A function that takes a &mut Vec<i32> and doubles every number in place | |||
| 4.4 | Two tests for 4.1, one ordinary and one edge, each with a message |
Part 5: Say it out loud
| Could you explain this to someone? | Got it | Shaky | Study it | |
|---|---|---|---|---|
| 5.1 | Why a push can break a reference into the vector | |||
| 5.2 | What malloc hands you, and why you write before you read | |||
| 5.3 | For a memory mistake, whether safe Rust gives a compile error, a panic, or makes it impossible to write | |||
| 5.4 | Why .clone() costs more than & | |||
| 5.5 | Why a function that returns a reference to its own local variable can't compile |