Hello dear helpful Rustaceans,
I am currently battling my way through the official book and there is one thing I apparently do not understand yet.
In the book, it is explained why cons-lists aren't possible by just declaring an enum with a tuple containing the initial enum as one of its variants (and Nil as the other). The solution presented is using the Box-Type.
Later in the book, it is stated that using normal references with & is not possible for cons-lists, because "we would have to specify lifetime parameters". It is further stated that
let a = Cons(10, &Nil);
would not be possible, because "the temporary Nil
value would be dropped before a
could take a reference to it.".
I fully understand the reasoning, but after I tried to use simple & anyway, the code mysteriously worked. Here is what I mean:
#[derive(Debug)] enum List<'a> { Cons(i32, &'a List<'a>), Nil } fn main() { let liste = List::Cons(10, &List::Nil); println!("{:?}", liste); }
How is this possible? I am quite tired at the moment, so I am fully prepared for it to be an insanely stupid thinking bug from myself.
Thank you in advance,
Arnanas