Closed
Description
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
Metadata
Metadata
Assignees
Labels
No labels