Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Structs

⚠️ NOTE: the current syntax to define structs is still type X struct { .. }. This will be changed soon.

You can also define, create and manipulate structs:

// First, you need to define the type
struct Person {
  name: String,
  age: Int,
}

// you can then instantiate it:
let p = Person {
  name: "John Doe",
  age: 42
}

// you can access fields of a struct:
let age = p.age

// and you can perform immutable updates using the spread syntax:
let older_person = Person {
  age: p.age + 1,
  ..p
}

Structs can have type parameters:

struct Box<a> {
  inner: a,
}

Optional fields

⚠️ NOTE: optional fields aren't implemented yet.

You can use the ? modifier on a struct definition to make it optional:

struct Person {
  name: String,
  age?: Int,
}

// this way, you can omit the `age` field
let p = Person {
  name: "John Doe",
}

// inferred as: `Option<Int>`
let age = p.age

// you can still pass the field normally:
let p2 = Person {
  name: "John Doe",
  age: 42,
}

In case you need to dynamically decide not, whether to pass the value or, you can use the ? modifier to pass an Option:

let p = Person {
  age?: Some(42),
  // ..
}