Curriculum Ownership & borrowing Lifetime intuition
Lifetime intuition
Lifetimes are not a separate runtime feature; they describe how long references are valid. This theme avoids advanced annotation syntax at first and focuses on why Rust rejects dangling references. Planned exercises: 1. choose the value that lives long enough. 2. recognise a dangling reference. 3. choose owned return instead of borrowed return. 4. fill a scope change that extends an owner. 5. fill a returned String. 6. write a function that returns one of two borrowed inputs with an explicit lifetime. 7. write code that avoids returning a local reference. 8. translate a TS closure over local data. 9. fix a lifetime error by changing ownership rather than adding random annotations.
Exercises
9 ready
01
pick one
The program below fails: r outlives s. Pick the fix that
02
pick one
rustc rejects this function with `error[E0515]: cannot return
03
pick one
A function takes a &str and returns a NEW string with extra
04
fill blanks
r is declared in the outer scope, then assigned inside a block.
05
fill blanks
Fill the OWNED return type so the new uppercase string can
06
write a program
Write a function longer that takes TWO &str arguments and
07
write a program
Write a function make_counter_value that takes a &str
08
write a program
Translate the TS pattern that maps over a local array with a
09
write a program
This program reaches for lifetime annotations to fix a borrow