Rust Tutorials: Struct Definition 🎯

beginner
12 min

Rust Tutorials: Struct Definition 🎯

Welcome to our Rust Struct Definition lesson! In this tutorial, we'll dive into one of the most important concepts in Rust – Structs (structures). By the end of this lesson, you'll understand what structs are, how to define them, and how to use them in your Rust projects. Let's get started!

What are Structs in Rust? 📝

In Rust, a struct (short for structure) is a custom data type used to combine multiple related values into a single entity. Think of it like a blueprint for creating complex data types that hold multiple variables.

Why use Structs?

  • Organize related data: Structs help to group related data together, making code more readable and easier to manage.
  • Define custom types: You can create your own data types tailored to your specific needs.
  • Encapsulate data: Structs can include functions, enabling you to encapsulate related data and behavior.

Defining a Struct 💡

Let's create a simple struct to represent a Person.

rust
struct Person { first_name: String, last_name: String, age: u32, }

Here's a breakdown of the code above:

  • struct keyword: Declares a new struct.
  • Person: The name of our custom data type.
  • first_name: String, last_name: String, age: u32: Fields containing the data we want to store. Each field has a name and a type.

Creating a new Struct Instance 💡

Now that we've defined the Person struct, we can create a new instance of it.

rust
let john = Person { first_name: String::from("John"), last_name: String::from("Doe"), age: 30, };

In the example above, we created a new Person instance called john and provided values for its fields.

Accessing Struct Fields 💡

To access the fields of a struct instance, you can use the dot (.) notation.

rust
println!("Name: {}", john.first_name); println!("Age: {}", john.age);

In the example above, we printed the first name and age of the john instance.

Modifying Struct Fields 💡

You can modify struct fields by assigning new values to them.

rust
john.age += 1; println!("John's new age: {}", john.age);

In the example above, we incremented John's age by 1.

Struct Methods 💡

You can also define methods on structs to provide additional functionality. Let's add a method to our Person struct that prints the person's full name.

rust
impl Person { fn full_name(&self) -> String { format!("{} {}", self.first_name, self.last_name) } } println!("Full name: {}", john.full_name());

In the example above, we defined a method called full_name on our Person struct. This method takes no arguments and returns a String containing the person's full name.

Struct Updates and Deconstruction 💡

Rust allows you to update struct fields by creating a new instance with the updated values, and deconstructing the original instance to update it.

rust
let updated_john = Person { first_name: String::from("Johnny"), last_name: String::from("Doe"), age: john.age + 1, }; john = updated_john;

In the example above, we created a new Person instance called updated_john with updated values for its fields. Then, we assigned the updated_john instance back to the john variable, effectively updating the john instance.

Quiz 💡

Quick Quiz
Question 1 of 1

What is the purpose of using a struct in Rust?


That's it for our Struct Definition lesson! By now, you should have a good understanding of structs in Rust. Keep practicing, and you'll be creating complex data structures in no time!

In the next lesson, we'll explore enums, another powerful tool for structuring your data in Rust. Until then, happy coding! 🎯💡📝