Table of Contents📚
Struct methods🛠️
What is a struct method❔
Structs methods are functions that are attached to structs, to make them easier to use.
The impl
keyword
If we have a struct named Person
struct Person {
name: String,
age: u8,
hobbies: Vec<String>,
country: String,
company: String,
}
ℹ️
hobbies
is a vector of strings.
We can create a method for the struct that will introduce the person.
To declare methods for a struct, we use the impl
keyword, like this:
impl Person {
fn introduce(&self) {
println!("Hi, my name is {}, I'm {} years old and I live in {}. I work at {} and my hobbies are: {}", self.name, self.age, self.country, self.company , self.hobbies.join(", "));
}
}
ℹ️ The function takes the
&self
parameter, that corresponds to the struct instance that we want to use the method on.
ℹ️ The join method is a method that joins the items of a vector with a separator to create a string.
We can now create a new instance of the struct and use the method.
fn main() {
let person = Person {
name: String::from("Léopold"),
age: 13,
hobbies: vec![String::from("💻"), String::from("🛌"), String::from("🍔")],
country: String::from("France 🇫🇷"),
company: String::from("Skwal-net"),
};
person.introduce();
}
ℹ️ The
String::from
is a function that creates aString
from from astr
.str
andString
are different types, but they are both used to represent strings.String
have a lot of methods, butstr
only have a few. By default, in the code"hello"
, the type isstr
.
Output:
Hi, my name is Léopold, I'm 13 years old and I live in France 🇫🇷. I work at Skwal-net and my hobbies are: 💻, 🛌, 🍔
Multiple methods
We can also create multiple methods for a struct.
let’s add a method is_adult
to the struct Person
:
impl Person {
fn introduce(&self) {
...
}
fn is_adult(&self) -> bool {
self.age >= 18
}
}
ℹ️ the
is_adult
doesn’t need thereturn
keyword, because when a method returns a value, it returns the value of the last expression.
We can now use the method is_adult
on the instance of the struct.
fn main() {
let person = Person {
...
};
if person.is_adult() {
println!("{} is an adult ✅", person.name);
} else ❔{
println!("{} is not an adult ⛔", person.name);
}
}
Output:
Léopold is not an adult ⛔