How do I pass a large struct around without cloning it everywhere


I have a large struct and in an infinite loop, I need to pass it into a function.
I don’t want to copy the entire struct and it’s read-only so I wanted to pass a reference to it which worked fine until serde::Deserialize started to complain.

Then I tried passing a box and cloning the box around but im not very sure what happens when you clone a box is it just cloning the pointer variable or the entire thing so I gave up using this until its clear to me.

Last thing I tried passing an Rc it also works fine until serde::Deserialize again.
So im back to cloning a read-only struct all over the place and I guess it’s doing memory allocation and free all the time this way so whats the performant way of passing a data around while still being able to use serde?
And what does clone do on the box exactly is it cloning the entire data or just the pointer variable?


Box owns its data stored in heap. If the pointer is cloned but the content is not cloned, who owns that content? So Box clones its data when cloned.

Out of curiosity, why you need to deserialize it from bytes while you already have that struct? Can you post the actual code via


I really didnt think of that and you are right :smiley: @Hyeonu thanks