rust-training
rust-training copied to clipboard
Lifetimes: Reword the slide about `takes_and_returns`
Slide code: https://github.com/ferrous-systems/rust-training/blob/main/training-slides/src/lifetimes.md?plain=1#L95
This function CAN return local data
fn takes_and_returns(s: &str) -> &str {
"abc"
}
s/local/static. A string literal isn't local - it's a string slice with static lifetime.
I think the note to add here is that whilst you can return a slice with static lifetime, the borrow checker can only assume the lifetime of the output is tied to the lifetime of the input, so you must drop the returned slice before dropping the input slice, even if you did return "abc".
We fixed this.