Re:Rust Lifetimes
by groovy2shoes on Saturday October 31, 2015 @08:08PM
(#10484812)
Thank you.
> It's possible to have an example where 'c must be shorter lived, and cannot outlive the struct.
I'm interested in such an example. Could you provide one?
Re:Rust Lifetimes
by Manishearth on Saturday October 31, 2015 @08:26PM
(#10484875)
Actually, I just looked into it, that's no longer possible. I was talking about contravariant lifetime positions, which happen with functions, but now it seems like function pointers are invariant in those positions, not contravariant.
The idea was to have something like
struct Foo<'a> {f: fn(&'a u8)}
should only be able to accept pointers that live shorter than the struct. But contravariance seems to have been removed.
Re:Rust Lifetimes
by groovy2shoes on Saturday October 31, 2015 @09:02PM
(#10484968)
Makes sense. I can see how having contravariant function arguments could produce some surprising behavior.
When you say `Foo<'c>`, all you're saying is that each `Foo` instance has an associated lifetime `'c`, similar to a type parameter. When you add the `x: &'c u8` field, then you're saying that "this lifetime is the lifetime of the inner u8".
> Am I correct in thinking that, in reality, 'c must live as long or outlive the struct
For the specific example, yes. It's possible to have an example where 'c must be shorter lived, and cannot outlive the struct.
If you had an &mut instead of & there, then 'c must live exactly as long as the struct.