bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Rust/Rust Data Structures
Rust•Rust Data Structures

Rust Structs

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Rust Structs?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ Person {
3Order

Put the learning moves in the order that makes the concept easiest to apply.

You define a struct using the struct keyword and place the fields (variables) inside:
You can think of a struct like a mini-database for one thing, like a person with a name and age.
A struct (short for "structure") is a custom data structure that lets you group related values together.

Structs

A struct (short for "structure") is a custom data structure that lets you group related values together.

You can think of a struct like a mini-database for one thing, like a person with a name and age.

Create a Struct

You define a struct using the struct keyword and place the fields (variables) inside:

Example

struct Person {
 name: String,
 age: u32, can_vote: bool,
}

Once you have a struct, you can create an object of it.

Then, you can access the fields of the struct using dot syntax ( . ):

Example

// Create a Struct called Person
struct Person {
  name: String, age: u32, can_vote: bool, }
// Create a Person object
let user = Person {
  name: String::from("John"), age: 35, can_vote: true, };
// Access and print the values
println!("Name: {}", user.name);
println!("Age: {}", user.age);
println!("Can vote? {}", user.can_vote);

Fields are similar to variables, but they belong to a struct. Since they are part of a larger structure (like Person or Car), they are called fields in Rust, not regular variables.

Change a Field

To change a value inside a struct, you must make the struct object mutable by using mut :

Example

struct Person {
  name: String, age: u32, }
let mut user = Person {
  name: String::from("John"), age: 35, };
user.age = 36; // Change value of age
println!("Name: {}", user.name);
println!("Updated age: {}", user.age);

Why Use Structs?

  • To group related data in a clean way
  • To make your code easier to read and maintain
  • To create real-world examples, like users, books, cars, etc.

Previous

Rust HashMap

Next

Rust Enums