Using Structs to Structure Related Data

Structs let you group related data into a single type. In Oxide, structs look similar to Rust, but methods are implemented using extension blocks.

What You'll Learn

  • How to define and instantiate structs
  • How to use field shorthand and update syntax
  • How to add methods with extension

A Simple Example

public struct User {
    public username: String,
    public email: String,
    public active: Bool,
}

extension User {
    public static fn new(username: String, email: String): User {
        User {
            username,
            email,
            active: true,
        }
    }

    public mutating fn deactivate() {
        self.active = false
    }
}

In the next sections, we'll explore how struct syntax works and how to build methods that make your types easier to use.