Rust Struct

In this tutorial, you’ll learn how to create your own data types with Rust structs.

What is a Struct? #

A struct (short for “structure”) is a way to group related data together under one name. Think of it like a blueprint for making objects.

In other languages, structs are similar to classes without methods.

Defining a Struct #

Here’s how you define a struct:

struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}Code language: Rust (rust)

In this struct:

  • A User has a username (a String).
  • An email (a String).
  • A sign_in_count (a u64 number).
  • An active flag (a bool).

Creating a Struct Instance #

fn main() {
    let user1 = User {
        username: String::from("alice"),
        email: String::from("[email protected]"),
        sign_in_count: 1,
        active: true,
    };

    println!("Username: {}", user1.username);
    println!("email: {}", user1.email);
    println!("sign_in_count: {}", user1.sign_in_count);
    println!("active: {}", user1.active);
}

struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}Code language: Rust (rust)

Output:

Username: alice
email: [email protected]
sign_in_count: 1
active: trueCode language: HTTP (http)

This creates a User instance called user1. You can access fields using dot notation (user1.username).

Mutating a Struct #

By default, structs are immutable. To change fields, declare it as mut:

fn main() {
    let mut user1 = User {
        username: String::from("bob"),
        email: String::from("[email protected]"),
        sign_in_count: 1,
        active: true,
    };

    user1.email = String::from("[email protected]");
    println!("Updated email: {}", user1.email);
}

struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}Code language: Rust (rust)

Using Struct Update Syntax #

If you want to create a new struct based on an existing one:

fn main() {
    let user1 = User {
        username: String::from("charlie"),
        email: String::from("[email protected]"),
        sign_in_count: 5,
        active: true,
    };

    let user2 = User {
        username: String::from("dave"),
        ..user1
    };

    println!("user2 username: {}", user2.username);
    println!("user2 email: {}", user2.email);
    println!("user2 sign_in_count: {}", user2.sign_in_count);
    println!("user2 active: {}", user2.active);
    
}

struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}
Code language: Rust (rust)

Output:

user2 username: dave
user2 email: charlie@rusttutorial.com
user2 sign_in_count: 5
user2 active: trueCode language: CSS (css)

In this example, the ..user1 copies the remaining fields from user1.

Note: user1 can no longer be used after this, since ownership of its fields has moved to user2.

Tuple Structs #

Structs can also look like tuples but with names:

struct Color(i32, i32, i32);

fn main() {
    let black = Color(0, 0, 0);
    println!("Black color: {}, {}, {}", black.0, black.1, black.2);
}Code language: Rust (rust)

Fields are accessed by index instead of name.

Unit-Like Structs #

Structs with no fields are valid too:

struct Marker;

fn main() {
    let _m = Marker;
}Code language: Rust (rust)

These are useful for traits or situations where you just need a “type marker.”

Summary #

  • Structs group related data into one custom type.
  • Use dot notation to access fields.
  • Use mut to make them mutable.
  • Struct update syntax helps reuse data.
  • Rust also has tuple structs and unit-like structs.
Was this Helpful ?