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

Chapter 10.3 Question about lifetime constraints #2467

Closed
dp304 opened this issue Sep 30, 2020 · 1 comment
Closed

Chapter 10.3 Question about lifetime constraints #2467

dp304 opened this issue Sep 30, 2020 · 1 comment

Comments

@dp304
Copy link

dp304 commented Sep 30, 2020

The lifetime-annotated longest function has the following signature:
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str;
The book says:

The function signature now tells Rust that for some lifetime 'a, the function takes two parameters, both of which are string slices that live at least as long as lifetime 'a. The function signature also tells Rust that the string slice returned from the function will live at least as long as lifetime 'a.

In my view (although all these concepts are new to me, and I haven't looked at how the Rust implementation actually works), the policy to be enforced should rather be that the return value lives at most as long as lifetime 'a, or else it could become a dangling reference. Am I understanding something wrong?

@carols10cents
Copy link
Member

Yes, please take a look at the extensive discussion here: #1875

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