Struct

Structs are types that wrap values of other types, which are called its fields.

Structs that contain no values or only zero-sized types are also zero-sized; they only exist in the type system, but are completely optimized away at runtime. Structs containing exactly one value are called newtypes. They are used to give an existing type a new meaning and/or new functionality.

Structs are equivalent to struct types in C and the Lisp family, and to record types of the ML family; they are sometimes called product types.

Struct fields are private by default and can have visibility modifiers.

Types of structs
Syntactically, there are three types of structs: Regular structs, tuple structs and unit structs.

Regular structs
Regular structs have curly braces and named fields as in the example above. When instantiating a regular struct or pattern-matching on it, the fields can be specified in any order, so reordering the fields of a regular struct is backwards compatible, but renaming them is not.

Tuple structs
Tuple structs use parentheses instead of curly braces; the fields are unnamed, and are accessed with their index, starting from zero:

Unlike regular structs, the field order in tuple structs is significant.

Tuple structs are callable, so their name can be used like a closure or function pointer. Furthermore, while all types of structs can be instantiated and pattern-matched with curly braces, only tuple structs can be instantiated and pattern-matched with parentheses.

Unit structs
When declaring a struct with no fields, it is possible to omit the braces. This means that the braces also have to be omitted when instantiating the struct or pattern-matching on it:

Struct update syntax
The struct update syntax is a syntactic sugar to make initializing a struct easier in certain cases. It copies or moves the missing fields from a previous value of the same struct:

The struct update syntax must come after all specified fields, and can't be followed by a comma. The omitted fields must be visible in the current module.

Fill in default fields
The struct update syntax can be used with the trait to fill in default fields:

Accessing fields
Fields are accessed with the dot syntax. Like method calls, this performs auto-dereferencing, so it's possible to access fields of a struct through smart pointers such as. Accessing a field is a place expression.

If the type of a field doesn't implement and the struct doesn't implement, the field can be moved out of the struct. Rust ensures that each field is moved out of a struct at most once, and that the struct isn't passed to a function after that.

Memory representation
The default representation of structs is unspecified. Rust will make sure that all fields are properly aligned by adding padding bytes and possibly reordering the fields. This behavior can be customized with the attribute. To prevent field reordering and guarantee the same representation as an equivalent struct in C,  must be used. If the struct contains only one value that isn't zero-sized,  can be used to ensure that the struct has the same representation as its inner type.