As someone who is not professionally a programmer or officially educated as such, I am about two to three months into learning Rust, and most concepts I have been able to slowly reason my way through. Something I have been stuck on for a while now, though, is the practical difference between using a reference pointer &T versus using the basic smart pointer Box.
I'm probably missing something, but from what I can tell, both are pointers, and so satisfy the need for certain things to of known size at compile time. Both have very similar if not the same dereferencing behavior from the dereferencing operator. Both can, themselves, be referenced. I believe both are stored on the stack.
Even one of the examples I've seen of using Box for recursive types, something along the lines of:
struct Recursive<T> { content: T, child: Box<Recursive<T>>, } seems to be identically achievable using the & pointer:
struct Recursive<'a, T> { content: T, child: &'a Recursive<'a, T>, } I would like to understand where the functional differences are, but I'm having trouble getting it through my skull with just the documentation.
3 posts - 3 participants