Struct Types

A struct type is a composite type consisting of named fields.

A struct is defined using the struct keyword:

struct_def = "struct" IDENT "{" [ struct_fields ] "}" ;
struct_fields = struct_field { "," struct_field } [ "," ] ;
struct_field = IDENT ":" type ;
struct Point {
    x: i32,
    y: i32,
}

fn main() -> i32 {
    let p = Point { x: 10, y: 20 };
    p.x + p.y  // 30
}

Struct fields are accessed using dot notation: value.field_name.

All fields must be initialized when creating a struct instance.

Field names must be unique within a struct.

Memory Layout

The memory layout rules described in this section are provisional and may change in future versions of Rue. The current design prioritizes simplicity over space efficiency.

Non-zero-sized types in Rue occupy one or more 8-byte slots. Scalar types (i8, i16, i32, i64, u8, u16, u32, u64, bool) each occupy one slot.

Struct fields are laid out in memory in declaration order, with each field occupying a number of slots determined by its type.

The size of a struct is the sum of the sizes of all its fields. There is no padding between fields or at the end of the struct.

// A struct with two i32 fields occupies 2 slots (16 bytes)
struct Point { x: i32, y: i32 }

// A struct with a nested struct occupies the sum of all nested field slots
struct Line { start: Point, end: Point }  // 4 slots (32 bytes)

Structs with one or more fields have 8-byte alignment. Empty structs (zero-sized types) have 1-byte alignment.

A struct with no fields is a zero-sized type. See Zero-Sized Types for the general definition.

// An empty struct is a zero-sized type
struct Empty {}

fn main() -> i32 {
    let e = Empty {};
    @size_of(Empty)  // 0
}