Problem with serde serialize of slice of structs

Rule of thumb is: if you had slice in Golang, you probably want Vec in Rust. In Rust the slice, or [T] is not a kind of collection type. It really is a memory chunk which consists of zero or more value of same type, placed directly next to each other. As it doesn't care about the heap memory, even stack-allocated fixed-size C-like arrays can be used as slice. Definition really matters!

Though people usually call it just "slice" , &[T] actually is a "refference to slice". Just like other references &[T] doesn't own the slice. Instead, it just references the slice owned by someone else.

By definition a slice's size can't be determined at compile time, as its length can only be known at runtime. In Rust we call such types "Unsized type". They exist in type system, but cannot be placed on the variable as Rust doesn't allows dynamic stack frame. Instead, you can use some wrapper types to use them. Stack-allocated array is a one way, but their size must be known at compile time. In most case the Vec<T>, or the growable array should be your mate. There also is Box<[T]>, but you can't easily modify it's length after construction.

4 Likes