2

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 temporary Nil value would be dropped before a 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);
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578

0 Answers0