In this blog, I write about Web Development, Blockchain, and DevOps. I am a Docker Captain, Public Speaker, and Developer Advocate at daily.dev. Check my YouTube channel.
In this lesson, we will talk about Structs in Rust
What are Structs?
Structs are a way to create more complex data types.
They are similar to tuples, but with a few differences.
For example, you can name the fields of a struct (and you can also define methods for structs).
If you prefer a video version
All the code is available on GitHub (link available in the video description)
Define a Struct
Let's say that we want to crete a User, with name, email, is_active and age. We can define a struct like this:
structUser {
name: String,
email: String,
is_active: bool,
age: u8,
}
And then you can create an instance of the structlike this:
```rust
structUser {
name: String,
email: String,
is_active: bool,
age: u8,
}
fnmain() {
let user = User {
name: String::from("Jhon Doe"),
email: String::from("doe@mail.com"),
is_active: true,
age: 25,
};
}
Accessing Struct Fields
You can access the fields of a struct using dot notation:
Note the you can't make individual fields of a struct mutable, you can only make the entire struct mutable.
Create a Struct instance using a function
You can create a function that returns a struct instance:
structUser {
name: String,
email: String,
is_active: bool,
age: u8,
}
fnmain() {
let user = create_user(String::from("Jhon Doe"), String::from("doe@mail"),);
println!("User name: {}", user.name);
}
fncreate_user(name: String, email: String,) -> User {
User {
name,
email,
is_active: true,
age : 25,
}
}
You can see the init shorthand syntax in the create_user function. This is a shorthand for initializing fields with variables that have the same name as the fields.
Creating Instances from Other Instances with Struct Update Syntax
You can create a new instance of a struct using another instance with the struct update syntax:
In this example, we define two tuple structs: Color and Point. We create instances of these tuple structs and access their fields using dot notation.
Unit-Like Structs
You can define a struct with no fields, called a unit-like struct:
#[derive(Debug)]structUser;
fnmain() {
let user = User;
println!("User: {:?}", user);
}
Unit-like structs are useful when you need to implement a trait on a type but don't have any data to store in the type.
Conclusion
Structs are a way to create more complex data types in Rust. They are similar to tuples, but with a few differences. You can name the fields of a struct (and you can also define methods for structs). You can also make a struct mutable by using the mut keyword.
You can create a function that returns a struct instance. You can create a new instance of a struct using another instance with the struct update syntax. You can define tuple structs and unit-like structs.
I hope this lesson was useful. In the next lesson, we will see a practical example of using structs in Rust.
If you prefer a video version
All the code is available on GitHub (link available in the video description)
You can find me on https://francescociulla.com and on Twitter/X