fn main() {
let a: [u8;3] = [1,2,3];
let b: &[u8] = &a;
}
The 1st statement creates a fixed-size array, but the 2nd variable has type &[u8], which looks like it will be allocated on heap. But the reference points to fixed-size array, which doesn't need heap allocation. Does the 2nd statement allocate data on heap? And there's a method .as_bytes() for String, does it copy the String data?
Aside from the two words used to store the pointer and length on the stack¹, an &[u8] doesn’t ever allocate memory. It instead points into some other object’s allocation, which can be either on the stack or the heap— It makes no difference which.
In this case, a is on the stack because it’s a local variable, and b points into a’s stack allocation.
¹ This might also end up on the heap if it’s stored inside some heap-using container, like Box or Vec.
Thanks for reminding me of that conversation chart. I met the author a while ago at a Rust conference and I'm sure we can use the ideas from the layout.
Yeah, I find it hard to follow discussions with this software because of the lack of clear threads. Not being able to quote a full (small) post doesn't help here.