[The Rust Programming Language] 5. Using Structs to Structure Related Data
5. Using Structs to Structure Related Data
A struct, or structure, is a custom data type that lets you name and package together multiple related values that make up a meaningful group.
Structs and enums (discussed in Chapter 6) - https://doc.rust-lang.org/book/ch06-00-enums.html are the building blocks for creating new types in your program’s domain to take full advantage of Rust’s compile time type checking.
5.1 Defining and Instantiating Structs
Structs are similar to tuples, which were discussed in Chapter 3. Like tuples, the pieces of a struct can be different types. Unlike with tuples, you’ll name each piece of data so it’s clear what the values mean. As a result of these names, structs are more flexible than tuples: you don’t have to rely on the order of the data to specify or access the values of an instance.
To define a struct, we enter the keyword struct and name the entire struct. A struct’s name should describe the significance of the pieces of data being grouped together. Then, inside curly brackets, we define the names and types of the pieces of data, which we call fields.
1 | struct User { |
To use a struct after we’ve defined it, we create an instance of that struct by specifying concrete values for each of the fields. We create an instance by stating the name of the struct and then add curly brackets containing key: value pairs, where the keys are the names of the fields and the values are the data we want to store in those fields.
To get a specific value from a struct, we can use dot notation .
. If the instance is mutable, we can change a value by using the dot notation and assigning into a particular field.
1 | let mut user1 = User { |
Note that the entire instance must be mutable; Rust doesn’t allow us to mark only certain fields as mutable.
Using the Field Init Shorthand when Variables and Fields Have the Same Name
Because the parameter names and the struct field names are exactly the same, we can use the field init shorthand syntax to rewrite build_user so that it behaves exactly the same but doesn’t have the repetition of email and username.
1 | fn build_user(email: String, username: String) -> User { |
Creating Instances From Other Instances With Struct Update Syntax
It’s often useful to create a new instance of a struct that uses most of an old instance’s values but changes some. You’ll do this using struct update syntax.
Using struct update syntax, we can achieve the same effect with less code. The syntax ..
specifies that the remaining fields not explicitly set should have the same value as the fields in the given instance.
1 | let user1 = User { |
The syntax ..
specifies that the remaining fields not explicitly set should have the same value as the fields in the given instance.
Using Tuple Structs without Named Fields to Create Different Types
Tuple structs have the added meaning the struct name provides but don’t have names associated with their fields; rather, they just have the types of the fields. Tuple structs are useful when you want to give the whole tuple a name and make the tuple be a different type from other tuples, and naming each field as in a regular struct would be verbose or redundant.
To define a tuple struct, start with the struct keyword and the struct name followed by the types in the tuple. For example, here are definitions and usages of two tuple structs named Color and Point:
1 | struct Color(i32, i32, i32); |
Unit-Like Structs Without Any Fields
You can also define structs that don’t have any fields! These are called unit-like structs because they behave similarly to (), the unit type. Unit-like structs can be useful in situations in which you need to implement a trait on some type but don’t have any data that you want to store in the type itself. We’ll discuss traits in Chapter 10 - https://doc.rust-lang.org/book/ch10-00-generics.html.
Ownership of Struct Data
It’s possible for structs to store references to data owned by something else, but to do so requires the use of lifetimes, a Rust feature that we’ll discuss in Chapter 10. Lifetimes ensure that the data referenced by a struct is valid for as long as the struct is. Let’s say you try to store a reference in a struct without specifying lifetimes, like this, which won’t work:
1 | struct User { |
The compiler will complain that it needs lifetime specifiers:
1 | cargo run |
In Chapter 10 - https://doc.rust-lang.org/book/ch10-00-generics.html, we’ll discuss how to fix these errors so you can store references in structs
5.2 An Example Program Using Structs
1 | struct Rectangle { |
Adding Useful Functionality with Derived Traits
Putting the specifier :?
inside the curly brackets tells println!
we want to use an output format called Debug. The Debug trait enables us to print our struct in a way that is useful for developers so we can see its value while we’re debugging our code.
1 |
|
5.3 Method Syntax
Methods are similar to functions: they’re declared with the fn keyword and their name, they can have parameters and a return value, and they contain some code that is run when they’re called from somewhere else.
However, methods are different from functions in that they’re defined within the context of a struct (or an enum or a trait object, which we cover in Chapters 6 and 17, respectively), and their first parameter is always self, which represents the instance of the struct the method is being called on.
Defining Methods
1 |
|
Methods can take ownership of self
, borrow self
immutably as we’ve done here, or borrow self mutably, just as they can any other parameter.
Having a method that takes ownership of the instance by using just self as the first parameter is rare; this technique is usually used when the method transforms self into something else and you want to prevent the caller from using the original instance after the transformation.
The main benefit of using methods instead of functions, in addition to using method syntax and not having to repeat the type of self in every method’s signature, is for organization. We’ve put all the things we can do with an instance of a type in one impl block rather than making future users of our code search
Where’s the -> Operator?
In C and C++, two different operators are used for calling methods: you use .
if you’re calling a method on the object directly and ->
if you’re calling the method on a pointer to the object and need to dereference the pointer first. In other words, if object is a pointer, object->something()
is similar to (*object).something()
.
Rust doesn’t have an equivalent to the -> operator; instead, Rust has a feature called automatic referencing and dereferencing. Calling methods is one of the few places in Rust that has this behavior.
Here’s how it works: when you call a method with object.something()
, Rust automatically adds in &
, &mut
, or *
so object matches the signature of the method. In other words, the following are the same:
1 | p1.distance(&p2); |
The first one looks much cleaner. This automatic referencing behavior works because methods have a clear receiver—the type of self. Given the receiver and name of a method, Rust can figure out definitively whether the method is reading (&self)
, mutating (&mut self)
, or consuming (self)
. The fact that Rust makes borrowing implicit for method receivers is a big part of making ownership ergonomic in practice.
Associated Functions
Another useful feature of impl blocks is that we’re allowed to define functions within impl blocks that don’t take self as a parameter. These are called associated functions because they’re associated with the struct. They’re still functions, not methods, because they don’t have an instance of the struct to work with.
Associated functions are often used for constructors that will return a new instance of the struct. For example, we could provide an associated function that would have one dimension parameter and use that as both width and height, thus making it easier to create a square Rectangle rather than having to specify the same value twice:
1 | impl Rectangle { |
To call this associated function, we use the ::
syntax with the struct name. This function is namespaced by the struct: the ::
syntax is used for both associated functions and namespaces created by modules.
Multiple impl Blocks
Each struct is allowed to have multiple impl blocks.
1 | impl Rectangle { |
Summary
Structs let you create custom types that are meaningful for your domain. By using structs, you can keep associated pieces of data connected to each other and name each piece to make your code clear. Methods let you specify the behavior that instances of your structs have, and associated functions let you namespace functionality that is particular to your struct without having an instance available.
But structs aren’t the only way you can create custom types: let’s turn to Rust’s enum feature to add another tool to your toolbox.