Skip to content

Structs

Structs are user-defined data types with named fields.

Defining Structs

struct Person {
    name,
    age,
    city
}

Creating Instances

let alice = Person {
    name: "Alice",
    age: 30,
    city: "NYC"
};

let bob = Person {
    name: "Bob",
    age: 25,
    city: "LA"
};

Accessing Fields

print alice.name;  // Alice
print alice.age;   // 30
print alice.city;  // NYC

Using in Functions

fn greet_person(person) {
    print "Hello, #{person.name} from #{person.city}!";
}

greet_person(alice);

Nested Structs

struct Address {
    street,
    city,
    zip
}

struct Employee {
    name,
    position,
    address
}

let addr = Address {
    street: "123 Main St",
    city: "Boston",
    zip: "02101"
};

let emp = Employee {
    name: "Charlie",
    position: "Developer",
    address: addr
};

print emp.address.city;  // Boston