4 ms·
There's a few other configurations of lifetimes, e.g. (not meant to be exhaustive) - only allowing storing references that are valid forever struct Sheep {
by dbaupp 11y ago
There's a few other configurations of lifetimes, e.g. (not meant to be exhaustive)
- only allowing storing references that are valid forever
struct Sheep { age: &'static i32 }
- storing multiple references, with the same lifetime
struct Sheep<'a> { age: &'a i32, name: &'a str }
or different ones
struct Sheep<'a, 'n> { age: &'a i32, name: &'n str }
(This is usually most important for mutable references (&mut), rather than shared ones (&).)
- lifetimes that have a non-trivial relationship to other lifetimes/types, e.g. via trait bounds:
struct Sheep<'a, N: SomeTrait<'a>> { age: &'a i32, name: N }
---
There could be some elision rule that allows omitting the explicit annotation in some cases, but I think it would be weird: allowing complete elision for the equivalent thing with types (e.g. writing `struct Foo<T> { x: T }` as just `struct Foo { x: T }`) would not be great at all IMO. These types are generic over lifetimes in essentially the same way as types that are generic over types (i.e. they can be instantiated with a lifetime (resp. type) of a downstream user's choosing), and being generic over a lifetime is part of its signature/behaviour.