Skip to content

Add lifetime elision information to the ownership guide. #19736

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

Merged
merged 1 commit into from
Jan 6, 2015

Conversation

steveklabnik
Copy link
Member

Fixes #19662.

fn print<'a>(s: &'a str); // expanded

fn debug(lvl: uint, s: &str); // elided
fn debug<'a>(lvl: uint, s: &'a str); // expanded
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a comment here is warranted similar to this. If you don't recognize this, it is puzzling why int doesn't get a lifetime too.

// `lvl` doesn't need a lifetime because it's not a reference (`&`). Only references need lifetimes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not only references: struct Foo<'a> { ... } would be fn foo<'a>(x: Foo<'a>).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

## Lifetime Elision

Earlier, we mentioned 'lifetime elision,' a feature of Rust which allows you to
not write lifetime annotations in certain circumstances. All references must
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be "all references have a lifetime" or something else that emphasises that it is an inherent property of references.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-worded

@steveklabnik
Copy link
Member Author

r? @huonw

fn foo<'a>() -> &'a str
```

This one has both:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it has one, that is used in both input and output position.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both meaning and input and output lifetime, even though they are the same one. Is it worth an extra few words?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed this

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jan 5, 2015
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jan 6, 2015
@bors bors merged commit a56e7ae into rust-lang:master Jan 6, 2015
@steveklabnik steveklabnik deleted the gh19662 branch October 25, 2017 18:27
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

Successfully merging this pull request may close these issues.

Fully document all forms of lifetime elision
4 participants