rust-training icon indicating copy to clipboard operation
rust-training copied to clipboard

Lifetimes: Reword the slide about `takes_and_returns`

Open amanjeev opened this issue 2 years ago • 2 comments

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"
}

amanjeev avatar Aug 02 '23 12:08 amanjeev

s/local/static. A string literal isn't local - it's a string slice with static lifetime.

jonathanpallant avatar Aug 03 '23 08:08 jonathanpallant

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".

jonathanpallant avatar Aug 21 '23 11:08 jonathanpallant

We fixed this.

jonathanpallant avatar Dec 09 '24 17:12 jonathanpallant