Skip to content
Fe 26.0 is not production-ready. This is an initial release of a new compiler. Learn more

Structs

Structs are custom data types that group related values under named fields. They’re fundamental for organizing data in Fe programs.

Define a struct with the struct keyword:

struct Point {
x: i32,
y: i32,
}

Each field has a name and a type. Fields are separated by commas.

By default, struct fields are private. Use pub to make them publicly accessible:

pub struct User {
pub name: String<20>,
pub balance: u256,
internal_id: u256, // Private field
}

The struct itself can also be public or private:

pub struct PublicStruct {
pub field: u256,
}
struct PrivateStruct {
field: u256,
}

Create a struct instance by specifying values for all fields:

struct Point {
x: i32,
y: i32,
}
let origin = Point { x: 0, y: 0 }
let point = Point { x: 10, y: 20 }

All fields must be initialized. There are no default values.

When a variable has the same name as a field, use shorthand syntax:

let x: i32 = 10
let y: i32 = 20
let point = Point { x, y } // Same as Point { x: x, y: y }

Access struct fields with dot notation:

let point = Point { x: 10, y: 20 }
let x_value = point.x // 10
let y_value = point.y // 20

For mutable struct instances, update fields directly:

let mut point = Point { x: 0, y: 0 }
point.x = 10
point.y = 20

Structs can have generic type parameters:

struct Pair<T> {
first: T,
second: T,
}
let int_pair: Pair<u256> = Pair { first: 1, second: 2 }
let bool_pair = Pair { first: true, second: false }

Use multiple generic parameters for different field types:

struct KeyValue<K, V> {
key: K,
value: V,
}
let entry: KeyValue<String<4>, u256> = KeyValue { key: "name", value: 42 }

Constrain generic types with trait bounds:

struct Container<T: Default> {
item: T,
}

Structs can contain other structs:

struct Point {
x: i32,
y: i32,
}
struct Rectangle {
top_left: Point,
bottom_right: Point,
}
let rect = Rectangle {
top_left: Point { x: 0, y: 10 },
bottom_right: Point { x: 10, y: 0 },
}
let x = rect.top_left.x // 0

Destructure structs in patterns:

let point = Point { x: 10, y: 20 }
let Point { x, y } = ref point
// x is 10, y is 20

Use .. to ignore remaining fields:

struct User {
name: u256,
balance: u256,
created_at: u256,
}
let User { name, .. } = user // Only extract name

Match on struct patterns:

let location = match point {
Point { x: 0, y: 0 } => "origin"
Point { x: 0, y: _ } => "on y-axis"
Point { x: _, y: 0 } => "on x-axis"
Point { x: _, y: _ } => "somewhere else"
}

Combine structs and tuples:

struct Line {
start: (i32, i32),
end: (i32, i32),
}
let line = Line {
start: (0, 0),
end: (100, 100),
}
let start_x = line.start.0
SyntaxDescription
struct Name { }Define a struct
pub field: TypePublic field
Struct { field: value }Create instance
Struct { field }Shorthand when variable matches field name
instance.fieldAccess field
struct Name<T>Generic struct
let Struct { field } = sDestructure struct
let Struct { field, .. } = sDestructure, ignoring other fields