Skip to main content

Structs

Structs are how FFS defines data with named fields, and optionally methods. They're declared with the struct keyword, like so:

struct MyFirstStruct {
// a plain field
some_number: i32
// fields can have default values
string_with_default: string = "hello"

// methods take a special parameter called "self"
fn increase_number(self, how_much: i32) {
self.some_number += how_much
}
}

Structs can be constructed by passing their fields as follows:

// we can pass field arguments by position
let from_positional = MyFirstStruct(1, "some string value")

// we can skip fields with defaults
let with_default = MyFirstStruct(2)

// we can also provide named arguments, for clarity
let named = MyFirstStruct(some_number = 3)

// if we used named arguments, the order doesn't matter
let named_order = MyFirstStruct(string_with_default = "sup?", some_number = 4)

And you can access fields and call functions on instances of structs using the . operator.

let my_instance = MyFirstStruct(1)

my_instance.increase_number(1)

let should_equal_2 = my_instance.some_number

Coming from languages like C#, the closest analogue would be a class. Structs, however, don't support inheritance, and FFS relies on traits instead of interfaces to support things like custom operators.