In C and some other languages, a struct has a list of fields declared. Fields
are laid out in memory in the declared order. Due to layout rules, there might be
padding between fields or at the end of a struct to align fields to be "easily
accessible" from a CPU.
The order of fields in a C struct can be considered part of the API of the
struct. If the order of the fields is changed, the new struct is not
compatible with the old one.
In Rust, the order of the fields can be changed arbitrarily by the compiler.
Even two different Rust structs with the same number of fields and field types
may have different layouts. There are no stable ABI guarantees for Rust.
struct A {
name: String
points: u32,
}
// May have the same layout as `A` or a different one.
struct B {
name: String
points: u32,
}
Usually, the compiler optimizes the layout for the best performance.
Some people such as myself may conclude that field declaration order does not
matter in Rust. However, field declaration still matters because when a struct
is dropped, then the fields are dropped in declaration order.
In the vast majority of cases, fields can be dropped in arbitrary order. In rare cases, a field may rely on another field to still exist. There may be resources which are dropped by an earlier field which a later field assumes still exists.
Just a word of caution if some mysterious crashes occur in a Drop
implementation in Rust.