I'm trying to add an impl
of a trait, Printable
to an Option
use a generic type bound - but the compiler complains that a `+ 'static' is needed for T, viz:-
impl<'a, T> Printable for Option<&'a T>
where T: Printable
{
const MyId: u64 = T::MyId;
fn print(&self) {
if let Some(printable) = *self {
printable.print();
}
}
}
Adding where T: Printable + 'static
makes this work. A slightly more complete example is on the Rust Playground. What I don't understand is why. The associated constant, MyId
, is const; it lives forever. Adding a static bound makes this impl
next-to-useless, as I can't then pass, say Option<&'a T>
as a variable to a method; instead, I have to pass Option<&'static T>
. I strongly suspect I don't understand 'static'
lifetimes properly. I certainly can't get my head round the compiler's error[E0311]: the parameter type T may not live long enough
. How can a type not live long enough?
Many thanks if you try to explain this to me...