Curriculum Ownership & borrowing Shared borrowing
Shared borrowing
Borrowing is how Rust lets code inspect data without taking it. &T references are shared, read-only views whose owner remains somewhere else. Planned exercises: 1. choose &String over String for a read helper. 2. recognise call-site borrowing with &name. 3. choose dereference-free field or method access. 4. fill an & parameter. 5. fill an & argument. 6. write a read-only helper. 7. write a caller that keeps using the owner afterward. 8. translate a TS read helper. 9. fix a function that accidentally takes ownership.
Exercises
9 ready
01
pick one
A function only needs to READ a String — log it, measure it,
02
pick one
Given fn log(s: &String) and let name = String::from("paul");,
03
pick one
Inside fn count(s: &String) -> usize, you want to return the
04
fill blanks
Fill the one character that turns a String parameter into a
05
fill blanks
Fill the one character that turns the call-site argument into a
06
type one line
Write the function signature line. length takes a shared
07
type one line
The function log takes &String. Write the call-site line that
08
write a program
Translate the TypeScript read-helper into a complete Rust
09
write a program
The program below fails to compile: