Reading the Rust documentation about Box it seems to work just like a normal pointer.
One example they give is this:
enum List {
Cons(i32, Box<List>),
Nil,
}
use crate::List::{Cons, Nil};
fn main() {
let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil))))));
}
But one could also write this:
#[derive(Debug)]
enum List<'a> {
Cons(i32, &'a List<'a>),
Nil,
}
use crate::List::{Cons, Nil};
fn main() {
let list = Cons(1, &Cons(2, &Cons(3, &Nil)));
println!("{:?}", list);
}
What are the compelling reasons to use Box?
Is it just for syntactic sugar?
Are there situations that we could/should only use Box instead of a pointer?
If Box works like a pointer should I prefer to use a Box instead of a pointer?
Why don't I need lifetimes when using a Box?