Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifetimes: Reword the slide about takes_and_returns #86

Open
amanjeev opened this issue Aug 2, 2023 · 2 comments
Open

Lifetimes: Reword the slide about takes_and_returns #86

amanjeev opened this issue Aug 2, 2023 · 2 comments

Comments

@amanjeev
Copy link
Member

amanjeev commented Aug 2, 2023

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"
}
@jonathanpallant
Copy link
Member

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

@jonathanpallant
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants