Structs
Structs are custom data types that group related values under named fields. They’re fundamental for organizing data in Fe programs.
Defining Structs
Section titled “Defining Structs”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.
Field Visibility
Section titled “Field Visibility”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,}Creating Instances
Section titled “Creating Instances”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.
Field Init Shorthand
Section titled “Field Init Shorthand”When a variable has the same name as a field, use shorthand syntax:
let x: i32 = 10let y: i32 = 20
let point = Point { x, y } // Same as Point { x: x, y: y }Accessing Fields
Section titled “Accessing Fields”Access struct fields with dot notation:
let point = Point { x: 10, y: 20 }
let x_value = point.x // 10let y_value = point.y // 20Updating Fields
Section titled “Updating Fields”For mutable struct instances, update fields directly:
let mut point = Point { x: 0, y: 0 }
point.x = 10point.y = 20Generic Structs
Section titled “Generic Structs”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 }Multiple Type Parameters
Section titled “Multiple Type Parameters”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 }Trait Bounds
Section titled “Trait Bounds”Constrain generic types with trait bounds:
struct Container<T: Default> { item: T,}Nested Structs
Section titled “Nested Structs”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 // 0Struct Patterns
Section titled “Struct Patterns”Destructure structs in patterns:
let point = Point { x: 10, y: 20 }
let Point { x, y } = point// x is 10, y is 20Use .. to ignore remaining fields:
struct User { name: u256, balance: u256, created_at: u256,}
let User { name, .. } = user // Only extract namePatterns in Match
Section titled “Patterns in Match”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"}Structs with Tuples
Section titled “Structs with Tuples”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.0Summary
Section titled “Summary”| Syntax | Description |
|---|---|
struct Name { } | Define a struct |
pub field: Type | Public field |
Struct { field: value } | Create instance |
Struct { field } | Shorthand when variable matches field name |
instance.field | Access field |
struct Name<T> | Generic struct |
let Struct { field } = s | Destructure struct |
let Struct { field, .. } = s | Destructure, ignoring other fields |