Rust Lesson 18 – Structs | Dataplexa

Structs in Rust

In this lesson, you will learn about structs in Rust. Structs allow you to group related data together into a single custom data type.

They are fundamental for modeling real-world entities and building complex programs.


What Is a Struct?

A struct is a custom data type that lets you combine multiple values of different types. Each value inside a struct is called a field.

Structs are similar to classes in other languages but do not include methods by default.


Defining a Struct

You define a struct using the struct keyword.

struct User {
    username: String,
    email: String,
    active: bool,
}

This defines a struct named User with three fields.


Creating Struct Instances

To use a struct, you create an instance by providing values for all its fields.

let user1 = User {
    username: String::from("alice"),
    email: String::from("alice@example.com"),
    active: true,
};

All fields must be initialized when creating an instance.


Accessing Struct Fields

You can access fields using dot notation.

println!("{}", user1.username);
println!("{}", user1.email);

Mutable Structs

If you want to modify a struct’s fields, the entire instance must be mutable.

let mut user2 = User {
    username: String::from("bob"),
    email: String::from("bob@example.com"),
    active: false,
};

user2.active = true;

Field Init Shorthand

When variable names match struct field names, Rust allows a shorthand syntax.

fn build_user(username: String, email: String) -> User {
    User {
        username,
        email,
        active: true,
    }
}

Struct Update Syntax

You can create a new struct using values from an existing one.

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

This copies remaining fields from user1.


Tuple Structs

Tuple structs are structs without named fields. They are useful when field names are unnecessary.

struct Color(i32, i32, i32);

let red = Color(255, 0, 0);

Unit-Like Structs

Unit-like structs have no fields. They are useful for traits or marker types.

struct Marker;

Ownership and Structs

Struct fields follow ownership rules. If a struct owns data, it is dropped when the struct goes out of scope.

Fields that hold references must use lifetime annotations.


Why Structs Matter

Structs allow Rust programs to:

  • Model real-world data
  • Group related values together
  • Improve code readability
  • Build scalable applications

📝 Practice Exercises


Exercise 1

Create a struct representing a book with title and author fields.

Exercise 2

Create a mutable struct instance and update one field.

Exercise 3

Use field init shorthand in a function.

Exercise 4

Create a tuple struct and access its values.


✅ Practice Answers


Answer 1

struct Book {
    title: String,
    author: String,
}

Answer 2

let mut book = Book {
    title: String::from("Rust"),
    author: String::from("Someone"),
};

book.title = String::from("Rust Programming");

Answer 3

fn new_book(title: String, author: String) -> Book {
    Book { title, author }
}

Answer 4

struct Point(i32, i32);

let p = Point(3, 4);
println!("{}", p.0);

What’s Next?

Now that you understand structs, the next lesson will introduce enums, which allow values to be one of several variants.