I'm trying to get my head around the way that generics and lifetimes interact. Consider:
use std::ops::Add;
struct Gloop<'a, T: Add> {
wumpus: &'a Wumpus<T>,
}
trait Wumpus<T: Add> {
fn fleeb(&self, x: &T) -> bool;
}
struct Mimsy {
jubjub: f64,
}
impl<T: Add> Wumpus<T> for Mimsy {
fn fleeb(&self, x: &T) -> bool {
return (x + x) > 0;
}
}
fn main() {
let a = Mimsy { jubjub: 1. };
let b = Gloop::<i32> { wumpus: &a };
println!("{}", b.fleeb(1));
}
Which yields:
error[E0309]: the parameter type `T` may not live long enough
--> src/main.rs:4:5
|
3 | struct Gloop<'a, T: Add> {
| -- help: consider adding an explicit lifetime bound `T: 'a`...
4 | wumpus: &'a Wumpus<T>,
| ^^^^^^^^^^^^^^^^^^^^^
|
note: ...so that the reference type `&'a Wumpus<T> + 'a` does not outlive the data it points at
--> src/main.rs:4:5
|
4 | wumpus: &'a Wumpus<T>,
| ^^^^^^^^^^^^^^^^^^^^^
Which object in my program is the one which may not live long enough? Nowhere in either Gloop
or Mimsy
(or any Wumpus<T>
) is a T
ever stored.