Is this example right?

I finished reading "Book" and now I read "Rust by Example".
Here I found example of code
This example output is

Point occupies 16 bytes on the stack
Rectangle occupies 32 bytes on the stack
Boxed point occupies 8 bytes on the heap
Boxed rectangle occupies 8 bytes on the heap
Boxed box occupies 8 bytes on the heap
Unboxed point occupies 16 bytes on the stack

All boxed data only show size of pointer, not size of data, but in my knowledge pointers live on stack and not on heap. They only point to data on heap. So, in my option this example is not right. It should say:

Boxed point occupies 8 bytes on the stack and 16 bytes on the heap.

Now it looks like point on the heap takes less memory than point on stack. Even boxed rectangle occupies only 8 bytes on heap. So if we moved all this forum data on heap it would take only 8 bytes in RAM :smiley:

1 Like

Right, I believe that's correct. You could submit an issue.

1 Like

That example definitely looks wrong. One potential fix would be something like this:

    println!("Boxed point occupies {} bytes on the stack",
             mem::size_of_val(&boxed_point));
    println!("Boxed point occupies {} bytes on the heap",
             mem::size_of_val(&*boxed_point));

which gives

Boxed point occupies 8 bytes on the stack
Boxed point occupies 16 bytes on the heap

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6f462dfb092be4022677d65a4c107b41

1 Like

I have submitted it on github