5
votes

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.

1
My guess would be no, since unsafe code can still see the difference and since you need particular layout to interface with functions written in other languages, which Rust supports linking with. But I'm not sufficiently familiar with Rust to know for sure. - R.. GitHub STOP HELPING ICE
Unlike in C, Rust structure fields order is not guaranteed, unless it is repr(C). Read more here - Eugene Sh.

1 Answers

8
votes

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 the repr attribute. In either case, fields may be given in any order in a corresponding struct expression; the resulting struct value will always have the same memory layout.