Skip to content

TRPL: Requested: explanation of Lifetimes on impl #26375

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

Closed
mcast opened this issue Jun 17, 2015 · 0 comments
Closed

TRPL: Requested: explanation of Lifetimes on impl #26375

mcast opened this issue Jun 17, 2015 · 0 comments

Comments

@mcast
Copy link
Contributor

mcast commented Jun 17, 2015

I think this is an omission from TRPL, in the description of either Lifetimes or impl - requesting because I think any new user would benefit from it.

Having got a limited understanding of struct, lifetime and impl I wanted to write

pub struct Foo<'a> { ... }
impl Foo<'a> {
  pub fn new<'a>(...) -> Foo<'a> { ...}
}

thinking that the 'a on impl Foo defines it for... whatever is using it later.

I got the answer (thanks user22207 / made mcast/markdown-chapterise@9fcff539) and concluded that in

impl<'a> Foo<'b> {   // does not compile

I am defining 'a for the impl block, then using that definition at 'b (which must be 'a here, to compile) to describe the Foo.

The sort of explanatory example that would make sense to me would be like

// does not compile
struct Foo<'a, 'b> {
  bar: &'c str,
  baz: &'d str,
}

impl<'e, 'f> Foo<'g, 'h> {
  fn mkfoo<'i>(arg: &'j str) -> Foo<'k> { ... }
}

in which I avoid repeating a lifetime label so it's easier to describe

  • which define and which consume
  • which could be elided & when, and what that means
  • what the described lifetime constraints will mean when we set e.g. 'a == 'c == 'e and so on, to make it compile
  • relationships to lifetimes in enclosing blocks, if there could be any
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