Structs
Struct ->
StructStruct
| TupleStruct
StructStruct ->
`struct` IDENTIFIER GenericParams? WhereClause? ( `{` StructFields? `}` | `;` )
TupleStruct ->
`struct` IDENTIFIER GenericParams? `(` TupleFields? `)` WhereClause? `;`
StructFields -> StructField (`,` StructField)* `,`?
StructField -> OuterAttribute* Visibility? IDENTIFIER `:` Type
TupleFields -> TupleField (`,` TupleField)* `,`?
TupleField -> OuterAttribute* Visibility? Type
A struct is a nominal struct type defined with the keyword struct.
A struct declaration defines the given name in the type namespace of the module or block where it is located.
An example of a struct item and its use:
let p = Point ;
let px: i32 = p.x;
A tuple struct is a nominal tuple type, and is also defined with the keyword struct. In addition to defining a type, it also defines a constructor of the same name in the value namespace. The constructor is a function which can be called to create a new instance of the struct. For example:
;
let p = Point;
let px: i32 = match p ;
A unit-like struct is a struct without any fields, defined by leaving off the list of fields entirely. Such a struct implicitly defines a constant of its type with the same name. For example:
;
let c = ;
is equivalent to
const Cookie: Cookie = Cookie ;
let c = ;
The precise memory layout of a struct is not specified. One can specify a particular layout using the repr attribute.