I have a set of rules that surround references and I would like to make sure that I'm correct about all of them:
- Standard references are the same as raw pointers, they just offer static analysis with their lifetimes and therefore comply with the following depending on the system's architecture
assert_eq!(std::mem::size_of::<&T>(), std::mem::size_of::usize());
//where T: Sized
- A reference to a trait, also known as dynamic dispatch is twice as large due to it having both the data and the vtable
- A reference to a slice is also an exception, in that it has the data pointer, and the length of which it points, but the slice itself isn't usually a struct in and of itself, instead more like a smart buffer pointer (so an
&[T]
is kind of like a&&[T; size]
butsize
is unknown so it's[T]: !Sized
) and actually doesn't represent the data itself, instead representing the pointer and length of said data -
str
and&str
either follow the same rules as[T]
and&[T]
or they're magic because of unicode
I'm sorry for the verbosity of these questions, but I just wanted to be reassured that I was correct in how the memory internals for rust and its references work. Also, are there any more exceptions in rust pointers/references which don't follow rule #1?