I'm struggling to understand lifetimes. I understand (I think) that specifying a lifetime with input parameters (i.e. foo<'a>(x: &'a String) -> &'a String {}) means that the returned value inherits the lifetime of the input parameter and will therefore live as long as that does. However, the code below compiles just fine without any input parameters, so I don't understand how the compiler knows I wanted the reference to Dragon::Green to live long enough to leave scope. Is the ownership getting moved to a higher scope, or is something else happening?
fn main() {
let d;
{
let dtile = testtile();
d = dtile;
}
println!("{:?}", d);
}
#[derive(PartialOrd, Ord, PartialEq, Eq, Debug)]
enum Dragon {
White,
Green,
Red
}
fn testtile<'a>() -> &'a Dragon {
&Dragon::Green
}