Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 itShakyStudy it
1.1Say what a stack frame is, when it appears, and when it is thrown away
1.2Say what a { } block inside a function does to a variable declared inside it
1.3Say why the stack is fast, and what the heap can do that the stack can't
1.4Say what a String on the stack is made of, and where its letters are
1.5Say what a reference has that a raw pointer doesn't, and why only one of them needs unsafe
1.6Say what unsafe is a promise about
1.7List the seven memory bugs, and say which ones safe Rust turns into a compile error
1.8Say the rule for which types copy and which types move, and where your own struct falls
1.9Say what happens to the heap when a String moves, and when it is cloned
1.10List the four steps a full Vec takes on a push
1.11Say what Box::new(7) puts where

Part 2: Strings, borrowing, tests

Could you do this?Got itShakyStudy it
2.1Tell String, &String, and &str apart, and say where each one's arrow ends
2.2Say why s.len() and s.chars().count() can differ, and why s[0] is refused
2.3Say what a byte-range slice does when it ends in the middle of a character
2.4State the two borrowing rules, and say which C bug each one prevents
2.5Say where a borrow ends, and why the owner's own name counts as a writer
2.6Say what each of &T, &mut T, and T as a parameter costs the caller
2.7Say what .iter_mut() hands you that .iter() doesn't, and what you do with it in the loop
2.8Read fn f<'a>(x: &'a str) -> &'a str out loud and say what the 'a promises
2.9Say what 'static means and which values have it
2.10Write a test: the attribute, the function, an assert with a message
2.11Tell 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 itShakyStudy 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 itShakyStudy it
4.1A function that takes a &[i32] and gives back the largest number as Some(n), or None for an empty slice
4.2A function that takes a &str and gives back a String that is three copies of the &str in a row
4.3A function that takes a &mut Vec<i32> and doubles every number in place
4.4Two 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 itShakyStudy it
5.1Why a push can break a reference into the vector
5.2What malloc hands you, and why you write before you read
5.3For a memory mistake, whether safe Rust gives a compile error, a panic, or makes it impossible to write
5.4Why .clone() costs more than &
5.5Why a function that returns a reference to its own local variable can't compile