Skip to content

11. Structs

A struct groups values that belong together, each under a name of its own, its field. You declare the struct once, then build values of it:

struct Book
    title: String
    pages: Int
end

fn main()
    book = Book{title: "Matilda", pages: 240}
    dbg book.title
    dbg book.pages
end

book.title reads the title field. A struct’s name starts with a capital letter.

describe gets a Pet and should give back a sentence such as "Rex is a dog, 3 years old". Right now it puts only the name in the text.

struct Pet
    name: String
    kind: String
    age:  Int
end

fn describe(pet: Pet) -> String
    "${pet.name} is a pet"
end

// The checks below run when you press Check. You don't need to change them.
test "a dog"
    assert describe(Pet{name: "Rex", kind: "dog", age: 3}) == "Rex is a dog, 3 years old"
end

test "a cat"
    assert describe(Pet{name: "Tom", kind: "cat", age: 7}) == "Tom is a cat, 7 years old"
end