strum icon indicating copy to clipboard operation
strum copied to clipboard

Question about effect on memory allocation

Open dmr07 opened this issue 2 years ago • 1 comments

Hi,

I'm looking to use this strum's #[derive(Display)] feature, but I'm fairly new to rust and I'm not sure how this will affect memory allocation. Namely for a simple enumerations below, Thing would allocated on the stack since it's of known size, and I was wondering if using the derive display method would cause Thing to be moved to the heap. My guess is likely not, but I wanted to make sure:

#[derive(Display)]
enum Letters { A, B, C, D }

struct Thing { letters: Letters }

dmr07 avatar May 05 '22 19:05 dmr07

@dmr07 Letters & Thing will remain on stack. Only calling Letters::to_string will allocate memory on heap for the output string. However, you can use #[derive(IntoStaticStr)] to avoid allocations altogether.

Logarithmus avatar Jun 01 '22 20:06 Logarithmus

@dmr07, it looks like your question was answered so I'm going to close this, but feel free to re-open if needed.

Peternator7 avatar Oct 08 '22 19:10 Peternator7