Variable created on the heap still looks on the stack
⚓ Rust 📅 2026-02-15 👤 surdeus 👁️ 7I am trying to create a variable on the stack and one on the heap:
fn main() {
let mut x = 1;
let mut y = &x;
let mut z = Box::new(x);
let mut w = Box::new(2);
println!("&x = {:p}", &x);
println!("&y = {:p}", &y);
println!("&z = {:p}", &z);
println!("&w = {:p}", &w);
}
This is the output:
&x = 0x7fffe51ba364
&y = 0x7fffe51ba368
&z = 0x7fffe51ba370
&w = 0x7fffe51ba378
All these addresses are consecutive, while I would expect the last one to be farther away, since it should be allocated on the heap.
What am I doing wrong?
5 posts - 4 participants
🏷️ Rust_feed