I'm reading the Rust Programming Language book. In Chapter 15.4 it says:
The borrow checker wouldn’t let us compile
let a = Cons(10, &Nil);
for example, because the temporaryNil
value would be dropped beforea
could take a reference to it.
I have tried to create the Cons
list with references like below, and it works for me. Why does this work when the book says it shouldn't?
#[derive(Debug)]
enum List<'a> {
Cons(i32, &'a List<'a>),
Nil,
}
use crate::List::{Cons, Nil};
fn main() {
println!("Hello, world!");
let a = Cons(10, &Nil);
println!("{:?}", a);
}