I know that in C the compiler is not allowed to reorder struct fields and that this is important to the memory layout and alignment of the struct.
I am a beginner in Rust, and since it seems that for the most part raw pointers are hidden (I know you can still use them) if rustc is allowed to reorder the fields from their declared order in a struct.
The order of fields in a struct does matter - the compiler is not allowed to reorder fields, so the size of the struct may change as the result of adding some padding.
To define a struct, we enter the keyword struct and name the entire struct. A struct's name should describe the significance of the pieces of data being grouped together. Then, inside curly brackets, we define the names and types of the pieces of data, which we call fields.
From the rust reference struct.md:
The memory layout of a
struct
is undefined by default to allow for compiler optimizations like field reordering, but it can be fixed with therepr
attribute. In either case, fields may be given in any order in a corresponding struct expression; the resultingstruct
value will always have the same memory layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With