This article is about "the Reference Counted Smart Pointer".
We could change the definition of
Cons
to hold references instead, but then we would have to specify lifetime parameters. By specifying lifetime parameters, we would be specifying that every element in the list will live at least as long as the entire list. The borrow checker wouldn’t let us compilelet a = Cons(10, &Nil);
for example, because the temporaryNil
value would be dropped beforea
could take a reference to it.
What should the code look like?
I wrote a code like this:
enum List<'a> {
Cons(i32, &'a List<'a>),
Nil,
}
use crate::List::{Cons, Nil};
fn main() {
let a = Cons(10, &Cons(4, &Nil));
let mut c = &a;
loop {
match c {
Cons(x, l) => {
println!("item {}", x);
c = l;
},
Nil => {
println!("done");
break;
}
}
}
}
I can't understand the last sentence of the paragraph. It seems that the compiler doesn't report any error.