Getting Start
rustup
: a command line tool for managing Rust versions and associated tools.
rustc
: a command line tool for compiling Rust code.
rustfmt
: a command line automatic formatter tool to format your code in a particular style.
rust-analyzer
: IDE support.
cargo
: Rust’s build system and package manager.
rustc --version
rustc [your_file_path].rs
rustup update
rustup self uninstall
rustup doc
cargo --version
cargo new [your_project_name]
cargo new [your_project_name] --vcs=git
cargo new [your_project_name] --vcs
cargo build
cargo build --release
cargo run
cargo check
cargo update
cargo doc --open
Compiling and Running Are Separate Steps
Rust files always end with the .rs extension. If you’re using more than one word in your filename, the convention is to use an underscore to separate them. For example, use hello_world.rs rather than helloworld.rs.
Before running a Rust program, you must compile it using the Rust compiler by entering the rustc
command and passing it the name of your source file, like this:
rustc main.rs
After compiling successfully, Rust outputs a binary executable.
On Linux, macOS, and PowerShell on Windows, you can see the executable by entering the ls
command in your shell:
$ ls
main main.rs
On Linux and macOS, you’ll see two files. With PowerShell on Windows, you’ll see the same three files that you would see using CMD. With CMD on Windows, you would enter the following:
> dir /B %= the /B option says to only show the file names =%
main.exe
main.pdb
main.rs
This shows the source code file with the .rs extension, the executable file (main.exe on Windows, but main on all other platforms), and, when using Windows, a file containing debugging information with the .pdb extension. From here, you run the main or main.exe file, like this:
./main
Anatomy of a Cargo Project
cargo new hello_cargo
cd hello_cargo
Go into the hello_cargo directory and list the files. You’ll see that Cargo has generated two files and one directory for us: a Cargo.toml file and a src directory with a main.rs file inside.
Cargo expects your source files to live inside the src directory. The top-level project directory is just for README files, license information, configuration files, and anything else not related to your code.
It has also initialized a new Git repository along with a .gitignore file. Git files won’t be generated if you run cargo new
within an existing Git repository; you can override this behavior by using cargo new --vcs=git
.
Note: Git is a common version control system. You can change cargo new
to use a different version control system or no version control system by using the --vcs
flag.
Open Cargo.toml in your text editor of choice.
Filename: Cargo.toml
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
This file is in the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration format.
The first line, [package]
, is a section heading that indicates that the following statements are configuring a package.
The last line, [dependencies]
, is the start of a section for you to list any of your project’s dependencies. In Rust, packages of code are referred to as crates.
Now open src/main.rs and take a look:
Filename: src/main.rs
fn main() {
println!("Hello, world!");
}
The main
function is special: it is always the first code that runs in every executable Rust program.
Rust style is to indent with four spaces, not a tab.
println!
calls a Rust macro. If it had called a function instead, it would be entered as println
(without the !
). For now, you just need to know that using a !
means that you’re calling a macro instead of a normal function and that macros don’t always follow the same rules as functions.
you see the "Hello, world!"
string. We pass this string as an argument to println!
, and the string is printed to the screen.
we end the line with a semicolon (;
), which indicates that this expression is over and the next one is ready to begin. Most lines of Rust code end with a semicolon.
Programming a Guessing Game
cargo new guessing_game
cd guessing_game
Filename: Cargo.toml
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8.5"
Cargo understands Semantic Versioning (sometimes called SemVer), which is a standard for writing version numbers. The specifier 0.8.5
is actually shorthand for ^0.8.5
, which means any version that is at least 0.8.5 but below 0.9.0.
When you build a project for the first time, Cargo figures out all the versions of the dependencies that fit the criteria and then writes them to the Cargo.lock file. When you build your project in the future, Cargo will see that the Cargo.lock file exists and will use the versions specified there rather than doing all the work of figuring out versions again. In other words, your project will remain at 0.8.5 until you explicitly upgrade, thanks to the Cargo.lock file.
When you do want to update a crate, Cargo provides the command update
, which will ignore the Cargo.lock file and figure out all the latest versions that fit your specifications in Cargo.toml. Cargo will then write those versions to the Cargo.lock file. Otherwise, by default, Cargo will only look for versions greater than 0.8.5 and less than 0.9.0. If the rand
crate has released the two new versions 0.8.6 and 0.9.0, you would see the version bumped to 0.8.6 rather than 0.9.0. To use rand
version 0.9.0 or any version in the 0.9.x series, you’d have to update the Cargo.toml file manually.
Note: Another neat feature of Cargo is that running the cargo doc --open
command will build documentation provided by all your dependencies locally and open it in your browser.
Filename: src/main.rs
use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1..=100);
loop {
println!("Please input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {guess}");
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}
Common Programming Concepts
Variables and Mutability
Keywords
The Rust language has a set of keywords that are reserved for use by the language only, much as in other languages. Keep in mind that you cannot use these words as names of variables or functions. You can find a list of the keywords in Appendix A.
By default, variables are immutable. When a variable is immutable, once a value is bound to a name, you can’t change that value.
Filename: src/main.rs
fn main() {
let x = 5;
println!("The value of x is: {x}");
x = 6;
println!("The value of x is: {x}");
}
But mutability can be very useful, and can make code more convenient to write. Although variables are immutable by default, you can make them mutable by adding mut
in front of the variable name.
Filename: src/main.rs
fn main() {
let mut x = 5;
println!("The value of x is: {x}");
x = 6;
println!("The value of x is: {x}");
}
Constants
Differences between constants and variables:
- You declare constants using the
const
keyword instead of the let
keyword.
- You aren’t allowed to use
mut
with constants. Constants’re always immutable.
- The type of the value must be annotated.
- Constants can be declared in any scope, including the global scope.
- Constants may be set only to a constant expression, not the result of a value that could only be computed at runtime.
Here’s an example of a constant declaration:
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
Rust’s naming convention for constants is to use all uppercase with underscores between words.
Constants are valid for the entire time a program runs, within the scope in which they were declared.
Shadowing
You can declare a new variable with the same name as a previous variable. In effect, the second variable overshadows the first, taking any uses of the variable name to itself until either it itself is shadowed or the scope ends. We can shadow a variable by using the same variable’s name and repeating the use of the let
keyword as follows:
Filename: src/main.rs
fn main() {
let x = 5;
let x = x + 1;
{
let x = x * 2;
println!("The value of x in the inner scope is: {x}");
}
println!("The value of x is: {x}");
}
Differences between shadowing and making a variable as mut
:
we’ll get a compile-time error if we accidentally try to reassign to this variable without using the let
keyword. By using let
, we can perform a few transformations on a value but have the variable be immutable after those transformations have been completed.
we’re effectively creating a new variable when we use the let
keyword again, we can change the type of the value but reuse the same name.
let spaces = " ";
let spaces = spaces.len();
However, if we try to use mut
for this, as shown here, we’ll get a compile-time error:
let mut spaces = " ";
spaces = spaces.len();
Data Types
Keep in mind that Rust is a statically typed language, which means that it must know the types of all variables at compile time. The compiler can usually infer what type we want to use based on the value and how we use it.
In cases when many types are possible, such as when we converted a String
to a numeric type using parse
, we must add a type annotation, like this:
let guess: u32 = "42".parse().expect("Not a number!");
Scalar Types
A scalar type represents a single value. Rust has four primary scalar types: integers, floating-point numbers, Booleans, and characters.
Integer Types
An integer is a number without a fractional component.
Length |
Signed |
Unsigned |
8-bit |
i8 |
u8 |
16-bit |
i16 |
u16 |
32-bit |
i32 |
u32 |
64-bit |
i64 |
u64 |
128-bit |
i128 |
u128 |
arch |
isize |
usize |
Each signed variant can store numbers from -2^(n - 1) to 2^(n - 1) - 1 inclusive, where n is the number of bits that variant uses. So an i8
can store numbers from -2^7 to 2^7 - 1, which equals -128 to 127.
Unsigned variants can store numbers from 0 to 2^n - 1. So a u8
can store numbers from 0 to 2^8 - 1, which equals 0 to 255.
Additionally, the isize
and usize
types depend on the architecture of the computer your program is running on, which is denoted in the table as “arch”: 64 bits if you’re on a 64-bit architecture and 32 bits if you’re on a 32-bit architecture.
Note that number literals that can be multiple numeric types allow a type suffix, such as 57u8
, to designate the type. Number literals can also use _
as a visual separator to make the number easier to read, such as 1_000
, which will have the same value as if you had specified 1000
.
Number Literals |
Example |
Decimal |
98_222 |
Hex |
0xff |
Octal |
0o77 |
Binary |
0b1111_0000 |
Byte (u8 only) |
b'A' |
So how do you know which type of integer to use? If you’re unsure, Rust’s defaults are generally good places to start: integer types default to i32
. The primary situation in which you’d use isize
or usize
is when indexing some sort of collection.
Integer Overflow
If you try to change the variable to a value outside that range, such as 256u8
, integer overflow will occur, which can result in one of two behaviors:
When you’re compiling in debug mode, Rust includes checks for integer overflow that cause your program to panic at runtime if this behavior occurs. Rust uses the term panicking when a program exits with an error.
When you’re compiling in release mode with the --release
flag, Rust does not include checks for integer overflow that cause panics. Instead, if overflow occurs, Rust performs two’s complement wrapping. In short, values greater than the maximum value the type can hold “wrap around” to the minimum of the values the type can hold. In the case of a u8
, the value 256 becomes 0, the value 257 becomes 1, and so on. The program won’t panic, but the variable will have a value that probably isn’t what you were expecting it to have.
Relying on integer overflow’s wrapping behavior is considered an error.
To explicitly handle the possibility of overflow, you can use these families of methods provided by the standard library for primitive numeric types:
Wrap in all modes with the wrapping_*
methods, such as wrapping_add
.
Return the None
value if there is overflow with the checked_*
methods.
Return the value and a boolean indicating whether there was overflow with the overflowing_*
methods.
Saturate at the value’s minimum or maximum values with the saturating_*
methods.
Floating-Point Types
Rust’s floating-point types are f32
and f64
, which are 32 bits and 64 bits in size, respectively.
The default type is f64
because on modern CPUs, it’s roughly the same speed as f32
but is capable of more precision.
All floating-point types are signed.
Filename: src/main.rs
fn main() {
let x = 2.0;
let y: f32 = 3.0;
}
Floating-point numbers are represented according to the IEEE-754 standard. The f32
type is a single-precision float, and f64
has double precision.
Numeric Operations
Rust supports the basic mathematical operations you’d expect for all the number types: addition, subtraction, multiplication, division, and remainder.
Integer division truncates toward zero to the nearest integer.
Filename: src/main.rs
fn main() {
let sum = 5 + 10;
let difference = 95.5 - 4.3;
let product = 4 * 30;
let quotient = 56.7 / 32.2;
let truncated = -5 / 3;
let remainder = 43 % 5;
}
The Boolean Type
A Boolean type in Rust has two possible values: true
and false
.
Booleans are one byte in size.
The Boolean type in Rust is specified using bool
.
Filename: src/main.rs
fn main() {
let t = true;
let f: bool = false;
}
The Character Type
Rust’s char
type is the language’s most primitive alphabetic type.
Filename: src/main.rs
fn main() {
let c = 'z';
let z: char = 'ℤ';
let heart_eyed_cat = '😻';
}
Note that we specify char
literals with single quotes, as opposed to string literals, which use double quotes.
Rust’s char
type is four bytes in size and represents a Unicode Scalar Value, which means it can represent a lot more than just ASCII. Accented letters; Chinese, Japanese, and Korean characters; emoji; and zero-width spaces are all valid char
values in Rust. Unicode Scalar Values range from U+0000
to U+D7FF
and U+E000
to U+10FFFF
inclusive.
However, a “character” isn’t really a concept in Unicode, so your human intuition for what a “character” is may not match up with what a char
is in Rust.
Compound Types
Compound types can group multiple values into one type. Rust has two primitive compound types: tuples and arrays.
The Tuple Type
Tuples have a fixed length: once declared, they cannot grow or shrink in size.
Each position in the tuple has a type, and the types of the different values in the tuple don’t have to be the same.
We’ve added optional type annotations in this example:
Filename: src/main.rs
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
}
To get the individual values out of a tuple, we can use pattern matching to destructure a tuple value(即解构赋值), like this:
Filename: src/main.rs
fn main() {
let tup = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of y is: {y}");
}
We can also access a tuple element directly by using a period (.
) followed by the index of the value we want to access. For example:
Filename: src/main.rs
fn main() {
let x: (i32, f64, u8) = (500, 6.4, 1);
let five_hundred = x.0;
let six_point_four = x.1;
let one = x.2;
}
The tuple without any values has a special name, unit. This value and its corresponding type are both written ()
and represent an empty value or an empty return type. Expressions implicitly return the unit value if they don’t return any other value.
The Array Type
Unlike a tuple, every element of an array must have the same type. Unlike arrays in some other languages, arrays in Rust have a fixed length.
Filename: src/main.rs
fn main() {
let a = [1, 2, 3, 4, 5];
}
Arrays are useful when you want your data allocated on the stack rather than the heap or when you want to ensure you always have a fixed number of elements.
An array isn’t as flexible as the vector type, though. A vector is a similar collection type provided by the standard library that is allowed to grow or shrink in size. If you’re unsure whether to use an array or a vector, chances are you should use a vector.
Write an array’s type using square brackets with the type of each element, a semicolon, and then the number of elements in the array, like so:
let a: [i32; 5] = [1, 2, 3, 4, 5];
You can also initialize an array to contain the same value for each element by specifying the initial value, followed by a semicolon, and then the length of the array in square brackets, as shown here:
let a = [3; 5];
Accessing Array Elements
An array is a single chunk of memory of a known, fixed size that can be allocated on the stack. You can access elements of an array using indexing, like this:
Filename: src/main.rs
fn main() {
let a = [1, 2, 3, 4, 5];
let first = a[0];
let second = a[1];
}
Invalid Array Element Access
Let’s see what happens if you try to access an element of an array that is past the end of the array. Say you run this code to get an array index from the user:
Filename: src/main.rs
use std::io;
fn main() {
let a = [1, 2, 3, 4, 5];
println!("Please enter an array index.");
let mut index = String::new();
io::stdin()
.read_line(&mut index)
.expect("Failed to read line");
let index: usize = index
.trim()
.parse()
.expect("Index entered was not a number");
let element = a[index];
println!("The value of the element at index {index} is: {element}");
}
This code compiles successfully. The program resulted in a runtime error at the point of using an invalid value in the indexing operation. The program exited with an error message and didn’t execute the final println! statement.
When you attempt to access an element using indexing, Rust will check that the index you’ve specified is less than the array length. If the index is greater than or equal to the length, Rust will panic. This check has to happen at runtime, especially in this case, because the compiler can’t possibly know what value a user will enter when they run the code later.
Functions
Rust code uses snake case as the conventional style for function and variable names, in which all letters are lowercase and underscores separate words. Here’s a program that contains an example function definition:
Filename: src/main.rs
fn main() {
println!("Hello, world!");
another_function();
}
fn another_function() {
println!("Another function.");
}
Rust doesn’t care where you define your functions, only that they’re defined somewhere in a scope that can be seen by the caller.
Parameters
Technically, the concrete values are called arguments, but in casual conversation, people tend to use the words parameter and argument interchangeably for either the variables in a function’s definition or the concrete values passed in when you call a function.
Filename: src/main.rs
fn main() {
print_labeled_measurement(5, 'h');
}
fn print_labeled_measurement(value: i32, unit_label: char) {
println!("The measurement is: {value}{unit_label}");
}
In function signatures, you must declare the type of each parameter.
Statements and Expressions
Function bodies are made up of a series of statements optionally ending in an expression.
Creating a variable and assigning a value to it with the let
keyword is a statement. Function definitions are also statements.
Filename: src/main.rs
fn main() {
let y = 6;
}
Statements do not return values. Therefore, you can’t assign a let statement to another variable, as the following code tries to do; you’ll get an error:
Filename: src/main.rs
fn main() {
let x = (let y = 6);
}
Expressions can be part of statements. The 6
in the statement let y = 6;
is an expression that evaluates to the value 6
.
Calling a function is an expression. Calling a macro is an expression. A new scope block created with curly brackets is an expression, for example:
Filename: src/main.rs
fn main() {
let y = {
let x = 3;
x + 1
};
println!("The value of y is: {y}");
}
Expressions do not include ending semicolons. If you add a semicolon to the end of an expression, you turn it into a statement, and it will then not return a value.
Functions with Return Values
Functions can return values to the code that calls them. We don’t name return values, but we must declare their type after an arrow (->
).
In Rust, the return value of the function is synonymous with the value of the final expression in the block of the body of a function.
You can return early from a function by using the return
keyword and specifying a value, but most functions return the last expression implicitly. Here’s an example of a function that returns a value:
Filename: src/main.rs
fn five() -> i32 {
5
}
fn main() {
let x = five();
println!("The value of x is: {x}");
}
But if we place a semicolon at the end of the line containing 5
, changing it from an expression to a statement, we’ll get an error:
Filename: src/main.rs
fn five() -> i32 {
5;
}
fn main() {
let x = five();
println!("The value of x is: {x}");
}
The definition of the function five
says that it will return an i32
, but statements don’t evaluate to a value, which is expressed by ()
, the unit type. Therefore, nothing is returned, which contradicts the function definition and results in an error.
In Rust, the idiomatic comment style starts a comment with two slashes, and the comment continues until the end of the line.
For comments that extend beyond a single line, you’ll need to include //
on each line, like this:
Comments can also be placed at the end of lines containing code:
Filename: src/main.rs
fn main() {
let lucky_number = 7;
}
Rust also has another kind of comment, documentation comments.
Control Flow
if Expressions
An if
expression allows you to branch your code depending on conditions.
Filename: src/main.rs
fn main() {
let number = 3;
if number < 5 {
println!("condition was true");
} else {
println!("condition was false");
}
}
It’s also worth noting that the condition must be a bool
. If the condition isn’t a bool
, we’ll get an error. For example, try running the following code:
Filename: src/main.rs
fn main() {
let number = 3;
if number {
println!("number was three");
}
}
Unlike languages such as Ruby and JavaScript, Rust will not automatically try to convert non-Boolean types to a Boolean. You must be explicit and always provide if
with a Boolean as its condition.
Handling Multiple Conditions with else if
You can use multiple conditions by combining if
and else
in an else if
expression. For example:
Filename: src/main.rs
fn main() {
let number = 6;
if number % 4 == 0 {
println!("number is divisible by 4");
} else if number % 3 == 0 {
println!("number is divisible by 3");
} else if number % 2 == 0 {
println!("number is divisible by 2");
} else {
println!("number is not divisible by 4, 3, or 2");
}
}
When this program executes, it checks each if
expression in turn and executes the first body for which the condition evaluates to true
.
Note that even though 6 is divisible by 2, we don’t see the output number is divisible by 2
, nor do we see the number is not divisible by 4, 3, or 2
text from the else
block. That’s because Rust only executes the block for the first true
condition, and once it finds one, it doesn’t even check the rest.
Using too many else if
expressions can clutter your code, so if you have more than one, you might want to refactor your code by match
.
Using if in a let Statement
Because if
is an expression, we can use it on the right side of a let
statement to assign the outcome to a variable.
Filename: src/main.rs
fn main() {
let condition = true;
let number = if condition { 5 } else { 6 };
println!("The value of number is: {number}");
}
The values that have the potential to be results from each arm of the if
must be the same type. If the types are mismatched, as in the following example, we’ll get an error:
Filename: src/main.rs
fn main() {
let condition = true;
let number = if condition { 5 } else { "six" };
println!("The value of number is: {number}");
}
Repetition with Loops
Rust has three kinds of loops: loop
, while
, and for
.
Repeating Code with loop
The loop
keyword tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop.
Filename: src/main.rs
fn main() {
loop {
println!("again!");
}
}
You can place the break
keyword within the loop to tell the program when to stop executing the loop.
We can also use continue
in a loop, which tells the program to skip over any remaining code in this iteration of the loop and go to the next iteration.
Returning Values from Loops
You might also need to pass the result of that operation out of the loop to the rest of your code. To do this, you can add the value you want returned after the break
expression you use to stop the loop; that value will be returned out of the loop so you can use it, as shown here:
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("The result is {result}");
}
You can also return
from inside a loop. While break
only exits the current loop, return
always exits the current function.
Loop Labels to Disambiguate Between Multiple Loops
If you have loops within loops, break
and continue
apply to the innermost loop at that point. You can optionally specify a loop label on a loop that you can then use with break
or continue
to specify that those keywords apply to the labeled loop instead of the innermost loop. Loop labels must begin with a single quote. Here’s an example with two nested loops:
fn main() {
let mut count = 0;
'counting_up: loop {
println!("count = {count}");
let mut remaining = 10;
loop {
println!("remaining = {remaining}");
if remaining == 9 {
break;
}
if count == 2 {
break 'counting_up;
}
remaining -= 1;
}
count += 1;
}
println!("End count = {count}");
}
Conditional Loops with while
A program will often need to evaluate a condition within a loop. While the condition is true
, the loop runs. When the condition ceases to be true
, the program calls break
, stopping the loop. However, this pattern is so common that Rust has a built-in language construct for it, called a while
loop.
Filename: src/main.rs
fn main() {
let mut number = 3;
while number != 0 {
println!("{number}!");
number -= 1;
}
println!("LIFTOFF!!!");
}
Looping Through a Collection with for
You can use a for
loop and execute some code for each item in a collection.
Filename: src/main.rs
fn main() {
let a = [10, 20, 30, 40, 50];
for element in a {
println!("the value is: {element}");
}
}
Range
can also be used in for
loops.
Filename: src/main.rs
fn main() {
for number in (1..4).rev() {
println!("{number}!");
}
println!("LIFTOFF!!!");
}
Understanding Ownership
Ownership enables Rust to make memory safety guarantees without needing a garbage collector, so it’s important to understand how ownership works.
What Is Ownership?
Ownership is a set of rules that govern how a Rust program manages memory.
Memory is managed through a system of ownership with a set of rules that the compiler checks. If any of the rules are violated, the program won’t compile. None of the features of ownership will slow down your program while it’s running.
The Stack and the Heap
Stack
The stack stores values in the order it gets them and removes the values in the opposite order. This is referred to as last in, first out.
Adding data is called pushing onto the stack, and removing data is called popping off the stack.
All data stored on the stack must have a known, fixed size. Data with an unknown size at compile time or a size that might change must be stored on the heap instead.
Heap
The heap is less organized: when you put data on the heap, you request a certain amount of space. The memory allocator finds an empty spot in the heap that is big enough, marks it as being in use, and returns a pointer, which is the address of that location. This process is called allocating on the heap and is sometimes abbreviated as just allocating (pushing values onto the stack is not considered allocating).
Because the pointer to the heap is a known, fixed size, you can store the pointer on the stack, but when you want the actual data, you must follow the pointer.
comparison
Pushing to the stack is faster than allocating on the heap because the allocator never has to search for a place to store new data; that location is always at the top of the stack. Comparatively, allocating space on the heap requires more work because the allocator must first find a big enough space to hold the data and then perform bookkeeping to prepare for the next allocation.
Accessing data in the heap is slower than accessing data on the stack because you have to follow a pointer to get there. Contemporary processors are faster if they jump around less in memory. A processor can do its job better if it works on data that’s close to other data (as it is on the stack) rather than farther away (as it can be on the heap).
When your code calls a function, the values passed into the function (including, potentially, pointers to data on the heap) and the function’s local variables (just the pointers and references, not the data) get pushed onto the stack. When the function is over, those values get popped off the stack.
Ownership Rules
Each value in Rust has an owner.
There can only be one owner at a time.
When the owner goes out of scope, the value will be dropped.
Variable Scope
A variable and the scope in which it is valid.
{
let s = "hello";
}
The String Type
String type manages data allocated on the heap and as such is able to store an amount of text that is unknown to us at compile time.
You can create a String
from a string literal using the from
function, like so:
let s = String::from("hello");
The double colon ::
operator allows us to namespace this particular from
function under the String
type rather than using some sort of name like string_from
.
This kind of string can be mutated:
let mut s = String::from("hello");
s.push_str(", world!");
println!("{}", s);
Memory and Allocation
With the String
type, in order to support a mutable, growable piece of text, we need to allocate an amount of memory on the heap, unknown at compile time, to hold the contents. This means:
The memory is automatically returned once the variable that owns it goes out of scope. Here’s a version of our scope example using a String
instead of a string literal:
{
let s = String::from("hello");
}
There is a natural point at which we can return the memory our String
needs to the allocator: when s
goes out of scope. When a variable goes out of scope, Rust calls a special function for us. This function is called drop
, and it’s where the author of String
can put the code to return the memory. Rust calls drop
automatically at the closing curly bracket.
Variables and Data Interacting with Move
Multiple variables can interact with the same data in different ways in Rust. Let’s look at an example using an integer in the following listing.
let x = 5;
let y = x;
We can probably guess what this is doing: “bind the value 5
to x
; then make a copy of the value in x
and bind it to y
.” We now have two variables, x
and y
, and both equal 5
. This is indeed what is happening, because integers are simple values with a known, fixed size, and these two 5
values are pushed onto the stack.
Now let’s look at the String
version:
let s1 = String::from("hello");
let s2 = s1;
s1
on the stack (not valid after assigning to s2
)
name |
value |
ptr |
(s1 on the heap) |
len |
5 |
capacity |
5 |
s1
on the heap
index |
value |
0 |
h |
1 |
e |
2 |
l |
3 |
l |
4 |
o |
s2
on the stack
name |
value |
ptr |
(s1 on the heap) |
len |
5 |
capacity |
5 |
The length is how much memory, in bytes, the contents of the String
are currently using. The capacity is the total amount of memory, in bytes, that the String
has received from the allocator. The difference between length and capacity matters, but not in this context, so for now, it’s fine to ignore the capacity.
If you’ve heard the terms shallow copy and deep copy while working with other languages, the concept of copying the pointer, length, and capacity without copying the data probably sounds like making a shallow copy. But because Rust also invalidates the first variable, instead of being called a shallow copy, it’s known as a move.
Move helps us to solve the double free error: When a variable goes out of scope, Rust automatically calls the drop
function and cleans up the heap memory for that variable. Freeing memory twice can lead to memory corruption. After the line let s2 = s1
, Rust considers s1
as no longer valid. Therefore, Rust doesn’t need to free anything when s1
goes out of scope.
Check out what happens when you try to use s1
after s2
is created; it won’t work:
let s1 = String::from("hello");
let s2 = s1;
println!("{}, world!", s1);
Rust will never automatically create “deep” copies of your data. Therefore, any automatic copying can be assumed to be inexpensive in terms of runtime performance.
Scope and Assignment
When you assign a completely new value to an existing variable, Rust will call drop
and free the original value’s memory immediately.
let mut s = String::from("hello");
s = String::from("ahoy"); // When nothing is referring to the original value `hello` on the heap at all, the original string `hello` thus immediately goes out of scope. Rust will run the `drop` function on it and its memory will be freed right away.
println!("{s}, world!");
s
on the stack
name |
value |
ptr |
(s on the heap) |
len |
4 |
capacity |
4 |
s
on the heap
index |
value |
0 |
a |
1 |
h |
2 |
o |
3 |
y |
s
on the heap (dropped after assigned “ahoy”)
index |
value |
0 |
h |
1 |
e |
2 |
l |
3 |
l |
4 |
o |
Variables and Data Interacting with Clone
If we do want to deeply copy the heap data of the String
, not just the stack data, we can use a common method called clone
. Here’s an example of the clone method in action:
let s1 = String::from("hello");
let s2 = s1.clone();
println!("s1 = {}, s2 = {}", s1, s2);
When you see a call to clone
, you know that some arbitrary code is being executed and that code may be expensive. It’s a visual indicator that something different is going on.
Stack-Only Data: Copy
This code using integers works and is valid:
let x = 5;
let y = x;
println!("x = {}, y = {}", x, y);
But this code seems to contradict what we just learned: we don’t have a call to clone
, but x
is still valid and wasn’t moved into y
.
The reason is that types such as integers that have a known size at compile time are stored entirely on the stack, so copies of the actual values are quick to make. There’s no difference between deep and shallow copying here.
Rust has a special annotation called the Copy
trait that we can place on types that are stored on the stack. If a type implements the Copy
trait, variables that use it do not move, but rather are trivially copied, making them still valid after assignment to another variable.
Rust won’t let us annotate a type with Copy
if the type, or any of its parts, has implemented the Drop
trait. If the type needs something special to happen when the value goes out of scope and we add the Copy
annotation to that type, we’ll get a compile-time error.
Drop
trait has no relationship with move mechanism. String
can move and has Drop
trait. But a struct only contains scalar type can move and doesn’t have Drop
trait.
use std::mem::needs_drop;
struct Point {
x: i32,
y: i32,
}
fn main() {
let p1 = Point { x: 1, y: 2 };
let p2 = p1;
println!("p2: ({}, {})", p2.x, p2.y);
println!("Point has Drop trait: {}", needs_drop::<Point>());
println!("String has Drop trait: {}", needs_drop::<String>());
}
As a general rule, any group of simple scalar values can implement Copy
, and nothing that requires allocation or is some form of resource can implement Copy
. Here are some of the types that implement Copy
:
All the integer types, such as u32
.
The Boolean type, bool
, with values true
and false
.
All the floating-point types, such as f64
.
The character type, char
.
Tuples, if they only contain types that also implement Copy
. For example, (i32, i32)
implements Copy
, but (i32, String)
does not.
Drop
trait and drop mechanism
Why scalar types like u32
doesn’t implement Drop
trait but still be dropped when out of scope?
Because scalar types deallocate memory by the drop mechanism. The Drop
trait just adds custom cleanup logic to the existing drop mechanism.
Drop
trait: Provides a way to define extra custom cleanup logic for a type. When a type implementing Drop
trait goes out of scope, its drop method is called.
Drop mechanism: The overarching system that ensures memory and resources are properly cleaned up when values go out of scope. Known as drop
method (not the drop
method on Drop
trait).
Ownership and Functions
Passing a variable to a function will move or copy, just as assignment does. The following listing has an example with some annotations showing where variables go into and out of scope.
Filename: src/main.rs
fn main() {
let s = String::from("hello");
takes_ownership(s);
let x = 5;
makes_copy(x);
}
fn takes_ownership(some_string: String) {
println!("{}", some_string);
}
fn makes_copy(some_integer: i32) {
println!("{}", some_integer);
}
Return Values and Scope
Returning values can also transfer ownership. The following listing shows an example of a function that returns some value.
Filename: src/main.rs
fn main() {
let s1 = gives_ownership();
let s2 = String::from("hello");
let s3 = takes_and_gives_back(s2);
}
fn gives_ownership() -> String {
let some_string = String::from("yours");
some_string
}
fn takes_and_gives_back(a_string: String) -> String {
a_string
}
The ownership of a variable follows the same pattern every time: assigning a value to another variable moves it. When a variable that includes data on the heap goes out of scope, the value will be cleaned up by drop
unless ownership of the data has been moved to another variable.
References and Borrowing
A reference is like a pointer in that it’s an address we can follow to access the data stored at that address; that data is owned by some other variable. Unlike a pointer, a reference is guaranteed to point to a valid value of a particular type for the life of that reference.
Here is how you would define and use a calculate_length
function that has a reference to an object as a parameter instead of taking ownership of the value:
Filename: src/main.rs
fn main() {
let s1 = String::from("hello");
let len = calculate_length(&s1);
println!("The length of '{}' is {}.", s1, len);
}
fn calculate_length(s: &String) -> usize {
s.len()
}
These ampersands(&
) represent references, and they allow you to refer to some value without taking ownership of it.
s on the stack
s1 on the stack
name |
value |
ptr |
(s1 on the heap) |
len |
5 |
capacity |
5 |
s1 on the heap
index |
value |
0 |
h |
1 |
e |
2 |
l |
3 |
l |
4 |
o |
Note: The opposite of referencing by using &
is dereferencing, which is accomplished with the dereference operator, *
.
When functions have references as parameters instead of the actual values, we won’t need to return the values in order to give back ownership, because we never had ownership, the value it points to will not be dropped when the reference stops being used.
We call the action of creating a reference borrowing.
Just as variables are immutable by default, so are references. We’re not allowed to modify something we have a reference to.
Mutable References
Filename: src/main.rs
fn main() {
let mut s = String::from("hello");
change(&mut s);
}
fn change(some_string: &mut String) {
some_string.push_str(", world");
}
We create a mutable reference with &mut
.
Mutable references have one big restriction: if you have a mutable reference to a value, you can have no other references to that value.
The benefit of having this restriction is that Rust can prevent data races at compile time.
A data race is similar to a race condition and happens when these three behaviors occur:
- Two or more pointers access the same data at the same time.
- At least one of the pointers is being used to write to the data.
- There’s no mechanism being used to synchronize access to the data.
Data races cause undefined behavior and can be difficult to diagnose and fix when you’re trying to track them down at runtime; Rust prevents this problem by refusing to compile code with data races.
As always, we can use curly brackets to create a new scope, allowing for multiple mutable references, just not simultaneous ones:
let mut s = String::from("hello");
{
let r1 = &mut s;
}
let r2 = &mut s;
We also cannot have a mutable reference while we have an immutable one to the same value.
let mut s = String::from("hello");
let r1 = &s;
let r2 = &s;
let r3 = &mut s;
println!("{}, {}, and {}", r1, r2, r3);
Users of an immutable reference don’t expect the value to suddenly change out from under them! However, multiple immutable references are allowed because no one who is just reading the data has the ability to affect anyone else’s reading of the data.
Note that a reference’s scope starts from where it is introduced and continues through the last time that reference is used. For instance, this code will compile because the last usage of the immutable references, the println!
, occurs before the mutable reference is introduced:
let mut s = String::from("hello");
let r1 = &s;
let r2 = &s;
println!("{} and {}", r1, r2);
let r3 = &mut s;
println!("{}", r3);
Dangling References
In languages with pointers, it’s easy to erroneously create a dangling pointer - a pointer that references a location in memory that may have been given to someone else - by freeing some memory while preserving a pointer to that memory.
In Rust, by contrast, the compiler guarantees that references will never be dangling references: if you have a reference to some data, the compiler will ensure that the data will not go out of scope before the reference to the data does.
Let’s try to create a dangling reference to see how Rust prevents them with a compile-time error:
Filename: src/main.rs
fn main() {
let reference_to_nothing = dangle();
}
fn dangle() -> &String {
let s = String::from("hello");
&s
}
Because s
is created inside dangle
, when the code of dangle
is finished, s
will be deallocated. But we tried to return a reference to it. That means this reference would be pointing to an invalid String
. That’s no good! Rust won’t let us do this.
The Rules of References
At any given time, you can have either one mutable reference or any number of immutable references.
References must always be valid.
The Slice Type
Slices let you reference a contiguous sequence of elements in a collection rather than the whole collection. A slice is a kind of reference, so it does not have ownership.
Here’s a small programming problem: write a function that takes a string of words separated by spaces and returns the first word it finds in that string.
Filename: src/main.rs
fn first_word(s: &String) -> usize {
for (i, &item) in s.as_bytes().iter().enumerate() {
if item == b' ' {
return i;
}
}
s.len()
}
let mut s = String::from("hello world");
let word = first_word(&s);
s.clear();
as_bytes
converts String
to an array of bytes.
iter
is a method that returns each element in a collection.
enumerate
wraps the result of iter
and returns each element as part of a tuple instead. The first element of the tuple returned from enumerate
is the index, and the second element is a reference to the element.
String Slices
A string slice is a reference to part of a String
.
let s = String::from("hello world");
let hello = &s[0..5];
let world = &s[6..11];
We create slices using a range within brackets by specifying [starting_index..ending_index]
, where starting_index
is the first position in the slice and ending_index
is one more than the last position in the slice. Internally, the slice data structure stores the starting position and the length of the slice, which corresponds to ending_index
minus starting_index
.
s
name |
value |
ptr |
(s on the heap) at index 0 |
len |
11 |
capacity |
11 |
world
name |
value |
ptr |
(s on the heap) at index 6 |
len |
5 |
s
on the heap
index |
value |
0 |
h |
1 |
e |
2 |
l |
3 |
l |
4 |
o |
5 |
|
6 |
w |
7 |
o |
8 |
r |
9 |
l |
10 |
d |
With Rust’s ..
range syntax, if you want to start at index 0, you can drop the value before the two periods. In other words, these are equal:
let s = String::from("hello");
let slice = &s[0..2];
let slice = &s[..2];
By the same token, if your slice includes the last byte of the String
, you can drop the trailing number. That means these are equal:
let s = String::from("hello");
let len = s.len();
let slice = &s[3..len];
let slice = &s[3..];
You can also drop both values to take a slice of the entire string. So these are equal:
let s = String::from("hello");
let len = s.len();
let slice = &s[0..len];
let slice = &s[..];
Note: String slice range indices must occur at valid UTF-8 character boundaries. If you attempt to create a string slice in the middle of a multibyte character, your program will exit with an error.
With all this information in mind, let’s rewrite first_word
to return a slice. The type that signifies “string slice” is written as &str
:
Filename: src/main.rs
fn first_word(s: &String) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
Remember the bug in the program in the previous listing, when we got the index to the end of the first word but then cleared the string so our index was invalid? That code was logically incorrect but didn’t show any immediate errors. The problems would show up later if we kept trying to use the first word index with an emptied string. Slices make this bug impossible and let us know we have a problem with our code much sooner. Using the slice version of first_word will throw a compile-time error:
Filename: src/main.rs
fn main() {
let mut s = String::from("hello world");
let word = first_word(&s);
s.clear();
println!("the first word is: {}", word);
}
Recall from the borrowing rules that if we have an immutable reference to something, we cannot also take a mutable reference. Because clear
needs to truncate the String
, it needs to get a mutable reference. The println!
after the call to clear
uses the reference in word
, so the immutable reference must still be active at that point.
String Literals as Slices
String literals being stored inside the binary.
let s = "Hello, world!";
The type of s
here is &str
: it’s a slice pointing to that specific point of the binary. This is also why string literals are immutable; &str
is an immutable reference.
String Slices as Parameters
Using &str
instead of &String
as parameters allows us to use the same function on both &String
values and &str
values. This flexibility takes advantage of deref coercions.
fn first_word(s: &String) -> &str {
fn first_word(s: &str) -> &str {
Defining a function to take a string slice instead of a reference to a String
makes our API more general and useful without losing any functionality:
Filename: src/main.rs
fn main() {
let my_string = String::from("hello world");
let word = first_word(&my_string[0..6]);
let word = first_word(&my_string[..]);
let word = first_word(&my_string);
let my_string_literal = "hello world";
let word = first_word(&my_string_literal[0..6]);
let word = first_word(&my_string_literal[..]);
let word = first_word(my_string_literal);
}
Other Slices
let a = [1, 2, 3, 4, 5];
let slice = &a[1..3];
assert_eq!(slice, &[2, 3]);
This slice has the type &[i32]
. It works the same way as string slices do, by storing a reference to the first element and a length. You’ll use this kind of slice for all sorts of other collections.
A struct, or structure, is a custom data type that lets you package together and name multiple related values that make up a meaningful group.
Defining and Instantiating Struct
For example, the following listing shows a struct that stores information about a user account:
Filename: src/main.rs
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
For example, we can declare a particular user as shown in the following listing:
Filename: src/main.rs
fn main() {
let user1 = User {
active: true,
username: String::from("someusername123"),
email: String::from("[email protected]"),
sign_in_count: 1,
};
}
To get a specific value from a struct, we use dot notation. If the instance is mutable, we can change a value by using the dot notation and assigning into a particular field.
Filename: src/main.rs
fn main() {
let mut user1 = User {
active: true,
username: String::from("someusername123"),
email: String::from("[email protected]"),
sign_in_count: 1,
};
user1.email = String::from("[email protected]");
}
Note that the entire instance must be mutable; Rust doesn’t allow us to mark only certain fields as mutable.
As with any expression, we can construct a new instance of the struct as the last expression in the function body to implicitly return that new instance.
Filename: src/main.rs
fn build_user(email: String, username: String) -> User {
User {
active: true,
username: username,
email: email,
sign_in_count: 1,
}
}
Using the Field Init Shorthand
Because the parameter names and the struct field names are exactly the same in previous listing, we can use the field init shorthand syntax to rewrite build_user
so it behaves exactly the same but doesn’t have the repetition of username
and email
, as shown in the following listing:
Filename: src/main.rs
fn build_user(email: String, username: String) -> User {
User {
active: true,
username,
email,
sign_in_count: 1,
}
}
Creating Instances from Other Instances with Struct Update Syntax
The syntax ..
specifies that the remaining fields not explicitly set should have the same value as the fields in the given instance.
Filename: src/main.rs
fn main() {
let user2 = User {
email: String::from("[email protected]"),
..user1
};
}
The syntax ..user1
must come last to specify that any remaining fields should get their values from the corresponding fields in user1
, but we can choose to specify values for as many fields as we want in any order, regardless of the order of the fields in the struct’s definition.
Note that the struct update syntax uses =
like an assignment; this is because it moves the data. In this example, we can no longer use user1
as a whole after creating user2
because the String
in the username
field of user1
was moved into user2
.
If we had given user2
new String
values for both email
and username
, and thus only used the active
and sign_in_count
values from user1
, then user1
would still be valid after creating user2
. Both active
and sign_in_count
are types that implement the Copy
trait, so types that implement the Copy
trait (stack-only data), wouldn’t be moved and still be valid after creating new instance by struct update syntax.
Using Tuple Structs Without Named Fields to Create Different Types
Rust also supports structs that look similar to tuples, called tuple structs. 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 a different type from other tuples, and when naming each field as in a regular struct would be verbose or redundant.
For example, here we define and use two tuple structs named Color
and Point
:
Filename: src/main.rs
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
fn main() {
let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);
}
Note that the black
and origin
values are different types because they’re instances of different tuple structs. Each struct you define is its own type, even though the fields within the struct might have the same types.
Otherwise, tuple struct instances are similar to tuples in that you can destructure them into their individual pieces, and you can use a .
followed by the index to access an individual value.
Unlike tuples, tuple structs require you to name the the type of the struct when you destructure them. For example, we would write let Point(x, y, z) = point
.
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 ()
. Unit-like structs can be useful when you need to implement a trait on some type but don’t have any data that you want to store in the type itself.
Here’s an example of declaring and instantiating a unit struct named AlwaysEqual
:
Filename: src/main.rs
struct AlwaysEqual;
fn main() {
let subject = AlwaysEqual;
}
An Example Program Using Structs
To understand when we might want to use structs, let’s write a program that calculates the area of a rectangle.
Refactoring with Structs: Adding More Meaning
Filename: src/main.rs
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!(
"The area of the rectangle is {} square pixels.",
area(&rect1)
);
}
fn area(rectangle: &Rectangle) -> u32 {
rectangle.width * rectangle.height
}
Adding Useful Functionality with Derived Traits
It’d be useful to be able to print an instance of Rectangle
while we’re debugging our program and see the values for all its fields. The following listing tries using the println!
macro. This won’t work, however:
Filename: src/main.rs
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!("rect1 is {}", rect1);
}
The println!
macro can do many kinds of formatting, and by default, the curly brackets tell println!
to use formatting known as Display
: output intended for direct end user consumption.
The primitive types we’ve seen so far implement Display
by default because there’s only one way you’d want to show a 1
or any other primitive type to a user. But with structs, the way println!
should format the output is less clear because there are more display possibilities: Do you want commas or not? Do you want to print the curly brackets? Should all the fields be shown? Due to this ambiguity, Rust doesn’t try to guess what we want, and structs don’t have a provided implementation of Display
to use with println!
and the {}
placeholder.
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.
Rust does include functionality to print out debugging information, but we have to explicitly opt in to make that functionality available for our struct. To do that, we add the outer attribute #[derive(Debug)]
just before the struct definition as shown in the following list.
Filename: src/main.rs
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!("rect1 is {:?}", rect1);
}
When we have larger structs, it’s useful to have output that’s a bit easier to read; in those cases, we can use {:#?}
instead of {:?}
in the println!
string.
Using the {:?}
style will output the following:
rect1 is Rectangle { width: 30, height: 50 }
Using the {:#?}
style will output the following:
rect1 is Rectangle {
width: 30,
height: 50,
}
Another way to print out a value using the Debug
format is to use the dbg!
macro, which takes ownership of an expression (as opposed to println!
, which takes a reference), prints the file and line number of where that dbg!
macro call occurs in your code along with the resultant value of that expression, and returns ownership of the value.
Note: Calling the dbg!
macro prints to the standard error console stream (stderr
), as opposed to println!
, which prints to the standard output console stream (stdout
).
Here’s an example where we’re interested in the value that gets assigned to the width
field, as well as the value of the whole struct in rect1
:
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let scale = 2;
let rect1 = Rectangle {
width: dbg!(30 * scale),
height: 50,
};
dbg!(&rect1);
}
We can put dbg!
around the expression 30 * scale
and, because dbg!
returns ownership of the expression’s value, the width
field will get the same value as if we didn’t have the dbg!
call there. We don’t want dbg!
to take ownership of rect1
, so we use a reference to rect1
in the next call. Here’s what the output of this example looks like:
[src/main.rs:10:16] 30 * scale = 60
[src/main.rs:14:5] &rect1 = Rectangle {
width: 60,
height: 50,
}
Method Syntax
Unlike functions, methods are defined within the context of a struct (or an enum or a trait object), and their first parameter is always self
, which represents the instance of the struct the method is being called on.
Defining Methods
Let’s change the area
function that has a Rectangle
instance as a parameter and instead make an area
method defined on the Rectangle
struct, as shown in the following listing.
Filename: src/main.rs
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!(
"The area of the rectangle is {} square pixels.",
rect1.area()
);
}
The &self
is actually short for self: &Self
. Within an impl
block, the type Self
is an alias for the type that the impl
block is for.
Methods must have a parameter named self
of type Self
for their first parameter, so Rust lets you abbreviate this with only the name self
in the first parameter spot.
Note that we still need to use the &
in front of the self
shorthand to indicate that this method borrows the Self
instance, just as we did in rectangle: &Rectangle
: we don’t want to take ownership, and we just want to read the data in the struct, not write to it.
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.
Type |
Form |
Description |
reading |
&self |
just want to read the data in the struct, not write to it |
mutating |
&mut self |
want to change the instance |
consuming |
self |
rare; usually used when method transforms self into something else and want to prevent the caller from using the original instance after the transformation |
The main reason for using methods instead of functions, in addition to providing method syntax and not having to repeat the type of self
in every method’s signature, is for organization.
Note that we can choose to give a method the same name as one of the struct’s fields. For example, we can define a method on Rectangle
that is also named width
:
Filename: src/main.rs
impl Rectangle {
fn width(&self) -> bool {
self.width > 0
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
if rect1.width() {
println!("The rectangle has a nonzero width; it is {}", rect1.width);
}
}
We can use a field within a method of the same name for any purpose. In main, when we follow rect1.width
with parentheses, Rust knows we mean the method width
. When we don’t use parentheses, Rust knows we mean the field width
.
Often, but not always, when we give a method the same name as a field we want it to only return the value in the field and do nothing else. Methods like this are called getters, and Rust does not implement them automatically for struct fields as some other languages do. Getters are useful because you can make the field private but the method public, and thus enable read-only access to that field as part of the type’s public API.
Methods with More Parameters
Let’s practice using methods by implementing a second method on the Rectangle
struct.
Filename: src/main.rs
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
let rect2 = Rectangle {
width: 10,
height: 40,
};
let rect3 = Rectangle {
width: 60,
height: 45,
};
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
}
Methods can take multiple parameters that we add to the signature after the self
parameter, and those parameters work just like parameters in functions.
Associated Functions
All functions defined within an impl
block are called associated functions because they’re associated with the type named after the impl
.
We can define associated functions that don’t have self
as their first parameter (and thus are not methods) because they don’t need an instance of the type to work with. We’ve already used one function like this: the String::from
function that’s defined on the String
type.
Associated functions that aren’t methods are often used for constructors that will return a new instance of the struct. These are often called new
, but new
isn’t a special name and isn’t built into the language.
For example, we could choose to provide an associated function named square
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:
Filename: src/main.rs
impl Rectangle {
fn square(size: u32) -> Self {
Self {
width: size,
height: size,
}
}
}
let sq = Rectangle::square(3);
The Self
keywords in the return type and in the body of the function are aliases for the type that appears after the impl
keyword, which in this case is Rectangle
.
To call this associated function, we use the ::
syntax with the struct name. Associated 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. For example, the following listing is equivalent to the code shown in the previous listing, which has each method in its own impl
block.
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
impl Rectangle {
fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
}
Enums and Pattern Matching
Defining an Enum
Any IP address can be either a version four or a version six address, but not both at the same time. That property of IP addresses makes the enum data structure appropriate because an enum value can only be one of its variants.
We can express this concept in code by defining an IpAddrKind
enumeration and listing the possible kinds an IP address can be, V4
and V6
. These are the variants of the enum:
enum IpAddrKind {
V4,
V6,
}
IpAddrKind
is now a custom data type that we can use elsewhere in our code.
Enum Values
We can create instances of each of the two variants of IpAddrKind
like this:
let four = IpAddrKind::V4;
let six = IpAddrKind::V6;
Note that the variants of the enum are namespaced under its identifier, and we use a double colon to separate the two. This is useful because now both values IpAddrKind::V4
and IpAddrKind::V6
are of the same type: IpAddrKind
. We can then, for instance, define a function that takes any IpAddrKind
:
fn route(ip_kind: IpAddrKind) {}
And we can call this function with either variant:
route(IpAddrKind::V4);
route(IpAddrKind::V6);
Rather than an enum inside a struct, we can put data directly into each enum variant. This new definition of the IpAddr
enum says that both V4
and V6
variants will have associated String
values:
enum IpAddr {
V4(String),
V6(String),
}
let home = IpAddr::V4(String::from("127.0.0.1"));
let loopback = IpAddr::V6(String::from("::1"));
The name of each enum variant that we define also becomes a function that constructs an instance of the enum. That is, IpAddr::V4()
is a function call that takes a String
argument and returns an instance of the IpAddr
type.
There’s another advantage to using an enum rather than a struct: each variant can have different types and amounts of associated data.
enum IpAddr {
V4(u8, u8, u8, u8),
V6(String),
}
let home = IpAddr::V4(127, 0, 0, 1);
let loopback = IpAddr::V6(String::from("::1"));
Let’s look at how the standard library defines IpAddr
: it has the exact enum and variants that we’ve defined and used, but it embeds the address data inside the variants in the form of two different structs, which are defined differently for each variant:
struct Ipv4Addr {
}
struct Ipv6Addr {
}
enum IpAddr {
V4(Ipv4Addr),
V6(Ipv6Addr),
}
You can put any kind of data inside an enum variant: strings, numeric types, or structs, for example. You can even include another enum.
Let’s look at another example of an enum in the following listing: this one has a wide variety of types embedded in its variants.
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
There is one more similarity between enums and structs: just as we’re able to define methods on structs using impl
, we’re also able to define methods on enums. Here’s a method named call that we could define on our Message
enum:
impl Message {
fn call(&self) {
}
}
let m = Message::Write(String::from("hello"));
m.call();
The Option Enum and Its Advantages Over Null Values
Rust does not have nulls, but it does have an enum that can encode the concept of a value being present or absent. This enum is Option<T>
, and it is defined by the standard library as follows:
enum Option<T> {
None,
Some(T),
}
The Option<T>
enum is so useful that it’s even included in the prelude; you don’t need to bring it into scope explicitly. Its variants are also included in the prelude: you can use Some
and None
directly without the Option::
prefix.
<T>
means that the Some
variant of the Option
enum can hold one piece of data of any type, and that each concrete type that gets used in place of T
makes the overall Option<T>
type a different type. Here are some examples of using Option
values to hold number types and string types:
let some_number = Some(5);
let some_char = Some('e');
let absent_number: Option<i32> = None;
Option<T>
and T
(where T
can be any type) are different types, the compiler won’t let us use an Option<T>
value as if it were definitely a valid value.
You have to convert an Option<T>
to a T
before you can perform T
operations with it. Generally, this helps catch one of the most common issues with null: assuming that something isn’t null when it actually is.
Everywhere that a value has a type that isn’t an Option<T>
, you can safely assume that the value isn’t null. This was a deliberate design decision for Rust to limit null’s pervasiveness and increase the safety of Rust code.
In general, in order to use an Option<T>
value, you want to have code that will handle each variant. You want some code that will run only when you have a Some(T)
value, and this code is allowed to use the inner T
. You want some other code to run only if you have a None
value, and that code doesn’t have a T
value available.
The match
expression is a control flow construct that does just this when used with enums: it will run different code depending on which variant of the enum it has, and that code can use the data inside the matching value.
The Match Control Flow Construct
Rust has an extremely powerful control flow construct called match
that allows you to compare a value against a series of patterns and then execute code based on which pattern matches. Patterns can be made up of literal values, variable names, wildcards, and many other things.
When the match
expression executes, it compares the resultant value against the pattern of each arm, in order. If a pattern matches the value, the code associated with that pattern is executed. If that pattern doesn’t match the value, execution continues to the next arm. We can have as many arms as we need.
We can write a function that takes an unknown US coin and, in a similar way as the counting machine, determines which coin it is and returns its value in cents, as shown in following listing:
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => {
println!("Lucky penny!");
1
}
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
The code associated with each arm is an expression, and the resultant value of the expression in the matching arm is the value that gets returned for the entire match
expression.
Each arm is separated from the next with a comma.
We don’t typically use curly brackets if the match arm code is short. If you want to run multiple lines of code in a match arm, you must use curly brackets, and the comma following the arm is then optional.
Patterns That Bind to Values
Another useful feature of match arms is that they can bind to the parts of the values that match the pattern. This is how we can extract values out of enum variants.
As an example, let’s change one of our enum variants to hold data inside it. From 1999 through 2008, the United States minted quarters with different designs for each of the 50 states on one side. No other coins got state designs, so only quarters have this extra value. We can add this information to our enum
by changing the Quarter
variant to include a UsState
value stored inside it, which we’ve done in the following listing.
#[derive(Debug)]
enum UsState {
Alabama,
Alaska,
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
In the match expression for this code, we add a variable called state
to the pattern that matches values of the variant Coin::Quarter
. When a Coin::Quarter
matches, the state
variable will bind to the value of that quarter’s state. Then we can use state
in the code for that arm, like so:
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("State quarter from {:?}!", state);
25
}
}
}
Matching with Option<T>
Let’s say we want to write a function that takes an Option<i32>
and, if there’s a value inside, adds 1 to that value. If there isn’t a value inside, the function should return the None
value and not attempt to perform any operations.
This function is very easy to write, thanks to match
, and will look like the following listing.
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
}
let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);
Combining match
and enums is useful in many situations. You’ll see this pattern a lot in Rust code: match
against an enum, bind a variable to the data inside, and then execute code based on it.
Matches Are Exhaustive
There’s one other aspect of match
we need to discuss: the arms’ patterns must cover all possibilities. Consider this version of our plus_one
function, which has a bug and won’t compile:
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
Some(i) => Some(i + 1),
}
}
Rust knows that we didn’t cover every possible case, and even knows which pattern we forgot! Matches in Rust are exhaustive: we must exhaust every last possibility in order for the code to be valid. Especially in the case of Option<T>
, when Rust prevents us from forgetting to explicitly handle the None
case, it protects us from assuming that we have a value when we might have null.
Catch-all Patterns and the _ Placeholder
Using enums, we can also take special actions for a few particular values, but for all other values take one default action.
Imagine we’re implementing a game where, if you roll a 3 on a dice roll, your player doesn’t move, but instead gets a new fancy hat. If you roll a 7, your player loses a fancy hat. For all other values, your player moves that number of spaces on the game board. Here’s a match that implements that logic:
let dice_roll = 9;
match dice_roll {
3 => add_fancy_hat(),
7 => remove_fancy_hat(),
other => move_player(other),
}
fn add_fancy_hat() {}
fn remove_fancy_hat() {}
fn move_player(num_spaces: u8) {}
This code compiles, even though we haven’t listed all the possible values a u8
can have, because the last pattern will match all values not specifically listed. This catch-all pattern meets the requirement that match
must be exhaustive.
Note that we have to put the catch-all arm last because the patterns are evaluated in order. If we put the catch-all arm earlier, the other arms would never run, so Rust will warn us if we add arms after a catch-all.
Rust also has a pattern we can use when we want a catch-all but don’t want to use the value in the catch-all pattern: _
is a special pattern that matches any value and does not bind to that value. This tells Rust we aren’t going to use the value, so Rust won’t warn us about an unused variable.
We can also tell Rust explicitly that we don’t want to run any code. We can express that by using the unit value ()
(the empty tuple type) as the code that goes with the _
arm:
let dice_roll = 9;
match dice_roll {
3 => add_fancy_hat(),
7 => remove_fancy_hat(),
_ => (),
}
fn add_fancy_hat() {}
fn remove_fancy_hat() {}
Concise Control Flow with if let and let else
The if let
syntax lets you combine if
and let
into a less verbose way to handle values that match one pattern while ignoring the rest.
Consider the program in the following listing that matches on an Option<u8>
value in the config_max
variable but only wants to execute code if the value is the Some
variant.
let config_max = Some(3u8);
match config_max {
Some(max) => println!("The maximum is configured to be {}", max),
_ => (),
}
We don’t want to do anything with the None
value. To satisfy the match
expression, we have to add _ => ()
after processing just one variant, which is annoying boilerplate code to add.
Instead, we could write this in a shorter way using if let
. The following code behaves the same as the match
in the following listing:
let config_max = Some(3u8);
if let Some(max) = config_max {
println!("The maximum is configured to be {}", max);
}
Using if let
means less typing, less indentation, and less boilerplate code. However, you lose the exhaustive checking that match
enforces.
In other words, you can think of if let
as syntax sugar for a match
that runs code when the value matches one pattern and then ignores all other values.
We can include an else
with an if let
. The block of code that goes with the else
is the same as the block of code that would go with the _
case in the match
expression that is equivalent to the if let
and else
.
Recall the Coin
enum definition in the previous listing, where the Quarter
variant also held a UsState
value. If we wanted to count all non-quarter coins we see while also announcing the state of the quarters, we could do that with a match
expression, like this:
let mut count = 0;
match coin {
Coin::Quarter(state) => println!("State quarter from {:?}!", state),
_ => count += 1,
}
Or we could use an if let
and else
expression, like this:
let mut count = 0;
if let Coin::Quarter(state) = coin {
println!("State quarter from {:?}!", state);
} else {
count += 1;
}
Staying on the “happy path” with let else
One common pattern is to perform some computation when a value is present and return a default value otherwise.
Continuing on with our example of coins with a UsState
value, if we wanted to say something funny depending on how old the state on the quarter was, we might introduce a method on UsState
to check the age of a state, like so:
impl UsState {
fn existed_in(&self, year: u16) -> bool {
match self {
UsState::Alabama => year >= 1819,
UsState::Alaska => year >= 1959,
}
}
}
Then we might use if let
to match on the type of coin, introducing a state
variable:
fn describe_state_quarter(coin: Coin) -> Option<String> {
let state = if let Coin::Quarter(state) = coin {
state
} else {
return None;
};
if state.existed_in(1900) {
Some(format!("{state:?} is pretty old, for America!"))
} else {
Some(format!("{state:?} is relatively new."))
}
}
This is a bit annoying to follow in its own way, though! One branch of the if let
produces a value, and the other one returns from the function entirely.
The let
-else
syntax takes a pattern on the left side and an expression on the right, very similar to if let
, but it does not have an if
branch, only an else
branch. If the pattern matches, it will bind the value from the pattern in the outer scope. If the pattern does not match, the program will flow into the else
arm, which must return from the function.
In the following listing, you can see how previous listing looks when using let
-else
in place of if let
.
fn describe_state_quarter(coin: Coin) -> Option<String> {
let Coin::Quarter(state) = coin else {
return None;
};
if state.existed_in(1900) {
Some(format!("{state:?} is pretty old, for America!"))
} else {
Some(format!("{state:?} is relatively new."))
}
}
Managing Growing Projects with Packages, Crates, and Modules
As a project grows, you should organize code by splitting it into multiple modules and then multiple files. A package can contain multiple binary crates and optionally one library crate. As a package grows, you can extract parts into separate crates that become external dependencies.
The way you write code defines which parts are public for other code to use and which parts are private implementation details that you reserve the right to change.
A related concept is scope: the nested context in which code is written has a set of names that are defined as “in scope”. You can create scopes and change which names are in or out of scope. You can’t have two items with the same name in the same scope; tools are available to resolve name conflicts.
The Rust module system, include:
Packages: A Cargo feature that lets you build, test, and share crates.
Crates: A tree of modules that produces a library or executable.
Modules and use: Let you control the organization, scope, and privacy of paths.
Paths: A way of naming an item, such as a struct, function, or module.
Packages and Crates
A crate is the smallest amount of code that the Rust compiler considers at a time. Even if you run rustc
rather than cargo
and pass a single source code file, the compiler considers that file to be a crate. Crates can contain modules, and the modules may be defined in other files that get compiled with the crate.
A crate can come in one of two forms: a binary crate or a library crate.
Binary crates are programs you can compile to an executable that you can run, such as a command-line program or a server. Each must have a function called main
that defines what happens when the executable runs.
Library crates don’t have a main
function, and they don’t compile to an executable. Instead, they define functionality intended to be shared with multiple projects.
Most of the time when Rustaceans say “crate”, they mean library crate, and they use “crate” interchangeably with the general programming concept of a “library”.
The crate root is a source file that the Rust compiler starts from and makes up the root module of your crate.
A package is a bundle of one or more crates that provides a set of functionality. A package contains a Cargo.toml file that describes how to build those crates.
Cargo is actually a package that contains the binary crate for the command-line tool you’ve been using to build your code. The Cargo package also contains a library crate that the binary crate depends on. Other projects can depend on the Cargo library crate to use the same logic the Cargo command-line tool uses.
A package can contain as many binary crates as you like, but at most only one library crate. A package must contain at least one crate, whether that’s a library or binary crate.
Cargo follows a convention that src/main.rs is the crate root of a binary crate with the same name as the package. Likewise, Cargo knows that if the package directory contains src/lib.rs, the package contains a library crate with the same name as the package, and src/lib.rs is its crate root. Cargo passes the crate root files to rustc
to build the library or binary.
A package that only contains src/main.rs, meaning it only contains a binary crate which has the same name as the package. If a package contains src/main.rs and src/lib.rs, it has two crates: a binary and a library, both with the same name as the package. A package can have multiple binary crates by placing files in the src/bin directory: each file will be a separate binary crate.
Defining Modules to Control Scope and Privacy
Namely paths that allow you to name items; the use
keyword that brings a path into scope; and the pub
keyword to make items public.
Modules Cheat Sheet
Start from the crate root: When compiling a crate, the compiler first looks in the crate root file (usually src/lib.rs for a library crate or src/main.rs for a binary crate) for code to compile.
Declaring modules: In the crate root file, you can declare new modules; say, you declare a “garden” module with mod garden;
. The compiler will look for the module’s code in these places:
Inline, within curly brackets that replace the semicolon following mod garden
In the file src/garden.rs
In the file src/garden/mod.rs
Declaring submodules: In any file other than the crate root, you can declare submodules. For example, you might declare mod vegetables
; in src/garden.rs. The compiler will look for the submodule’s code within the directory named for the parent module in these places:
Inline, directly following mod vegetables
, within curly brackets instead of the semicolon
In the file src/garden/vegetables.rs
In the file src/garden/vegetables/mod.rs
Paths to code in modules: Once a module is part of your crate, you can refer to code in that module from anywhere else in that same crate, as long as the privacy rules allow, using the path to the code. For example, an Asparagus
type in the garden vegetables module would be found at crate::garden::vegetables::Asparagus
.
Private vs. public: Code within a module is private from its parent modules by default. To make a module public, declare it with pub mod
instead of mod
. To make items within a public module public as well, use pub before their declarations.
The use
keyword: Within a scope, the use
keyword creates shortcuts to items to reduce repetition of long paths. In any scope that can refer to crate::garden::vegetables::Asparagus
, you can create a shortcut with use crate::garden::vegetables::Asparagus
; and from then on you only need to write Asparagus
to make use of that type in the scope.
Here we create a binary crate named backyard
that illustrates these rules. The crate’s directory, also named backyard
, contains these files and directories:
backyard
├── Cargo.lock
├── Cargo.toml
└── src
├── garden
│ └── vegetables.rs
├── garden.rs
└── main.rs
The crate root file in this case is src/main.rs, and it contains:
Filename: src/main.rs
use crate::garden::vegetables::Asparagus;
pub mod garden;
fn main() {
let plant = Asparagus {};
println!("I'm growing {:?}!", plant);
}
The pub mod garden
; line tells the compiler to include the code it finds in src/garden.rs, which is:
Filename: src/garden.rs
pub mod vegetables;
Here, pub mod vegetables
; means the code in src/garden/vegetables.rs is included too. That code is:
#[derive(Debug)]
pub struct Asparagus {}
Modules also allow us to control the privacy of items, because code within a module is private by default. Private items are internal implementation details not available for outside use. We can choose to make modules and the items within them public, which exposes them to allow external code to use and depend on them.
As an example, let’s write a library crate that provides the functionality of a restaurant. We’ll define the signatures of functions but leave their bodies empty to concentrate on the organization of the code, rather than the implementation of a restaurant.
In the restaurant industry, some parts of a restaurant are referred to as front of house and others as back of house. Front of house is where customers are; this encompasses where the hosts seat customers, servers take orders and payment, and bartenders make drinks. Back of house is where the chefs and cooks work in the kitchen, dishwashers clean up, and managers do administrative work.
To structure our crate in this way, we can organize its functions into nested modules. Create a new library named restaurant
by running cargo new restaurant --lib
; then enter the following code into src/lib.rs to define some modules and function signatures. Here’s the front of house section:
Filename: src/lib.rs
mod front_of_house {
mod hosting {
fn add_to_waitlist() {}
fn seat_at_table() {}
}
mod serving {
fn take_order() {}
fn serve_order() {}
fn take_payment() {}
}
}
We define a module with the mod
keyword followed by the name of the module (in this case, front_of_house
). The body of the module then goes inside curly brackets. Inside modules, we can place other modules, as in this case with the modules hosting
and serving
. Modules can also hold definitions for other items, such as structs, enums, constants, traits, and functions.
Earlier, we mentioned that src/main.rs and src/lib.rs are called crate roots. The reason for their name is that the contents of either of these two files form a module named crate at the root of the crate’s module structure, known as the module tree.
The following listing shows the module tree for the structure in preceding listing.
crate
└── front_of_house
├── hosting
│ ├── add_to_waitlist
│ └── seat_at_table
└── serving
├── take_order
├── serve_order
└── take_payment
Notice that the entire module tree is rooted under the implicit module named crate.
Paths for Referring to an Item in the Module Tree
A path can take two forms:
An absolute path is the full path starting from a crate root; for code from an external crate, the absolute path begins with the crate name, and for code from the current crate, it starts with the literal crate
.
A relative path starts from the current module and uses self
, super
, or an identifier in the current module.
Both absolute and relative paths are followed by one or more identifiers separated by double colons (::
).
Filename: src/lib.rs
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
pub fn eat_at_restaurant() {
crate::front_of_house::hosting::add_to_waitlist();
front_of_house::hosting::add_to_waitlist();
}
Using the crate
name to start from the crate root is like using /
to start from the filesystem root in your shell.
Starting with a module name means that the path is relative.
Choosing whether to use a relative or absolute path is a decision you’ll make based on your project, and depends on whether you’re more likely to move item definition code separately from or together with the code that uses the item.
Our preference in general is to specify absolute paths because it’s more likely we’ll want to move code definitions and item calls independently of each other.
In Rust, all items (functions, methods, structs, enums, modules, and constants) are private to parent modules by default. If you want to make an item like a function or struct private, you put it in a module.
Items in a parent module can’t use the private items inside child modules, but items in child modules can use the items in their ancestor modules (by using the super
keyword). This is because child modules wrap and hide their implementation details, but the child modules can see the context in which they’re defined.
Rust chose to have the module system function this way so that hiding inner implementation details is the default. However, Rust does give you the option to expose inner parts of child modules’ code to outer ancestor modules by using the pub
keyword to make an item public.
Exposing Paths with the pub Keyword
The pub
keyword on a module only lets code in its ancestor modules refer to it, not access its inner code. Because modules are containers, there’s not much we can do by only making the module public; we need to go further and choose to make one or more of the items within the module public as well.
Best Practices for Packages with a Binary and a Library
The module tree should be defined in src/lib.rs. Then, any public items can be used in the binary crate by starting paths with the name of the package. The binary crate becomes a user of the library crate just like a completely external crate would use the library crate: it can only use the public API.
Starting Relative Paths with super
We can construct relative paths that begin in the parent module, rather than the current module or the crate root, by using super
at the start of the path. This is like starting a filesystem path with the ..
syntax.
Filename: src/lib.rs
fn deliver_order() {}
mod back_of_house {
fn fix_incorrect_order() {
cook_order();
super::deliver_order();
}
fn cook_order() {}
}
The fix_incorrect_order
function is in the back_of_house
module, so we can use super
to go to the parent module of back_of_house
, which in this case is crate
, the root. From there, we look for deliver_order and find it. Success!
We think the back_of_house
module and the deliver_order
function are likely to stay in the same relationship to each other and get moved together should we decide to reorganize the crate’s module tree. Therefore, we used super
so we’ll have fewer places to update code in the future if this code gets moved to a different module.
Making Structs and Enums Public
We can also use pub
to designate structs and enums as public, but there are a few details extra to the usage of pub
with structs and enums.
If we use pub
before a struct definition, we make the struct public, but the struct’s fields will still be private. We can make each field public or not on a case-by-case basis.
Filename: src/lib.rs
mod back_of_house {
pub struct Breakfast {
pub toast: String,
seasonal_fruit: String,
}
impl Breakfast {
pub fn summer(toast: &str) -> Breakfast {
Breakfast {
toast: String::from(toast),
seasonal_fruit: String::from("peaches"),
}
}
}
}
pub fn eat_at_restaurant() {
let mut meal = back_of_house::Breakfast::summer("Rye");
meal.toast = String::from("Wheat");
println!("I'd like {} toast please", meal.toast);
}
In contrast, if we make an enum public, all of its variants are then public. We only need the pub
before the enum
keyword.
Filename: src/lib.rs
mod back_of_house {
pub enum Appetizer {
Soup,
Salad,
}
}
pub fn eat_at_restaurant() {
let order1 = back_of_house::Appetizer::Soup;
let order2 = back_of_house::Appetizer::Salad;
}
Enums aren’t very useful unless their variants are public; it would be annoying to have to annotate all enum variants with pub
in every case, so the default for enum variants is to be public.
Structs are often useful without their fields being public, so struct fields follow the general rule of everything being private by default unless annotated with pub
.
Bringing Paths into Scope with the use Keyword
We can create a shortcut to a path with the use
keyword once, and then use the shorter name everywhere else in the scope.
Filename: src/lib.rs
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
}
Adding use
and a path in a scope is similar to creating a symbolic link in the filesystem. Paths brought into scope with use
also check privacy, like any other paths.
Note that use
only creates the shortcut for the particular scope in which the use
occurs. The following listing moves the eat_at_restaurant
function into a new child module named customer
, which is then a different scope than the use
statement, so the function body won’t compile:
Filename: src/lib.rs
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use crate::front_of_house::hosting;
mod customer {
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
}
}
To fix this problem, move the use
within the customer
module too, or reference the shortcut in the parent module with super::hosting
within the child customer
module.
Creating Idiomatic use Paths
Filename: src/lib.rs
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use crate::front_of_house::hosting::add_to_waitlist;
pub fn eat_at_restaurant() {
add_to_waitlist();
}
Although this listing accomplish the same task, the preceding one is the idiomatic way to bring a function into scope with use
. The code is unclear as to where add_to_waitlist
is defined.
Bringing the function’s parent module into scope with use
means we have to specify the parent module when calling the function. Specifying the parent module when calling the function makes it clear that the function isn’t locally defined while still minimizing repetition of the full path.
On the other hand, when bringing in structs, enums, and other items with use
, it’s idiomatic to specify the full path.
use std::collections::HashMap;
fn main() {
let mut map = HashMap::new();
map.insert(1, 2);
}
There’s no strong reason behind this idiom: it’s just the convention that has emerged, and folks have gotten used to reading and writing Rust code this way.
The exception to this idiom is if we’re bringing two items with the same name into scope with use
statements, because Rust doesn’t allow that. The following listing shows how to bring two Result
types into scope that have the same name but different parent modules, and how to refer to them.
Filename: src/lib.rs
use std::fmt;
use std::io;
fn function1() -> fmt::Result {
}
fn function2() -> io::Result<()> {
}
Providing New Names with the as Keyword
There’s another solution to the problem of bringing two types of the same name into the same scope with use
: after the path, we can specify as
and a new local name, or alias, for the type.
Filename: src/lib.rs
use std::fmt::Result;
use std::io::Result as IoResult;
fn function1() -> Result {
}
fn function2() -> IoResult<()> {
}
Re-exporting Names with pub use
When we bring a name into scope with the use
keyword, the name available in the new scope is private. To enable the code that calls our code to refer to that name as if it had been defined in that code’s scope, we can combine pub
and use
. This technique is called re-exporting because we’re bringing an item into scope but also making that item available for others to bring into their scope.
Filename: src/lib.rs
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
}
Before this change, external code would have to call the add_to_waitlist
function by using the path restaurant::front_of_house::hosting::add_to_waitlist()
. Now that this pub use
has re-exported the hosting
module from the root module, external code can now use the path restaurant::hosting::add_to_waitlist()
instead.
Re-exporting is useful when the internal structure of your code is different from how programmers calling your code would think about the domain.
With pub use
, we can write our code with one structure but expose a different structure. Doing so makes our library well organized for programmers working on the library and programmers calling the library.
Using External Packages
To use the external package rand
in our project, we added this line to Cargo.toml:
Filename: Cargo.toml
rand = "0.8.5"
Adding rand
as a dependency in Cargo.toml tells Cargo to download the rand
package and any dependencies from crates.io and make rand
available to our project.
Then, to bring rand
definitions into the scope of our package, we added a use
line starting with the name of the crate, rand
, and listed the items we wanted to bring into scope. We brought the Rng
trait into scope and called the rand::thread_rng
function:
use rand::Rng;
fn main() {
let secret_number = rand::thread_rng().gen_range(1..=100);
}
Members of the Rust community have made many packages available at crates.io, and pulling any of them into your package involves these same steps: listing them in your package’s Cargo.toml file and using use
to bring items from their crates into scope.
Note that the standard std
library is also a crate that’s external to our package. Because the standard library is shipped with the Rust language, we don’t need to change Cargo.toml to include std
. But we do need to refer to it with use
to bring items from there into our package’s scope.
use std::collections::HashMap;
Using Nested Paths to Clean Up Large use Lists
If we’re using multiple items defined in the same crate or same module, listing each item on its own line can take up a lot of vertical space in our files. Instead, we can use nested paths to bring the same items into scope in one line. We do this by specifying the common part of the path, followed by two colons, and then curly brackets around a list of the parts of the paths that differ.
Filename: src/main.rs
use std::cmp::Ordering;
use std::io;
use std::{cmp::Ordering, io};
To merge a complete path and its subpath into one use
statement, we can use self
in the nested path.
Filename: src/lib.rs
use std::io;
use std::io::Write;
use std::io::{self, Write};
The Glob Operator
If we want to bring all public items defined in a path into scope, we can specify that path followed by the *
glob operator:
use std::collections::*;
This use
statement brings all public items defined in std::collections
into the current scope.
Be careful when using the glob operator! Glob can make it harder to tell what names are in scope and where a name used in your program was defined.
The glob operator is often used when testing to bring everything under test into the tests module. The glob operator is also sometimes used as part of the prelude pattern.
Separating Modules into Different Files
For example, let’s start from the code in previous listing that had multiple restaurant modules. We’ll extract modules into files instead of having all the modules defined in the crate root file.
First, we’ll extract front_of_house
module to its own file. Remove the code inside the curly brackets for the front_of_house
module, leaving only the mod front_of_house
; declaration, so that src/lib.rs contains the code shown in following list.
Filename: src/lib.rs
mod front_of_house;
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
}
Next, place the code that was in the curly brackets into a new file named src/front_of_house.rs, as shown in the following listing. The compiler knows to look in this file because it came across the module declaration in the crate root with the name front_of_house
.
Filename: src/front_of_house.rs
pub mod hosting {
pub fn add_to_waitlist() {}
}
Note that you only need to load a file using a mod
declaration once in your module tree. Once the compiler knows the file is part of the project (and knows where in the module tree the code resides because of where you’ve put the mod statement), other files in your project should refer to the loaded file’s code using a path to where it was declared. In other words, mod
is not an “include” operation that you may have seen in other programming languages.
Next, we’ll extract the hosting
module to its own file. The process is a bit different because hosting
is a child module of front_of_house
, not of the root module. We’ll place the file for hosting
in a new directory that will be named for its ancestors in the module tree, in this case src/front_of_house.
To start moving hosting
, we change src/front_of_house.rs to contain only the declaration of the hosting
module:
Filename: src/front_of_house.rs
pub mod hosting;
Then we create a src/front_of_house directory and a file hosting.rs to contain the definitions made in the hosting
module:
Filename: src/front_of_house/hosting.rs
pub fn add_to_waitlist() {}
If we instead put hosting.rs in the src directory, the compiler would expect the hosting.rs code to be in a hosting
module declared in the crate root, and not declared as a child of the front_of_house
module.
Alternate File Paths
So far we’ve covered the most idiomatic file paths the Rust compiler uses, but Rust also supports an older style of file path. For a module named front_of_house
declared in the crate root, the compiler will look for the module’s code in:
src/front_of_house.rs (what we covered)
src/front_of_house/mod.rs (older style, still supported path)
For a module named hosting
that is a submodule of front_of_house
, the compiler will look for the module’s code in:
src/front_of_house/hosting.rs (what we covered)
src/front_of_house/hosting/mod.rs (older style, still supported path)
If you use both styles for the same module, you’ll get a compiler error. Using a mix of both styles for different modules in the same project is allowed, but might be confusing for people navigating your project.
The main downside to the style that uses files named mod.rs is that your project can end up with many files named mod.rs, which can get confusing when you have them open in your editor at the same time.
The mod
keyword declares modules, and Rust looks in a file with the same name as the module for the code that goes into that module.
Common Collections
Most other data types represent one specific value, but collections can contain multiple values. Unlike the built-in array and tuple types, the data these collections point to is stored on the heap, which means the amount of data does not need to be known at compile time and can grow or shrink as the program runs.
Storing Lists of Values with Vectors
Vectors allow you to store more than one value in a single data structure that puts all the values next to each other in memory. Vectors can only store values of the same type.
Creating a New Vector
To create a new empty vector, we call the Vec::new
function.
let v: Vec<i32> = Vec::new();
Note that we added a type annotation here. Because we aren’t inserting any values into this vector, Rust doesn’t know what kind of elements we intend to store.
More often, you’ll create a Vec<T>
with initial values and Rust will infer the type of value you want to store, so you rarely need to do this type annotation. Rust conveniently provides the vec!
macro, which will create a new vector that holds the values you give it.
let v = vec![1, 2, 3];
Updating a Vector
To create a vector and then add elements to it, we can use the push
method.
let mut v = Vec::new();
v.push(5);
v.push(6);
As with any variable, if we want to be able to change its value, we need to make it mutable using the mut
keyword. The numbers we place inside are all of type i32, and Rust infers this from the data, so we don’t need the Vec<i32>
annotation.
Reading Elements of Vectors
There are two ways to reference a value stored in a vector: via indexing or using the get
method.
let v = vec![1, 2, 3, 4, 5];
let third: &i32 = &v[2];
println!("The third element is {third}");
let third: Option<&i32> = v.get(2);
match third {
Some(third) => println!("The third element is {third}"),
None => println!("There is no third element."),
}
Using &
and []
gives us a reference to the element at the index value. When we use the get
method with the index passed as an argument, we get an Option<&T>
that we can use with match
.
Rust provides these two ways to reference an element so you can choose how the program behaves when you try to use an index value outside the range of existing elements.
let v = vec![1, 2, 3, 4, 5];
let does_not_exist = &v[100];
let does_not_exist = v.get(100);
When we run this code, the first []
method will cause the program to panic because it references a nonexistent element. This method is best used when you want your program to crash if there’s an attempt to access an element past the end of the vector.
When the get
method is passed an index that is outside the vector, it returns None
without panicking. You would use this method if accessing an element beyond the range of the vector may happen occasionally under normal circumstances. Your code will then have logic to handle having either Some(&element)
or None
.
When the program has a valid reference, the borrow checker enforces the ownership and borrowing rules to ensure this reference and any other references to the contents of the vector remain valid.
Recall the rule that states you can’t have mutable and immutable references in the same scope. That rule applies where we hold an immutable reference to the first element in a vector and try to add an element to the end. This program won’t work if we also try to refer to that element later in the function.
let mut v = vec![1, 2, 3, 4, 5];
let first = &v[0];
v.push(6);
println!("The first element is: {first}");
The code might look like it should work: why should a reference to the first element care about changes at the end of the vector? This error is due to the way vectors work: because vectors put the values next to each other in memory, adding a new element onto the end of the vector might require allocating new memory and copying the old elements to the new space, if there isn’t enough room to put all the elements next to each other where the vector is currently stored. In that case, the reference to the first element would be pointing to deallocated memory. The borrowing rules prevent programs from ending up in that situation.
Iterating over the Values in a Vector
To access each element in a vector in turn, we would iterate through all of the elements rather than use indices to access one at a time. You can use a for
loop to get immutable references to each element in a vector and print them.
let v = vec![100, 32, 57];
for i in &v {
println!("{i}");
}
We can also iterate over mutable references to each element in a mutable vector in order to make changes to all the elements.
let mut v = vec![100, 32, 57];
for i in &mut v {
*i += 50;
}
To change the value that the mutable reference refers to, we have to use the *
dereference operator to get to the value in i
before we can use the +=
operator.
Iterating over a vector, whether immutably or mutably, is safe because of the borrow checker’s rules. If we attempted to insert or remove items in the for
loop bodies, we would get a compiler error. The reference to the vector that the for
loop holds prevents simultaneous modification of the whole vector.
Using an Enum to Store Multiple Types
Vectors can only store values that are the same type. Fortunately, the variants of an enum are defined under the same enum type, so when we need one type to represent elements of different types, we can define and use an enum.
We can define an enum whose variants will hold the different value types, and all the enum variants will be considered the same type: that of the enum. Then we can create a vector to hold that enum and so, ultimately, holds different types.
enum SpreadsheetCell {
Int(i32),
Float(f64),
Text(String),
}
let row = vec![
SpreadsheetCell::Int(3),
SpreadsheetCell::Text(String::from("blue")),
SpreadsheetCell::Float(10.12),
];
Rust needs to know what types will be in the vector at compile time so it knows exactly how much memory on the heap will be needed to store each element. We must also be explicit about what types are allowed in this vector. If Rust allowed a vector to hold any type, there would be a chance that one or more of the types would cause errors with the operations performed on the elements of the vector. Using an enum plus a match expression means that Rust will ensure at compile time that every possible case is handled.
If you don’t know the exhaustive set of types a program will get at runtime to store in a vector, the enum technique won’t work. Instead, you can use a trait object.
Dropping a Vector Drops Its Elements
Like any other struct, a vector is freed when it goes out of scope.
{
let v = vec![1, 2, 3, 4];
}
When the vector gets dropped, all of its contents are also dropped, meaning the integers it holds will be cleaned up. The borrow checker ensures that any references to contents of a vector are only used while the vector itself is valid.
Storing UTF-8 Encoded Text with Strings
What Is a String?
Rust has only one string type in the core language, which is the string slice str that is usually seen in its borrowed form &str
. String literals, for example, are stored in the program’s binary and are therefore string slices.
The String
type, which is provided by Rust’s standard library rather than coded into the core language, is a growable, mutable, owned, UTF-8 encoded string type.
When Rustaceans refer to “strings” in Rust, they might be referring to either the String
or the string slice &str
types, not just one of those types. Both String
and string slices are UTF-8 encoded.
Creating a New String
Many of the same operations available with Vec<T>
are available with String
as well, because String
is actually implemented as a wrapper around a vector of bytes with some extra guarantees, restrictions, and capabilities.
let mut s = String::new();
Often, we’ll have some initial data that we want to start the string with. For that, we use the to_string
method, which is available on any type that implements the Display
trait, as string literals do.
let data = "initial contents";
let s = data.to_string();
let s = "initial contents".to_string();
We can also use the function String::from
to create a String
from a string literal.
let s = String::from("initial contents");
In this case, String::from
and to_string
do the same thing, so which you choose is a matter of style and readability.
Updating a String
A String
can grow in size and its contents can change, just like the contents of a Vec<T>
, if you push more data into it. In addition, you can conveniently use the +
operator or the format!
macro to concatenate String values.
Appending to a String with push_str
and push
let mut s = String::from("foo");
s.push_str("bar");
The push_str
method takes a string slice because we don’t necessarily want to take ownership of the parameter.
let mut s1 = String::from("foo");
let s2 = "bar";
s1.push_str(s2);
println!("s2 is {s2}");
The push
method takes a single character as a parameter and adds it to the String
.
let mut s = String::from("lo");
s.push('l');
Concatenation with the +
Operator or the format!
Macro
let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = s1 + &s2;
The reason s1
is no longer valid after the addition, and the reason we used a reference to s2
, has to do with the signature of the method that’s called when we use the +
operator. The +
operator uses the add
method, whose signature looks something like this:
fn add(self, s: &str) -> String {
In the standard library, you’ll see add
defined using generics and associated types. Here, we’ve substituted in concrete types, which is what happens when we call this method with String
values. This signature gives us the clues we need to understand the tricky bits of the +
operator.
First, s2
has an &
, meaning that we’re adding a reference of the second string to the first string. This is because of the s
parameter in the add
function: we can only add a &str
to a String
; we can’t add two String
values together. But wait - the type of &s2
is &String
, not &str
, as specified in the second parameter to add.
The reason we’re able to use &s2
in the call to add
is that the compiler can coerce the &String
argument into a &str
. When we call the add
method, Rust uses a deref coercion, which here turns &s2
into &s2[..]
. Because add
does not take ownership of the s
parameter, s2
will still be a valid String
after this operation.
Second, we can see in the signature that add
takes ownership of self
, because self
does not have an &
. This means s1
will be moved into the add
call and will no longer be valid after that. So although let s3 = s1 + &s2;
looks like it will copy both strings and create a new one, this statement actually takes ownership of s1
, appends a copy of the contents of s2
, and then returns ownership of the result. In other words, it looks like it’s making a lot of copies but isn’t; the implementation is more efficient than copying.
If we need to concatenate multiple strings, the behavior of the +
operator gets unwieldy:
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
let s = s1 + "-" + &s2 + "-" + &s3;
For combining strings in more complicated ways, we can instead use the format!
macro:
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
let s = format!("{s1}-{s2}-{s3}");
The format!
macro works like println!
, but instead of printing the output to the screen, it returns a String
with the contents. The version of the code using format!
is much easier to read, and the code generated by the format!
macro uses references so that this call doesn’t take ownership of any of its parameters.
Indexing into Strings
In many other programming languages, accessing individual characters in a string by referencing them by index is a valid and common operation. However, if you try to access parts of a String
using indexing syntax in Rust, you’ll get an error. Rust strings don’t support indexing.
let s1 = String::from("hello");
let h = s1[0];
Internal Representation
A String
is a wrapper over a Vec<u8>
.
let hello = String::from("Hola");
let hello = String::from("Здравствуйте");
Different Unicode scalar value takes different bytes of storage. Therefore, an index into the string’s bytes will not always correlate to a valid Unicode scalar value.
let hello = "hello";
let answer = &hello[0];
When encoded in UTF-8, indexing into a string will return a byte value instead of the character on its own.
The answer, then, is that to avoid returning an unexpected value and causing bugs that might not be discovered immediately, Rust doesn’t compile this code at all and prevents misunderstandings early in the development process.
Bytes and Scalar Values and Grapheme Clusters! Oh My!
Another point about UTF-8 is that there are actually three relevant ways to look at strings from Rust’s perspective: as bytes (how computers ultimately store this data), scalar values (what Rust’s char type is), and grapheme clusters (the closest thing to what we would call letters).
If we look at the Hindi word “नमस्ते” written in the Devanagari script, it is stored as a vector of u8 values that looks like this:
[224, 164, 168, 224, 164, 174, 224, 164, 184, 224, 165, 141, 224, 164, 164, 224, 165, 135]
That’s 18 bytes and is how computers ultimately store this data. If we look at them as Unicode scalar values, which are what Rust’s char
type is, those bytes look like this:
['न', 'म', 'स', '्', 'त', 'े']
There are six char
values here, but the fourth and sixth are not letters: they’re diacritics that don’t make sense on their own. Finally, if we look at them as grapheme clusters, we’d get what a person would call the four letters that make up the Hindi word:
["न", "म", "स्", "ते"]
Rust provides different ways of interpreting the raw string data that computers store so that each program can choose the interpretation it needs, no matter what human language the data is in.
A final reason Rust doesn’t allow us to index into a String
to get a character is that indexing operations are expected to always take constant time (O(1)). But it isn’t possible to guarantee that performance with a String
, because Rust would have to walk through the contents from the beginning to the index to determine how many valid characters there were.
Slicing Strings
Indexing into a string is often a bad idea because it’s not clear what the return type of the string-indexing operation should be: a byte value, a character, a grapheme cluster, or a string slice.
Rather than indexing using []
with a single number, you can use []
with a range to create a string slice containing particular bytes.
let hello = "Здравствуйте";
let s = &hello[0..4];
Here, s
will be a &str
that contains the first four bytes of the string. Earlier, we mentioned that each of these characters was two bytes, which means s
will be Зд
.
If we were to try to slice only part of a character’s bytes with something like &hello[0..1]
, Rust would panic at runtime because index 1 is not a char boundary. Each char has 2 bytes in this case.
Methods for Iterating Over Strings
The best way to operate on pieces of strings is to be explicit about whether you want characters or bytes. For individual Unicode scalar values, use the chars
method. Alternatively, the bytes
method returns each raw byte.
for c in "Зд".chars() {
println!("{c}");
}
for b in "Зд".bytes() {
println!("{b}");
}
But be sure to remember that valid Unicode scalar values may be made up of more than one byte.
Getting grapheme clusters from strings as with the Devanagari script is complex, so this functionality is not provided by the standard library.
Strings Are Not So Simple
Rust has chosen to make the correct handling of String
data the default behavior for all Rust programs, which means programmers have to put more thought into handling UTF-8 data upfront. This trade-off exposes more of the complexity of strings than is apparent in other programming languages, but it prevents you from having to handle errors involving non-ASCII characters later in your development life cycle.
Storing Keys with Associated Values in Hash Maps
The type HashMap<K, V>
stores a mapping of keys of type K
to values of type V
using a hashing function, which determines how it places these keys and values into memory.
Many programming languages support this kind of data structure, but they often use a different name, such as hash, map, object, hash table, dictionary, or associative array, just to name a few.
Hash maps are useful when you want to look up data not by using an index, as you can with vectors, but by using a key that can be of any type.
Creating a New Hash Map
One way to create an empty hash map is using new
and adding elements with insert
.
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
Note that we need to first use
the HashMap
from the collections portion of the standard library. Of our three common collections, this one is the least often used, so it’s not included in the features brought into scope automatically in the prelude. Hash maps also have less support from the standard library; there’s no built-in macro to construct them, for example.
Just like vectors, hash maps store their data on the heap. Like vectors, hash maps are homogeneous: all of the keys must have the same type as each other, and all of the values must have the same type.
Accessing Values in a Hash Map
We can get a value out of the hash map by providing its key to the get
method.
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
let team_name = String::from("Blue");
let score = scores.get(&team_name).copied().unwrap_or(0);
The get
method returns an Option<&V>
; if there’s no value for that key in the hash map, get
will return None
. This program handles the Option
by calling copied
to get an Option<i32>
rather than an Option<&i32>
, then unwrap_or
to set score
to zero if scores
doesn’t have an entry for the key.
We can iterate over each key-value pair in a hash map in a similar manner as we do with vectors, using a for
loop.
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
for (key, value) in &scores {
println!("{key}: {value}");
}
This code will print each pair in an arbitrary order:
Yellow: 50
Blue: 10
Hash Maps and Ownership
For types that implement the Copy
trait, like i32
, the values are copied into the hash map. For owned values like String
, the values will be moved and the hash map will be the owner of those values.
use std::collections::HashMap;
let field_name = String::from("Favorite color");
let field_value = String::from("Blue");
let mut map = HashMap::new();
map.insert(field_name, field_value);
If we insert references to values into the hash map, the values won’t be moved into the hash map. The values that the references point to must be valid for at least as long as the hash map is valid.
Updating a Hash Map
Although the number of key and value pairs is growable, each unique key can only have one value associated with it at a time, but not vice versa.
When you want to change the data in a hash map, you have to decide how to handle the case when a key already has a value assigned. You could replace the old value with the new value, completely disregarding the old value. You could keep the old value and ignore the new value, only adding the new value if the key doesn’t already have a value. Or you could combine the old value and the new value.
Overwriting a Value
If we insert a key and a value into a hash map and then insert that same key with a different value, the value associated with that key will be replaced.
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Blue"), 25);
println!("{:?}", scores);
Adding a Key and Value Only If a Key Isn’t Present
It’s common to check whether a particular key already exists in the hash map with a value then take the following actions: if the key does exist in the hash map, the existing value should remain the way it is. If the key doesn’t exist, insert it and a value for it.
Hash maps have a special API for this called entry
that takes the key you want to check as a parameter. The return value of the entry
method is an enum called Entry
that represents a value that might or might not exist.
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.entry(String::from("Yellow")).or_insert(50);
scores.entry(String::from("Blue")).or_insert(50);
println!("{:?}", scores);
The or_insert
method on Entry
is defined to return a mutable reference to the value for the corresponding Entry
key if that key exists, and if not, inserts the parameter as the new value for this key and returns a mutable reference to the new value.
Updating a Value Based on the Old Value
Another common use case for hash maps is to look up a key’s value and then update it based on the old value.
use std::collections::HashMap;
let text = "hello world wonderful world";
let mut map = HashMap::new();
for word in text.split_whitespace() {
let count = map.entry(word).or_insert(0);
*count += 1;
}
println!("{:?}", map);
The split_whitespace
method returns an iterator over sub-slices, separated by whitespace, of the value in text
. The or_insert
method returns a mutable reference (&mut V
) to the value for the specified key. Here we store that mutable reference in the count
variable, so in order to assign to that value, we must first dereference count
using the asterisk (*).
The mutable reference goes out of scope at the end of the for
loop, so all of these changes are safe and allowed by the borrowing rules.
Hashing Functions
By default, HashMap
uses a hashing function called SipHash that can provide resistance to denial-of-service (DoS) attacks involving hash tables. This is not the fastest hashing algorithm available, but the trade-off for better security that comes with the drop in performance is worth it.
If you profile your code and find that the default hash function is too slow for your purposes, you can switch to another function by specifying a different hasher. A hasher is a type that implements the BuildHasher
trait.
Error Handling
Rust groups errors into two major categories: recoverable and unrecoverable errors.
For a recoverable error, such as a file not found error, we most likely just want to report the problem to the user and retry the operation.
Unrecoverable errors are always symptoms of bugs, like trying to access a location beyond the end of an array, and so we want to immediately stop the program.
Unrecoverable Errors with panic!
There are two ways to cause a panic in practice:
By taking an action that causes our code to panic (such as accessing an array past the end).
By explicitly calling the panic!
macro.
By default, these panics will print a failure message, unwind, clean up the stack, and quit. Via an environment variable, you can also have Rust display the call stack when a panic occurs to make it easier to track down the source of the panic.
Unwinding the Stack or Aborting in Response to a Panic
By default, when a panic occurs, the program starts unwinding, which means Rust walks back up the stack and cleans up the data from each function it encounters. However, this walking back and cleanup is a lot of work. Rust, therefore, allows you to choose the alternative of immediately aborting, which ends the program without cleaning up.
Memory that the program was using will then need to be cleaned up by the operating system. If in your project you need to make the resulting binary as small as possible, you can switch from unwinding to aborting upon a panic by adding panic = 'abort'
to the appropriate [profile]
sections in your Cargo.toml file. For example, if you want to abort on panic in release mode, add this:
[profile.release]
panic = 'abort'
fn main() {
panic!("crash and burn");
}
The call to panic!
causes the error message contained in the last two lines. The first line shows our panic message and the place in our source code where the panic occurred: src/main.rs:2:5
indicates that it’s the second line, fifth character of our src/main.rs file.
In other cases, the panic!
call might be in code that our code calls, and the filename and line number reported by the error message will be someone else’s code where the panic!
macro is called, not the line of our code that eventually led to the panic!
call.
We can use the backtrace of the functions the panic!
call came from to figure out the part of our code that is causing the problem.
fn main() {
let v= vec![1, 2, 3];
v[99];
}
In C, attempting to read beyond the end of a data structure is undefined behavior. You might get whatever is at the location in memory that would correspond to that element in the data structure, even though the memory doesn’t belong to that structure. This is called a buffer overread and can lead to security vulnerabilities if an attacker is able to manipulate the index in such a way as to read data they shouldn’t be allowed to that is stored after the data structure.
To protect your program from this sort of vulnerability, if you try to read an element at an index that doesn’t exist, Rust will stop execution and refuse to continue.
We can set the RUST_BACKTRACE
environment variable to any value except 0
to get a backtrace of exactly what happened to cause the error. A backtrace is a list of all the functions that have been called to get to this point. Backtraces in Rust work as they do in other languages: the key to reading the backtrace is to start from the top and read until you see files you wrote. That’s the spot where the problem originated. The lines above that spot are code that your code has called; the lines below are code that called your code. These before-and-after lines might include core Rust code, standard library code, or crates that you’re using.
$ RUST_BACKTRACE=1 cargo run
thread 'main' panicked at src/main.rs:4:6:
index out of bounds: the len is 3 but the index is 99
stack backtrace:
0: rust_begin_unwind
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:662:5
1: core::panicking::panic_fmt
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/panicking.rs:74:14
2: core::panicking::panic_bounds_check
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/panicking.rs:276:5
3: <usize as core::slice::index::SliceIndex<[T]>>::index
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/slice/index.rs:302:10
4: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/slice/index.rs:16:9
5: <alloc::vec::Vec<T,A> as core::ops::index::Index<I>>::index
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/alloc/src/vec/mod.rs:2920:9
6: panic::main
at ./src/main.rs:4:6
7: core::ops::function::FnOnce::call_once
at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
In order to get backtraces with this information, debug symbols must be enabled. Debug symbols are enabled by default when using cargo build
or cargo run
without the --release
flag, as we have here.
Recoverable Errors with Result
The Result
enum is defined as having two variants, Ok
and Err
, as follows:
enum Result<T, E> {
Ok(T),
Err(E),
}
T
represents the type of the value that will be returned in a success case within the Ok
variant, and E
represents the type of the error that will be returned in a failure case within the Err
variant. Because Result
has these generic type parameters, we can use the Result
type and the functions defined on it in many different situations where the successful value and error value we want to return may differ.
use std::fs::File;
fn main() {
let greeting_file_result = File::open("hello.txt");
}
The return type of File::open
is a Result<T, E>
. The generic parameter T
has been filled in by the implementation of File::open
with the type of the success value, std::fs::File
, which is a file handle. The type of E
used in the error value is std::io::Error
.
We need to add to the code to take different actions depending on the value File::open
returns.
use std::fs::File;
fn main() {
let greeting_file_result = File::open("hello.txt");
let greeting_file = match greeting_file_result {
Ok(file) => file,
Err(error) => panic!("Problem opening the file: {:?}", error),
};
}
Note that, like the Option
enum, the Result
enum and its variants have been brought into scope by the prelude, so we don’t need to specify Result::
before the Ok
and Err
variants in the match
arms.
Matching on Different Errors
The preceding code will panic!
no matter why File::open
failed. However, we want to take different actions for different failure reasons: if File::open
failed because the file doesn’t exist, we want to create the file and return the handle to the new file. If File::open
failed for any other reason - for example, because we didn’t have permission to open the file - we still want the code to panic!
.
use std::fs::File;
use std::io::ErrorKind;
fn main() {
let greeting_file_result = File::open("hello.txt");
let greeting_file = match greeting_file_result {
Ok(file) => file,
Err(error) => match error.kind() {
ErrorKind::NotFound => match File::create("hello.txt") {
Ok(fc) => fc,
Err(e) => panic!("Problem creating the file: {:?}", e),
},
other_error => {
panic!("Problem opening the file: {:?}", other_error);
}
},
};
}
The type of the value that File::open
returns inside the Err
variant is io::Error
, which is a struct provided by the standard library. This struct has a method kind that we can call to get an io::ErrorKind
value. The enum io::ErrorKind
is provided by the standard library and has variants representing the different kinds of errors that might result from an io
operation. The variant we want to use is ErrorKind::NotFound
, which indicates the file we’re trying to open doesn’t exist yet. So we match on greeting_file_result
, but we also have an inner match on error.kind()
.
Alternatives to Using match with Result<T, E>
That’s a lot of match
! The match
expression is very useful but also very much a primitive. Closures are used with many of the methods defined on Result<T, E>
. These methods can be more concise than using match
when handling Result<T, E>
values in your code.
use std::fs::File;
use std::io::ErrorKind;
fn main() {
let greeting_file = File::open("hello.txt").unwrap_or_else(|error| {
if error.kind() == ErrorKind::NotFound {
File::create("hello.txt").unwrap_or_else(|error| {
panic!("Problem creating the file: {:?}", error);
})
} else {
panic!("Problem opening the file: {:?}", error);
}
});
}
Many more of these methods can clean up huge nested match
expressions when you’re dealing with errors.
Shortcuts for Panic on Error: unwrap and expect
The unwrap
method is a shortcut method implemented just like the match
expression. If the Result
value is the Ok
variant, unwrap
will return the value inside the Ok
. If the Result
is the Err
variant, unwrap
will call the panic!
macro for us.
use std::fs::File;
fn main() {
let greeting_file = File::open("hello.txt").unwrap();
}
Similarly, the expect
method lets us also choose the panic!
error message. The error message used by expect
in its call to panic!
will be the parameter that we pass to expect
, rather than the default panic!
message that unwrap
uses.
use std::fs::File;
fn main() {
let greeting_file = File::open("hello.txt")
.expect("hello.txt should be included in this project");
}
In production-quality code, most Rustaceans choose expect
rather than unwrap
and give more context about why the operation is expected to always succeed.
Propagating Errors
When a function’s implementation calls something that might fail, instead of handling the error within the function itself, you can return the error to the calling code so that it can decide what to do. This is known as propagating the error and gives more control to the calling code, where there might be more information or logic that dictates how the error should be handled than what you have available in the context of your code.
use std::fs::File;
use std::io::{self, Read};
fn read_username_from_file() -> Result<String, io::Error> {
let username_file_result = File::open("hello.txt");
let mut username_file = match username_file_result {
Ok(file) => file,
Err(e) => return Err(e),
};
let mut username = String::new();
match username_file.read_to_string(&mut username) {
Ok(_) => Ok(username),
Err(e) => Err(e),
}
}
This function can be written in a much shorter way, but we’re going to start by doing a lot of it manually in order to explore error handling; at the end, we’ll show the shorter way.
Let’s look at the return type of the function first: Result<String, io::Error>
. This means the function is returning a value of the type Result<T, E>
where the generic parameter T
has been filled in with the concrete type String
, and the generic type E
has been filled in with the concrete type io::Error
.
We chose io::Error
as the return type of this function because that happens to be the type of the error value returned from both of the operations we’re calling in this function’s body that might fail: the File::open
function and the read_to_string
method.
This pattern of propagating errors is so common in Rust that Rust provides the question mark operator ?
to make this easier.
A Shortcut for Propagating Errors: the ?
Operator
use std::fs::File;
use std::io::{self, Read};
fn read_username_from_file() -> Result<String, io::Error> {
let mut username_file = File::open("hello.txt")?;
let mut username = String::new();
username_file.read_to_string(&mut username)?;
Ok(username)
}
The ?
placed after a Result
value is defined to work in almost the same way as the match
expressions we defined to handle the Result
values. If the value of the Result
is an Ok
, the value inside the Ok
will get returned from this expression, and the program will continue. If the value is an Err
, the Err
will be returned from the whole function as if we had used the return
keyword so the error value gets propagated to the calling code.
There is a difference between what the match
expression does and what the ?
operator does: error values that have the ?
operator called on them go through the from
function, defined in the From
trait in the standard library, which is used to convert values from one type into another. When the ?
operator calls the from
function, the error type received is converted into the error type defined in the return type of the current function. This is useful when a function returns one error type to represent all the ways a function might fail, even if parts might fail for many different reasons.
For example, we could change the read_username_from_file
function to return a custom error type named OurError
that we define. If we also define impl From<io::Error> for OurError
to construct an instance of OurError
from an io::Error
, then the ?
operator calls in the body of read_username_from_file
will call from
and convert the error types without needing to add any more code to the function.
The ?
at the end of the File::open
call will return the value inside an Ok
to the variable username_file
. If an error occurs, the ?
operator will return early out of the whole function and give any Err
value to the calling code. The same thing applies to the ?
at the end of the read_to_string
call.
The ?
operator eliminates a lot of boilerplate and makes this function’s implementation simpler. We could even shorten this code further by chaining method calls immediately after the ?
.
use std::fs::File;
use std::io::{self, Read};
fn read_username_from_file() -> Result<String, io::Error> {
let mut username = String::new();
File::open("hello.txt")?.read_to_string(&mut username)?;
Ok(username)
}
Instead of creating a variable username_file
, we’ve chained the call to read_to_string
directly onto the result of File::open("hello.txt")?
. We still have a ?
at the end of the read_to_string
call, and we still return an Ok
value containing username
when both File::open
and read_to_string
succeed rather than returning errors.
use std::fs;
use std::io;
fn read_username_from_file() -> Result<String, io::Error> {
fs::read_to_string("hello.txt")
}
Reading a file into a string is a fairly common operation, so the standard library provides the convenient fs::read_to_string
function that opens the file, creates a new String
, reads the contents of the file, puts the contents into that String
, and returns it. Of course, using fs::read_to_string
doesn’t give us the opportunity to explain all the error handling, so we did it the longer way first.
Where The ? Operator Can Be Used
The ?
operator can only be used in functions whose return type is compatible with the value the ?
is used on. This is because the ?
operator is defined to perform an early return of a value out of the function.
Let’s look at the error we’ll get if we use the ?
operator in a main function with a return type incompatible with the type of the value we use ?
on:
use std::fs::File;
fn main() {
let greeting_file = File::open("hello.txt")?;
}
This code opens a file, which might fail. The ?
operator follows the Result
value returned by File::open
, but this main function has the return type of ()
, not Result
.
This error points out that we’re only allowed to use the ?
operator in a function that returns Result
, Option
, or another type that implements FromResidual
.
To fix the error, you have two choices. One choice is to change the return type of your function to be compatible with the value you’re using the ?
operator on as long as you have no restrictions preventing that. The other technique is to use a match or one of the Result<T, E>
methods to handle the Result<T, E>
in whatever way is appropriate.
The error message also mentioned that ?
can be used with Option<T>
values as well. As with using ?
on Result
, you can only use ?
on Option
in a function that returns an Option
. If the value is None
, the None
will be returned early from the function at that point. If the value is Some
, the value inside the Some
is the resulting value of the expression and the function continues.
fn last_char_of_first_line(text: &str) -> Option<char> {
text.lines().next()?.chars().last()
}
This function returns Option<char>
because it’s possible that there is a character there, but it’s also possible that there isn’t. This code takes the text
string slice argument and calls the lines
method on it, which returns an iterator over the lines in the string. Because this function wants to examine the first line, it calls next
on the iterator to get the first value from the iterator. If text
is the empty string, this call to next
will return None
, in which case we use ?
to stop and return None
from last_char_of_first_line
. If text
is not the empty string, next
will return a Some
value containing a string slice of the first line in text
.
The ?
extracts the string slice, and we can call chars
on that string slice to get an iterator of its characters. We’re interested in the last character in this first line, so we call last
to return the last item in the iterator. This is an Option
because it’s possible that the first line is the empty string, for example if text starts with a blank line but has characters on other lines, as in "\nhi"
. However, if there is a last character on the first line, it will be returned in the Some
variant.
Note that you can use the ?
operator on a Result
in a function that returns Result
, and you can use the ?
operator on an Option
in a function that returns Option
, but you can’t mix and match. The ?
operator won’t automatically convert a Result
to an Option
or vice versa; in those cases, you can use methods like the ok
method on Result
or the ok_or
method on Option
to do the conversion explicitly.
So far, all the main
functions we’ve used return ()
. The main
function is special because it’s the entry and exit point of executable programs, and there are restrictions on what its return type can be for the programs to behave as expected.
Luckily, main
can also return a Result<(), E>
. We’ve changed the return type of main
to be Result<(), Box<dyn Error>>
and added a return value Ok(())
to the end. This code will now compile:
use std::error::Error;
use std::fs::File;
fn main() -> Result<(), Box<dyn Error>> {
let greeting_file = File::open("hello.txt")?;
Ok(())
}
The Box<dyn Error>
type is a trait object. For now, you can read Box<dyn Error>
to mean “any kind of error.” Using ?
on a Result
value in a main
function with the error type Box<dyn Error>
is allowed, because it allows any Err
value to be returned early.
When a main
function returns a Result<(), E>
, the executable will exit with a value of 0
if main
returns Ok(())
and will exit with a nonzero value if main
returns an Err
value.
The main
function may return any types that implement the std::process::Termination
trait, which contains a function report
that returns an ExitCode
.
To panic!
or Not to panic!
When you choose to return a Result
value, you give the calling code options. The calling code could choose to attempt to recover in a way that’s appropriate for its situation, or it could decide that an Err
value in this case is unrecoverable, so it can call panic! and turn your recoverable error into an unrecoverable one. Therefore, returning Result
is a good default choice when you’re defining a function that might fail.
In situations such as examples, prototype code, and tests, it’s more appropriate to write code that panics instead of returning a Result
.
Examples, Prototype Code, and Tests
When you’re writing an example to illustrate some concept, also including robust error-handling code can make the example less clear. In examples, it’s understood that a call to a method like unwrap
that could panic is meant as a placeholder for the way you’d want your application to handle errors, which can differ based on what the rest of your code is doing.
Similarly, the unwrap
and expect
methods are very handy when prototyping, before you’re ready to decide how to handle errors. They leave clear markers in your code for when you’re ready to make your program more robust.
If a method call fails in a test, you’d want the whole test to fail, even if that method isn’t the functionality under test. Because panic!
is how a test is marked as a failure, calling unwrap
or expect
is exactly what should happen.
It would also be appropriate to call unwrap
or expect
when you have some other logic that ensures the Result
will have an Ok
value, but the logic isn’t something the compiler understands.
You’ll still have a Result
value that you need to handle: whatever operation you’re calling still has the possibility of failing in general, even though it’s logically impossible in your particular situation.
However, having a hardcoded, valid string doesn’t change the return type of the parse
method: we still get a Result
value, and the compiler will still make us handle the Result
as if the Err
variant is a possibility because the compiler isn’t smart enough to see that this string is always a valid IP address.
If you can ensure by manually inspecting the code that you’ll never have an Err
variant, it’s perfectly acceptable to call unwrap
, and even better to document the reason you think you’ll never have an Err
variant in the expect
text. Here’s an example:
use std::net::IpAddr;
let home: IpAddr = "127.0.0.1"
.parse()
.expect("Hardcoded IP address should be valid");
Guidelines for Error Handling
It’s advisable to have your code panic when it’s possible that your code could end up in a bad state. In this context, a bad state is when some assumption, guarantee, contract, or invariant has been broken, such as when invalid values, contradictory values, or missing values are passed to your code - plus one or more of the following:
The bad state is something that is unexpected, as opposed to something that will likely happen occasionally, like a user entering data in the wrong format.
Your code after this point needs to rely on not being in this bad state, rather than checking for the problem at every step.
There’s not a good way to encode this information in the types you use.
If someone calls your code and passes in values that don’t make sense, it’s best to return an error if you can so the user of the library can decide what they want to do in that case. However, in cases where continuing could be insecure or harmful, the best choice might be to call panic!
and alert the person using your library to the bug in their code so they can fix it during development. Similarly, panic!
is often appropriate if you’re calling external code that is out of your control and it returns an invalid state that you have no way of fixing.
However, when failure is expected, it’s more appropriate to return a Result
than to make a panic!
call. Examples include a parser being given malformed data or an HTTP request returning a status that indicates you have hit a rate limit. In these cases, returning a Result
indicates that failure is an expected possibility that the calling code must decide how to handle.
When your code performs an operation that could put a user at risk if it’s called using invalid values, your code should verify the values are valid first and panic if the values aren’t valid. This is mostly for safety reasons: attempting to operate on invalid data can expose your code to vulnerabilities. This is the main reason the standard library will call panic!
if you attempt an out-of-bounds memory access: trying to access memory that doesn’t belong to the current data structure is a common security problem. Functions often have contracts: their behavior is only guaranteed if the inputs meet particular requirements. Panicking when the contract is violated makes sense because a contract violation always indicates a caller-side bug and it’s not a kind of error you want the calling code to have to explicitly handle. In fact, there’s no reasonable way for calling code to recover; the calling programmers need to fix the code. Contracts for a function, especially when a violation will cause a panic, should be explained in the API documentation for the function.
However, having lots of error checks in all of your functions would be verbose and annoying. Fortunately, you can use Rust’s type system (and thus the type checking done by the compiler) to do many of the checks for you. If your function has a particular type as a parameter, you can proceed with your code’s logic knowing that the compiler has already ensured you have a valid value. For example, if you have a type rather than an Option
, your program expects to have something rather than nothing. Your code then doesn’t have to handle two cases for the Some
and None
variants: it will only have one case for definitely having a value. Code trying to pass nothing to your function won’t even compile, so your function doesn’t have to check for that case at runtime. Another example is using an unsigned integer type such as u32
, which ensures the parameter is never negative.
Creating Custom Types for Validation
Recall the guessing game in Chapter 2 in which our code asked the user to guess a number between 1 and 100. We never validated that the user’s guess was between those numbers before checking it against our secret number; we only validated that the guess was positive. But it would be a useful enhancement to guide the user toward valid guesses and have different behavior when a user guesses a number that’s out of range versus when a user types, for example, letters instead.
One way to do this would be to parse the guess as an i32
instead of only a u32
to allow potentially negative numbers, and then add a check for the number being in range, like so:
loop {
let guess: i32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
if guess < 1 || guess > 100 {
println!("The secret number will be between 1 and 100.");
continue;
}
match guess.cmp(&secret_number) {
}
However, this is not an ideal solution: if it was absolutely critical that the program only operated on values between 1 and 100, and it had many functions with this requirement, having a check like this in every function would be tedious (and might impact performance).
Instead, we can make a new type and put the validations in a function to create an instance of the type rather than repeating the validations everywhere. That way, it’s safe for functions to use the new type in their signatures and confidently use the values they receive. The following example shows one way to define a Guess
type that will only create an instance of Guess
if the new
function receives a value between 1 and 100.
pub struct Guess {
value: i32,
}
impl Guess {
pub fn new(value: i32) -> Guess {
if value < 1 || value > 100 {
panic!("Guess value must be between 1 and 100, got {}.", value);
}
Guess { value }
}
pub fn value(&self) -> i32 {
self.value
}
}
The code in the body of the new
function tests value
to make sure it’s between 1 and 100.
The public method value
is necessary because the value
field of the Guess
struct is private. It’s important that the value
field be private so code using the Guess
struct is not allowed to set value
directly: code outside the module must use the Guess::new
function to create an instance of Guess
, thereby ensuring there’s no way for a Guess
to have a value
that hasn’t been checked by the conditions in the Guess::new
function.
Generic Types, Traits, and Lifetimes
Generics: abstract stand-ins for concrete types or other properties. We can express the behavior of generics or how they relate to other generics without knowing what will be in their place when compiling and running the code.
Generics allow us to replace specific types with a placeholder that represents multiple types to remove code duplication.
Generic Data Types
In Function Definitions
To parameterize the types in a new single function, we need to name the type parameter, just as we do for the value parameters to a function. You can use any identifier as a type parameter name. But we’ll use T
because, by convention, type parameter names in Rust are short, often just a letter, and Rust’s type-naming convention is UpperCamelCase. Short for “type”, T
is the default choice of most Rust programmers.
When we use a type parameter name in a function signature, we have to declare the type parameter name before we use it. To define the generic largest
function, place type name declarations inside angle brackets, <>
, between the name of the function and the parameter list, like this:
fn largest<T>(list: &[T]) -> &T {
The listing also shows how we can call the function with either a slice of i32
values or char
values. Note that this code won’t compile yet, but we’ll fix it later.
Filename: src/main.rs
fn largest<T>(list: &[T]) -> &T {
let mut largest = &list[0];
for item in list {
if item > largest {
largest = item;
}
}
largest
}
fn main() {
let number_list = vec![34, 50, 25, 100, 65];
let result = largest(&number_list);
println!("The largest number is {}", result);
let char_list = vec!['y', 'm', 'a', 'q'];
let result = largest(&char_list);
println!("The largest char is {}", result);
}
The help text mentions std::cmp::PartialOrd
, which is a trait, and we’re going to talk about traits in the next section. For now, know that this error states that the body of largest won’t work for all possible types that T
could be. Because we want to compare values of type T
in the body, we can only use types whose values can be ordered. To enable comparisons, the standard library has the std::cmp::PartialOrd
trait that you can implement on types. By following the help text’s suggestion, we restrict the types valid for T
to only those that implement PartialOrd
and this example will compile, because the standard library implements PartialOrd
on both i32
and char
.
In struct Definitions
We can also define structs to use a generic type parameter in one or more fields using the <>
syntax.
Filename: src/main.rs
struct Point<T> {
x: T,
y: T,
}
fn main() {
let integer = Point { x: 5, y: 10 };
let float = Point { x: 1.0, y: 4.0 };
let wont_work = Point { x: 5, y: 4.0 };
}
To define a Point struct where x
and y
are both generics but could have different types, we can use multiple generic type parameters.
Filename: src/main.rs
struct Point<T, U> {
x: T,
y: U,
}
fn main() {
let both_integer = Point { x: 5, y: 10 };
let both_float = Point { x: 1.0, y: 4.0 };
let integer_and_float = Point { x: 5, y: 4.0 };
}
If you’re finding you need lots of generic types in your code, it could indicate that your code needs restructuring into smaller pieces.
In Enum Definitions
As we did with structs, we can define enums to hold generic data types in their variants.
enum Option<T> {
Some(T),
None,
}
By using the Option<T>
enum, we can express the abstract concept of an optional value, and because Option<T>
is generic, we can use this abstraction no matter what the type of the optional value is.
Enums can use multiple generic types as well. The definition of the Result
enum is one example:
enum Result<T, E> {
Ok(T),
Err(E),
}
This definition makes it convenient to use the Result
enum anywhere we have an operation that might succeed (return a value of some type T
) or fail (return an error of some type E
).
In Method Definitions
We can implement methods on structs and enums and use generic types in their definitions, too.
Filename: src/main.rs
struct Point<T> {
x: T,
y: T,
}
impl<T> Point<T> {
fn x(&self) -> &T {
&self.x
}
}
fn main() {
let p = Point { x: 5, y: 10 };
println!("p.x = {}", p.x());
}
Note that we have to declare T
just after impl
so we can use T
to specify that we’re implementing methods on the type Point<T>
. By declaring T
as a generic type after impl
, Rust can identify that the type in the angle brackets in Point
is a generic type rather than a concrete type. We could have chosen a different name for this generic parameter than the generic parameter declared in the struct definition, but using the same name is conventional. Methods written within an impl
that declares the generic type will be defined on any instance of the type, no matter what concrete type ends up substituting for the generic type.
We can also specify constraints on generic types when defining methods on the type. We could, for example, implement methods only on Point<f32>
instances rather than on Point<T>
instances with any generic type.
Filename: src/main.rs
impl Point<f32> {
fn distance_from_origin(&self) -> f32 {
(self.x.powi(2) + self.y.powi(2)).sqrt()
}
}
This code means the type Point<f32>
will have a distance_from_origin
method; other instances of Point<T>
where T
is not of type f32
will not have this method defined. The method measures how far our point is from the point at coordinates (0.0, 0.0) and uses mathematical operations that are available only for floating point types.
Generic type parameters in a struct definition aren’t always the same as those you use in that same struct’s method signatures.
Filename: src/main.rs
struct Point<X1, Y1> {
x: X1,
y: Y1,
}
impl<X1, Y1> Point<X1, Y1> {
fn mixup<X2, Y2>(self, other: Point<X2, Y2>) -> Point<X1, Y2> {
Point {
x: self.x,
y: other.y,
}
}
}
fn main() {
let p1 = Point { x: 5, y: 10.4 };
let p2 = Point { x: "Hello", y: 'c' };
let p3 = p1.mixup(p2);
println!("p3.x = {}, p3.y = {}", p3.x, p3.y);
}
You might be wondering whether there is a runtime cost when using generic type parameters. The good news is that using generic types won’t make your program run any slower than it would with concrete types.
Rust accomplishes this by performing monomorphization of the code using generics at compile time. Monomorphization is the process of turning generic code into specific code by filling in the concrete types that are used when compiled. In this process, the compiler does the opposite of the steps we used to create the generic function: the compiler looks at all the places where generic code is called and generates code for the concrete types the generic code is called with.
Let’s look at how this works by using the standard library’s generic Option<T>
enum:
let integer = Some(5);
let float = Some(5.0);
The monomorphized version of the code looks similar to the following (the compiler uses different names than what we’re using here for illustration):
Filename: src/main.rs
enum Option_i32 {
Some(i32),
None,
}
enum Option_f64 {
Some(f64),
None,
}
fn main() {
let integer = Option_i32::Some(5);
let float = Option_f64::Some(5.0);
}
The generic Option<T>
is replaced with the specific definitions created by the compiler. Because Rust compiles generic code into code that specifies the type in each instance, we pay no runtime cost for using generics. When the code runs, it performs just as it would if we had duplicated each definition by hand. The process of monomorphization makes Rust’s generics extremely efficient at runtime.
Traits: Defining Shared Behavior
A trait defines functionality a particular type has and can share with other types. We can use traits to define shared behavior in an abstract way. We can use trait bounds to specify that a generic type can be any type that has certain behavior.
Note: Traits are similar to a feature often called interfaces in other languages, although with some differences.
Defining a Trait
Trait definitions are a way to group method signatures together to define a set of behaviors necessary to accomplish some purpose.
For example, let’s say we have multiple structs that hold various kinds and amounts of text: a NewsArticle
struct that holds a news story filed in a particular location and a Tweet
that can have at most 280 characters along with metadata that indicates whether it was a new tweet, a retweet, or a reply to another tweet.
We want to make a media aggregator library crate named aggregator
that can display summaries of data that might be stored in a NewsArticle
or Tweet
instance. To do this, we need a summary from each type, and we’ll request that summary by calling a summarize
method on an instance. The following listing shows the definition of a public Summary
trait that expresses this behavior.
Filename: src/lib.rs
pub trait Summary {
fn summarize(&self) -> String;
}
After the method signature, instead of providing an implementation within curly brackets, we use a semicolon. Each type implementing this trait must provide its own custom behavior for the body of the method. The compiler will enforce that any type that has the Summary
trait will have the method summarize
defined with this signature exactly.
A trait can have multiple methods in its body: the method signatures are listed one per line and each line ends in a semicolon.
Implementing a Trait on a Type
Now that we’ve defined the desired signatures of the Summary
trait’s methods, we can implement it on the types in our media aggregator.
Filename: src/lib.rs
pub struct NewsArticle {
pub headline: String,
pub location: String,
pub author: String,
pub content: String,
}
impl Summary for NewsArticle {
fn summarize(&self) -> String {
format!("{}, by {} ({})", self.headline, self.author, self.location)
}
}
pub struct Tweet {
pub username: String,
pub content: String,
pub reply: bool,
pub retweet: bool,
}
impl Summary for Tweet {
fn summarize(&self) -> String {
format!("{}: {}", self.username, self.content)
}
}
Now that the library has implemented the Summary
trait on NewsArticle
and Tweet
, users of the crate can call the trait methods on instances of NewsArticle
and Tweet
in the same way we call regular methods. The only difference is that the user must bring the trait into scope as well as the types. Here’s an example of how a binary crate could use our aggregator
library crate:
use aggregator::{Summary, Tweet};
fn main() {
let tweet = Tweet {
username: String::from("horse_ebooks"),
content: String::from(
"of course, as you probably already know, people",
),
reply: false,
retweet: false,
};
println!("1 new tweet: {}", tweet.summarize());
}
Other crates that depend on the aggregator
crate can also bring the Summary
trait into scope to implement Summary
on their own types. One restriction to note is that we can implement a trait on a type only if at least one of the trait or the type is local to our crate. For example, we can implement standard library traits like Display
on a custom type like Tweet
as part of our aggregator
crate functionality, because the type Tweet is local to our aggregator
crate. We can also implement Summary
on Vec<T>
in our aggregator
crate, because the trait Summary
is local to our aggregator
crate.
But we can’t implement external traits on external types. For example, we can’t implement the Display
trait on Vec<T>
within our aggregator
crate, because Display
and Vec<T>
are both defined in the standard library and aren’t local to our aggregator
crate. This restriction is part of a property called coherence, and more specifically the orphan rule, so named because the parent type is not present. This rule ensures that other people’s code can’t break your code and vice versa. Without the rule, two crates could implement the same trait for the same type, and Rust wouldn’t know which implementation to use.
Default Implementations
Sometimes it’s useful to have default behavior for some or all of the methods in a trait instead of requiring implementations for all methods on every type. Then, as we implement the trait on a particular type, we can keep or override each method’s default behavior.
Filename: src/lib.rs
pub trait Summary {
fn summarize(&self) -> String {
String::from("(Read more...)")
}
}
To use a default implementation to summarize instances of NewsArticle
, we specify an empty impl
block with impl Summary for NewsArticle {}
.
let article = NewsArticle {
headline: String::from("Penguins win the Stanley Cup Championship!"),
location: String::from("Pittsburgh, PA, USA"),
author: String::from("Iceburgh"),
content: String::from(
"The Pittsburgh Penguins once again are the best \
hockey team in the NHL.",
),
};
println!("New article available! {}", article.summarize());
The syntax for overriding a default implementation is the same as the syntax for implementing a trait method that doesn’t have a default implementation.
Default implementations can call other methods in the same trait, even if those other methods don’t have a default implementation.
pub trait Summary {
fn summarize_author(&self) -> String;
fn summarize(&self) -> String {
format!("(Read more from {}...)", self.summarize_author())
}
}
To use this version of Summary, we only need to define summarize_author when we implement the trait on a type:
impl Summary for Tweet {
fn summarize_author(&self) -> String {
format!("@{}", self.username)
}
}
let tweet = Tweet {
username: String::from("horse_ebooks"),
content: String::from(
"of course, as you probably already know, people",
),
reply: false,
retweet: false,
};
println!("1 new tweet: {}", tweet.summarize());
Note that it isn’t possible to call the default implementation from an overriding implementation of that same method.
Traits as Parameters
Now that you know how to define and implement traits, we can explore how to use traits to define functions that accept many different types. To do this, we use the impl Trait
syntax, like this:
pub fn notify(item: &impl Summary) {
println!("Breaking news! {}", item.summarize());
}
Instead of a concrete type for the item
parameter, we specify the impl
keyword and the trait name. This parameter accepts any type that implements the specified trait.
Trait Bound Syntax
The impl Trait
syntax works for straightforward cases but is actually syntax sugar for a longer form known as a trait bound; it looks like this:
pub fn notify<T: Summary>(item: &T) {
println!("Breaking news! {}", item.summarize());
}
The impl Trait
syntax is convenient and makes for more concise code in simple cases, while the fuller trait bound syntax can express more complexity in other cases. For example, we can have two parameters that implement Summary
. Doing so with the impl Trait
syntax looks like this:
pub fn notify(item1: &impl Summary, item2: &impl Summary) {
Using impl Trait
is appropriate if we want this function to allow item1
and item2
to have different types (as long as both types implement Summary
). If we want to force both parameters to have the same type, however, we must use a trait bound, like this:
pub fn notify<T: Summary>(item1: &T, item2: &T) {
Specifying Multiple Trait Bounds with the + Syntax
We can also specify more than one trait bound. We specify in the notify
definition that item must implement both Display
and Summary
. We can do so using the +
syntax:
pub fn notify(item: &(impl Summary + Display)) {
The +
syntax is also valid with trait bounds on generic types:
pub fn notify<T: Summary + Display>(item: &T) {
Clearer Trait Bounds with where Clauses
Using too many trait bounds has its downsides. Each generic has its own trait bounds, so functions with multiple generic type parameters can contain lots of trait bound information between the function’s name and its parameter list, making the function signature hard to read. For this reason, Rust has alternate syntax for specifying trait bounds inside a where
clause after the function signature. So instead of writing this:
fn some_function<T: Display + Clone, U: Clone + Debug>(t: &T, u: &U) -> i32 {
we can use a where
clause, like this:
fn some_function<T, U>(t: &T, u: &U) -> i32
where
T: Display + Clone,
U: Clone + Debug,
{
Returning Types that Implement Traits
We can also use the impl Trait
syntax in the return position to return a value of some type that implements a trait, as shown here:
fn returns_summarizable() -> impl Summary {
Tweet {
username: String::from("horse_ebooks"),
content: String::from(
"of course, as you probably already know, people",
),
reply: false,
retweet: false,
}
}
The ability to specify a return type only by the trait it implements is especially useful in the context of closures and iterators. Closures and iterators create types that only the compiler knows or types that are very long to specify. The impl Trait
syntax lets you concisely specify that a function returns some type that implements the Iterator
trait without needing to write out a very long type.
However, you can only use impl Trait if you’re returning a single type. For example, this code that returns either a NewsArticle
or a Tweet
with the return type specified as impl Summary
wouldn’t work:
fn returns_summarizable(switch: bool) -> impl Summary {
if switch {
NewsArticle {
headline: String::from(
"Penguins win the Stanley Cup Championship!",
),
location: String::from("Pittsburgh, PA, USA"),
author: String::from("Iceburgh"),
content: String::from(
"The Pittsburgh Penguins once again are the best \
hockey team in the NHL.",
),
}
} else {
Tweet {
username: String::from("horse_ebooks"),
content: String::from(
"of course, as you probably already know, people",
),
reply: false,
retweet: false,
}
}
}
Using Trait Bounds to Conditionally Implement Methods
By using a trait bound with an impl
block that uses generic type parameters, we can implement methods conditionally for types that implement the specified traits. For example, the type Pair<T>
in the following listing always implements the new
function to return a new instance of Pair<T>
. But in the next impl
block, Pair<T>
only implements the cmp_display
method if its inner type T
implements the PartialOrd
trait that enables comparison and the Display
trait that enables printing.
Filename: src/lib.rs
use std::fmt::Display;
struct Pair<T> {
x: T,
y: T,
}
impl<T> Pair<T> {
fn new(x: T, y: T) -> Self {
Self { x, y }
}
}
impl<T: Display + PartialOrd> Pair<T> {
fn cmp_display(&self) {
if self.x >= self.y {
println!("The largest member is x = {}", self.x);
} else {
println!("The largest member is y = {}", self.y);
}
}
}
We can also conditionally implement a trait for any type that implements another trait. Implementations of a trait on any type that satisfies the trait bounds are called blanket implementations and are extensively used in the Rust standard library. For example, the standard library implements the ToString
trait on any type that implements the Display
trait. The impl
block in the standard library looks similar to this code:
impl<T: Display> ToString for T {
}
Because the standard library has this blanket implementation, we can call the to_string
method defined by the ToString
trait on any type that implements the Display
trait. For example, we can turn integers into their corresponding String
values like this because integers implement Display
:
let s = 3.to_string();
Validating References with Lifetimes
Lifetimes are another kind of generic. Rather than ensuring that a type has the behavior we want, lifetimes ensure that references are valid as long as we need them to be.
Every reference in Rust has a lifetime, which is the scope for which that reference is valid.
Most of the time, lifetimes are implicit and inferred, just like most of the time, types are inferred. We must only annotate types when multiple types are possible. In a similar way, we must annotate lifetimes when the lifetimes of references could be related in a few different ways. Rust requires us to annotate the relationships using generic lifetime parameters to ensure the actual references used at runtime will definitely be valid.
Preventing Dangling References with Lifetimes
The main aim of lifetimes is to prevent dangling references, which cause a program to reference data other than the data it’s intended to reference. Consider the program in the following listing, which has an outer scope and an inner scope.
fn main() {
let r;
{
let x = 5;
r = &x;
}
println!("r: {}", r);
}
The Borrow Checker
The Rust compiler has a borrow checker that compares scopes to determine whether all borrows are valid.
fn main() {
let r;
{
let x = 5;
r = &x;
}
println!("r: {}", r);
}
Here, we’ve annotated the lifetime of r
with 'a
and the lifetime of x
with 'b
. As you can see, the inner 'b
block is much smaller than the outer 'a
lifetime block. At compile time, Rust compares the size of the two lifetimes and sees that r
has a lifetime of 'a
but that it refers to memory with a lifetime of 'b
. The program is rejected because 'b
is shorter than 'a
: the subject of the reference doesn’t live as long as the reference.
The following listing fixes the code so it doesn’t have a dangling reference and compiles without any errors.
fn main() {
let x = 5;
let r = &x;
println!("r: {}", r);
}
Here, x
has the lifetime 'b
, which in this case is larger than 'a
. This means r
can reference x
because Rust knows that the reference in r
will always be valid while x
is valid.
Generic Lifetimes in Functions
We’ll write a function that returns the longer of two string slices. This function will take two string slices and return a single string slice. After we’ve implemented the longest
function, the code in the following listing should print The longest string is abcd
.
Filename: src/main.rs
fn main() {
let string1 = String::from("abcd");
let string2 = "xyz";
let result = longest(string1.as_str(), string2);
println!("The longest string is {}", result);
}
Note that we want the function to take string slices, which are references, rather than strings, because we don’t want the longest
function to take ownership of its parameters.
If we try to implement the longest function as shown in the following listing, it won’t compile.
Filename: src/main.rs
fn longest(x: &str, y: &str) -> &str {
if x.len() > y.len() {
x
} else {
y
}
}
The help text reveals that the return type needs a generic lifetime parameter on it because Rust can’t tell whether the reference being returned refers to x
or y
. Actually, we don’t know either, because the if
block in the body of this function returns a reference to x
and the else
block returns a reference to y
!
Lifetime Annotation Syntax
Lifetime annotations don’t change how long any of the references live. Rather, they describe the relationships of the lifetimes of multiple references to each other without affecting the lifetimes. Just as functions can accept any type when the signature specifies a generic type parameter, functions can accept references with any lifetime by specifying a generic lifetime parameter.
Lifetime annotations have a slightly unusual syntax: the names of lifetime parameters must start with an apostrophe ('
) and are usually all lowercase and very short, like generic types. Most people use the name 'a
for the first lifetime annotation. We place lifetime parameter annotations after the &
of a reference, using a space to separate the annotation from the reference’s type.
Here are some examples:
&i32
&'a i32
&'a mut i32
One lifetime annotation by itself doesn’t have much meaning because the annotations are meant to tell Rust how generic lifetime parameters of multiple references relate to each other.
Lifetime Annotations in Function Signatures
We want the signature to express the following constraint: the returned reference will be valid as long as both the parameters are valid. We’ll name the lifetime 'a
and then add it to each reference, as shown in the following listing.
Filename: src/main.rs
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
In practice, it means that the lifetime of the reference returned by the longest
function is the same as the smaller of the lifetimes of the values referred to by the function arguments.
Remember, when we specify the lifetime parameters in this function signature, we’re not changing the lifetimes of any values passed in or returned. Rather, we’re specifying that the borrow checker should reject any values that don’t adhere to these constraints. Note that the longest
function doesn’t need to know exactly how long x
and y
will live, only that some scope can be substituted for 'a
that will satisfy this signature.
When annotating lifetimes in functions, the annotations go in the function signature, not in the function body. Having function signatures contain the lifetime contract means the analysis the Rust compiler does can be simpler.
When we pass concrete references to longest
, the concrete lifetime that is substituted for 'a
is the part of the scope of x
that overlaps with the scope of y
. In other words, the generic lifetime 'a
will get the concrete lifetime that is equal to the smaller of the lifetimes of x
and y
.
Let’s look at how the lifetime annotations restrict the longest
function by passing in references that have different concrete lifetimes. The following listing is a straightforward example.
Filename: src/main.rs
fn main() {
let string1 = String::from("long string is long");
{
let string2 = String::from("xyz");
let result = longest(string1.as_str(), string2.as_str());
println!("The longest string is {}", result);
}
}
Next, let’s try an example that shows that the lifetime of the reference in result
must be the smaller lifetime of the two arguments. The code in the following listing will not compile.
Filename: src/main.rs
fn main() {
let string1 = String::from("long string is long");
let result;
{
let string2 = String::from("xyz");
result = longest(string1.as_str(), string2.as_str());
}
println!("The longest string is {}", result);
}
Thinking in Terms of Lifetimes
The way in which you need to specify lifetime parameters depends on what your function is doing. For example, if we changed the implementation of the longest
function to always return the first parameter rather than the longest string slice, we wouldn’t need to specify a lifetime on the y
parameter. The following code will compile:
Filename: src/main.rs
fn longest<'a>(x: &'a str, y: &str) -> &'a str {
x
}
When returning a reference from a function, the lifetime parameter for the return type needs to match the lifetime parameter for one of the parameters. If the reference returned does not refer to one of the parameters, it must refer to a value created within this function. However, this would be a dangling reference because the value will go out of scope at the end of the function. Consider this attempted implementation of the longest
function that won’t compile:
Filename: src/main.rs
fn longest<'a>(x: &str, y: &str) -> &'a str {
let result = String::from("really long string");
result.as_str()
}
In this case, the best fix would be to return an owned data type rather than a reference so the calling function is then responsible for cleaning up the value.
Ultimately, lifetime syntax is about connecting the lifetimes of various parameters and return values of functions. Once they’re connected, Rust has enough information to allow memory-safe operations and disallow operations that would create dangling pointers or otherwise violate memory safety.
Lifetime Annotations in Struct Definitions
We can define structs to hold references, but in that case we would need to add a lifetime annotation on every reference in the struct’s definition. The following listing has a struct named ImportantExcerpt
that holds a string slice.
Filename: src/main.rs
struct ImportantExcerpt<'a> {
part: &'a str,
}
fn main() {
let novel = String::from("Call me Ishmael. Some years ago...");
let first_sentence = novel.split('.').next().expect("Could not find a '.'");
let i = ImportantExcerpt {
part: first_sentence,
};
}
This annotation means an instance of ImportantExcerpt
can’t outlive the reference it holds in its part
field.
Lifetime Elision
You’ve learned that every reference has a lifetime and that you need to specify lifetime parameters for functions or structs that use references. However, in earlier chapter we had a function shown again in the following listing, that compiled without lifetime annotations.
Filename: src/lib.rs
fn first_word(s: &str) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
The patterns programmed into Rust’s analysis of references are called the lifetime elision rules. These aren’t rules for programmers to follow; they’re a set of particular cases that the compiler will consider, and if your code fits these cases, you don’t need to write the lifetimes explicitly.
Lifetimes on function or method parameters are called input lifetimes, and lifetimes on return values are called output lifetimes.
The compiler uses three rules to figure out the lifetimes of the references when there aren’t explicit annotations. The first rule applies to input lifetimes, and the second and third rules apply to output lifetimes. If the compiler gets to the end of the three rules and there are still references for which it can’t figure out lifetimes, the compiler will stop with an error. These rules apply to fn
definitions as well as impl
blocks.
The first rule is that the compiler assigns a lifetime parameter to each parameter that’s a reference. In other words, a function with one parameter gets one lifetime parameter: fn foo<'a>(x: &'a i32)
; a function with two parameters gets two separate lifetime parameters: fn foo<'a, 'b>(x: &'a i32, y: &'b i32)
; and so on.
The second rule is that, if there is exactly one input lifetime parameter, that lifetime is assigned to all output lifetime parameters: fn foo<'a>(x: &'a i32) -> &'a i32
.
The third rule is that, if there are multiple input lifetime parameters, but one of them is &self
or &mut self
because this is a method, the lifetime of self
is assigned to all output lifetime parameters. This third rule makes methods much nicer to read and write because fewer symbols are necessary.
Lifetime Annotations in Method Definitions
impl<'a> ImportantExcerpt<'a> {
fn level(&self) -> i32 {
3
}
}
The lifetime parameter declaration after impl
and its use after the type name are required, but we’re not required to annotate the lifetime of the reference to self
because of the first elision rule.
Here is an example where the first and the third lifetime elision rule applies:
impl<'a> ImportantExcerpt<'a> {
fn announce_and_return_part(&self, announcement: &str) -> &str {
println!("Attention please: {}", announcement);
self.part
}
}
There are two input lifetimes, so Rust applies the first lifetime elision rule and gives both &self
and announcement
their own lifetimes. Then, because one of the parameters is &self
, the return type gets the lifetime of &self
, and all lifetimes have been accounted for.
The Static Lifetime
One special lifetime we need to discuss is 'static
, which denotes that the affected reference can live for the entire duration of the program. All string literals have the 'static
lifetime, which we can annotate as follows:
let s: &'static str = "I have a static lifetime.";
The text of this string is stored directly in the program’s binary, which is always available. Therefore, the lifetime of all string literals is 'static
.
You might see suggestions in error messages to use the 'static
lifetime. But before specifying 'static
as the lifetime for a reference, think about whether the reference you have actually lives the entire lifetime of your program or not, and whether you want it to. Most of the time, an error message suggesting the 'static
lifetime results from attempting to create a dangling reference or a mismatch of the available lifetimes. In such cases, the solution is to fix those problems, not to specify the 'static
lifetime.
Generic Type Parameters, Trait Bounds, and Lifetimes Together
use std::fmt::Display;
fn longest_with_an_announcement<'a, T>(
x: &'a str,
y: &'a str,
ann: T,
) -> &'a str
where
T: Display,
{
println!("Announcement! {}", ann);
if x.len() > y.len() {
x
} else {
y
}
}
Writing Automated Tests
How to Write Tests
Tests are Rust functions that verify that the non-test code is functioning in the expected manner. The bodies of test functions typically perform these three actions:
Set up any needed data or state.
Run the code you want to test.
Assert the results are what you expect.
The Anatomy of a Test Function
Attributes are metadata about pieces of Rust code. A Test in Rust is a function that’s annotated with the test
attribute.
To change a function into a test function, add #[test]
on the line before fn
. When you run your tests with the cargo test
command, Rust builds a test runner binary that runs the annotated functions and reports on whether each test function passes or fails.
Whenever we make a new library project with Cargo, a test module with a test function in it is automatically generated for us.
Let’s create a new library project called adder
that will add two numbers:
$ cargo new adder --lib
Created library `adder` project
$ cd adder
The contents of the src/lib.rs file in your adder
library should look like the following listing.
Filename: src/lib.rs
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
For now, let’s ignore the top two lines and focus on the function. Note the #[test]
annotation: this attribute indicates this is a test function, so the test runner knows to treat this function as a test. We might also have non-test functions in the tests module to help set up common scenarios or perform common operations, so we always need to indicate which functions are tests.
The cargo test
command runs all tests in our project, as shown in the following listing.
$ cargo test
Compiling adder v0.1.0 (file:///projects/adder)
Finished test [unoptimized + debuginfo] target(s) in 0.57s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
It’s possible to mark a test as ignored so it doesn’t run in a particular instance. Because we haven’t done that here, the summary shows 0 ignored
.
The 0 measured
statistic is for benchmark tests that measure performance. Benchmark tests are, as of this writing, only available in nightly Rust.
We can pass an argument to the cargo test
command to run only tests whose name matches a string; this is called filtering. Here we haven’t filtered the tests being run, so the end of the summary shows 0 filtered out
.
The next part of the test output starting at Doc-tests adder
is for the results of any documentation tests. We don’t have any documentation tests yet, but Rust can compile any code examples that appear in our API documentation. This feature helps keep your docs and your code in sync! For now, we’ll ignore the Doc-tests
output.
Let’s start to customize the test to our own needs. First change the name of the it_works
function to a different name, such as exploration
. Then we’ll add another test, but this time we’ll make a test that fails! Tests fail when something in the test function panics. Each test is run in a new thread, and when the main thread sees that a test thread has died, the test is marked as failed. Enter the new test as a function named another
, so your src/lib.rs file looks like the following listing.
Filename: src/lib.rs
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exploration() {
assert_eq!(2 + 2, 4);
}
#[test]
fn another() {
panic!("Make this test fail");
}
}
Run the tests again using cargo test
. The output should look like the following listing, which shows that our exploration
test passed and another
failed.
$ cargo test
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.72s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 2 tests
test tests::another ... FAILED
test tests::exploration ... ok
failures:
---- tests::another stdout ----
thread 'tests::another' panicked at src/lib.rs:17:9:
Make this test fail
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::another
test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
Instead of ok
, the line test tests::another
shows FAILED
. Two new sections appear between the individual results and the summary: the first displays the detailed reason for each test failure. In this case, we get the details that another
failed because it panicked at 'Make this test fail'
on line 17 in the src/lib.rs file. The next section lists just the names of all the failing tests, which is useful when there are lots of tests and lots of detailed failing test output. We can use the name of a failing test to run just that test to more easily debug it.
The summary line displays at the end: overall, our test result is FAILED
. We had one test pass and one test fail.
Checking Results with the assert! Macro
The assert!
macro, provided by the standard library, is useful when you want to ensure that some condition in a test evaluates to true
. We give the assert!
macro an argument that evaluates to a Boolean
. If the value is true
, nothing happens and the test passes. If the value is false
, the assert!
macro calls panic!
to cause the test to fail.
In the preceding listing, we used a Rectangle
struct and a can_hold
method, which are repeated here. Let’s put this code in the src/lib.rs file, then write some tests for it using the assert!
macro.
Filename: src/lib.rs
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn larger_can_hold_smaller() {
let larger = Rectangle {
width: 8,
height: 7,
};
let smaller = Rectangle {
width: 5,
height: 1,
};
assert!(larger.can_hold(&smaller));
}
#[test]
fn smaller_cannot_hold_larger() {
let larger = Rectangle {
width: 8,
height: 7,
};
let smaller = Rectangle {
width: 5,
height: 1,
};
assert!(!smaller.can_hold(&larger));
}
}
Note the use super::*
; line inside the tests
module. The tests
module is a regular module that follows the usual visibility rules. Because the tests
module is an inner module, we need to bring the code under test in the outer module into the scope of the inner module. We use a glob here, so anything we define in the outer module is available to this tests
module.
$ cargo test
Compiling rectangle v0.1.0 (file:///projects/rectangle)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.66s
Running unittests src/lib.rs (target/debug/deps/rectangle-6584c4561e48942e)
running 2 tests
test tests::larger_can_hold_smaller ... ok
test tests::smaller_cannot_hold_larger ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests rectangle
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Two tests that pass! Now let’s see what happens to our test results when we introduce a bug in our code. We’ll change the implementation of the can_hold
method by replacing the greater-than sign with a less-than sign when it compares the widths:
impl Rectangle {
fn can_hold(&self, other: &Rectangle) -> bool {
self.width < other.width && self.height > other.height
}
}
Running the tests now produces the following:
$ cargo test
Compiling rectangle v0.1.0 (file:///projects/rectangle)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.66s
Running unittests src/lib.rs (target/debug/deps/rectangle-6584c4561e48942e)
running 2 tests
test tests::larger_can_hold_smaller ... FAILED
test tests::smaller_cannot_hold_larger ... ok
failures:
---- tests::larger_can_hold_smaller stdout ----
thread 'tests::larger_can_hold_smaller' panicked at src/lib.rs:28:9:
assertion failed: larger.can_hold(&smaller)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::larger_can_hold_smaller
test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
Our tests caught the bug!
Testing Equality with the assert_eq! and assert_ne! Macros
A common way to verify functionality is to test for equality between the result of the code under test and the value you expect the code to return. You could do this by using the assert!
macro and passing it an expression using the ==
operator. However, this is such a common test that the standard library provides a pair of macros - assert_eq!
and assert_ne!
- to perform this test more conveniently. These macros compare two arguments for equality or inequality, respectively. They’ll also print the two values if the assertion fails, which makes it easier to see why the test failed; conversely, the assert!
macro only indicates that it got a false
value for the ==
expression, without printing the values that led to the false
value.
Filename: src/lib.rs
pub fn add_two(a: usize) -> usize {
a + 3
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_adds_two() {
let result = add_two(2);
assert_eq!(result, 4);
}
}
Run the tests:
$ cargo test
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.61s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 1 test
test tests::it_adds_two ... FAILED
failures:
---- tests::it_adds_two stdout ----
thread 'tests::it_adds_two' panicked at src/lib.rs:12:9:
assertion `left == right` failed
left: 5
right: 4
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::it_adds_two
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
Note that in some languages and test frameworks, the parameters to equality assertion functions are called expected
and actual
, and the order in which we specify the arguments matters. However, in Rust, they’re called left
and right
, and the order in which we specify the value we expect and the value the code produces doesn’t matter.
The assert_ne!
macro will pass if the two values we give it are not equal and fail if they’re equal. This macro is most useful for cases when we’re not sure what a value will be, but we know what the value definitely shouldn’t be.
Under the surface, the assert_eq!
and assert_ne!
macros use the operators ==
and !=
, respectively. When the assertions fail, these macros print their arguments using debug formatting, which means the values being compared must implement the PartialEq
and Debug
traits. All primitive types and most of the standard library types implement these traits. For structs and enums that you define yourself, you’ll need to implement PartialEq
to assert equality of those types. You’ll also need to implement Debug
to print the values when the assertion fails. Because both traits are derivable traits, this is usually as straightforward as adding the #[derive(PartialEq, Debug)]
annotation to your struct or enum definition.
Adding Custom Failure Messages
You can also add a custom message to be printed with the failure message as optional arguments to the assert!
, assert_eq!
, and assert_ne!
macros. Any arguments specified after the required arguments are passed along to the format!
macro, so you can pass a format string that contains {}
placeholders and values to go in those placeholders. Custom messages are useful for documenting what an assertion means; when a test fails, you’ll have a better idea of what the problem is with the code.
For example, let’s say we have a function that greets people by name and we want to test that the name we pass into the function appears in the output:
Filename: src/lib.rs
pub fn greeting(name: &str) -> String {
format!("Hello {name}!")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn greeting_contains_name() {
let result = greeting("Carol");
assert!(result.contains("Carol"));
}
}
Now let’s introduce a bug into this code by changing greeting to exclude name to see what the default test failure looks like:
pub fn greeting(name: &str) -> String {
String::from("Hello!")
}
Running this test produces the following:
$ cargo test
Compiling greeter v0.1.0 (file:///projects/greeter)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.91s
Running unittests src/lib.rs (target/debug/deps/greeter-170b942eb5bf5e3a)
running 1 test
test tests::greeting_contains_name ... FAILED
failures:
---- tests::greeting_contains_name stdout ----
thread 'tests::greeting_contains_name' panicked at src/lib.rs:12:9:
assertion failed: result.contains("Carol")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::greeting_contains_name
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
This result just indicates that the assertion failed and which line the assertion is on. A more useful failure message would print the value from the greeting
function. Let’s add a custom failure message composed of a format string with a placeholder filled in with the actual value we got from the greeting
function:
#[test]
fn greeting_contains_name() {
let result = greeting("Carol");
assert!(
result.contains("Carol"),
"Greeting did not contain name, value was `{result}`"
);
}
Now when we run the test, we’ll get a more informative error message:
$ cargo test
Compiling greeter v0.1.0 (file:///projects/greeter)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.93s
Running unittests src/lib.rs (target/debug/deps/greeter-170b942eb5bf5e3a)
running 1 test
test tests::greeting_contains_name ... FAILED
failures:
---- tests::greeting_contains_name stdout ----
thread 'tests::greeting_contains_name' panicked at src/lib.rs:12:9:
Greeting did not contain name, value was `Hello!`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::greeting_contains_name
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
Checking for Panics with should_panic
In addition to checking return values, it’s important to check that our code handles error conditions as we expect.
We do this by adding the attribute should_panic
to our test function. The test passes if the code inside the function panics; the test fails if the code inside the function doesn’t panic.
Filename: src/lib.rs
pub struct Guess {
value: i32,
}
impl Guess {
pub fn new(value: i32) -> Guess {
if value < 1 || value > 100 {
panic!("Guess value must be between 1 and 100, got {value}.");
}
Guess { value }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic]
fn greater_than_100() {
Guess::new(200);
}
}
We place the #[should_panic]
attribute after the #[test]
attribute and before the test function it applies to. Let’s look at the result when this test passes:
$ cargo test
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.58s
Running unittests src/lib.rs (target/debug/deps/guessing_game-57d70c3acb738f4d)
running 1 test
test tests::greater_than_100 - should panic ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests guessing_game
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Looks good! Now let’s introduce a bug in our code by removing the condition that the new
function will panic if the value is greater than 100:
impl Guess {
pub fn new(value: i32) -> Guess {
if value < 1 {
panic!("Guess value must be between 1 and 100, got {value}.");
}
Guess { value }
}
}
When we run the test, it will fail:
$ cargo test
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.62s
Running unittests src/lib.rs (target/debug/deps/guessing_game-57d70c3acb738f4d)
running 1 test
test tests::greater_than_100 - should panic ... FAILED
failures:
---- tests::greater_than_100 stdout ----
note: test did not panic as expected
failures:
tests::greater_than_100
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
We don’t get a very helpful message in this case, but when we look at the test function, we see that it’s annotated with #[should_panic]
. The failure we got means that the code in the test function did not cause a panic.
Tests that use should_panic
can be imprecise. A should_panic
test would pass even if the test panics for a different reason from the one we were expecting. To make should_panic
tests more precise, we can add an optional expected
parameter to the should_panic
attribute. The test harness will make sure that the failure message contains the provided text.
Filename: src/lib.rs
impl Guess {
pub fn new(value: i32) -> Guess {
if value < 1 {
panic!(
"Guess value must be greater than or equal to 1, got {value}."
);
} else if value > 100 {
panic!(
"Guess value must be less than or equal to 100, got {value}.",
);
}
Guess { value }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic(expected = "less than or equal to 100")]
fn greater_than_100() {
Guess::new(200);
}
}
This test will pass because the value we put in the should_panic
attribute’s expected
parameter is a substring of the message that the Guess::new
function panics with.
To see what happens when a should_panic
test with an expected
message fails, let’s again introduce a bug into our code by swapping the bodies of the if value < 1
and the else if value > 100
blocks:
if value < 1 {
panic!(
"Guess value must be less than or equal to 100, got {value}."
);
} else if value > 100 {
panic!(
"Guess value must be greater than or equal to 1, got {value}.",
);
}
This time when we run the should_panic
test, it will fail:
$ cargo test
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.66s
Running unittests src/lib.rs (target/debug/deps/guessing_game-57d70c3acb738f4d)
running 1 test
test tests::greater_than_100 - should panic ... FAILED
failures:
---- tests::greater_than_100 stdout ----
thread 'tests::greater_than_100' panicked at src/lib.rs:12:13:
Guess value must be greater than or equal to 1, got 200.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
note: panic did not contain expected string
panic message: `"Guess value must be greater than or equal to 1, got 200."`,
expected substring: `"less than or equal to 100"`
failures:
tests::greater_than_100
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
The failure message indicates that this test did indeed panic as we expected, but the panic message did not include the expected string 'Guess value must be less than or equal to 100'
. The panic message that we did get in this case was Guess value must be greater than or equal to 1, got 200.
. Now we can start figuring out where our bug is!
Using Result<T, E> in Tests
We can also write tests that use Result<T, E>
!
#[cfg(test)]
mod tests {
#[test]
fn it_works() -> Result<(), String> {
if 2 + 2 == 4 {
Ok(())
} else {
Err(String::from("two plus two does not equal four"))
}
}
}
Writing tests so they return a Result<T, E>
enables you to use the question mark operator in the body of tests, which can be a convenient way to write tests that should fail if any operation within them returns an Err
variant.
You can’t use the #[should_panic]
annotation on tests that use Result<T, E>
. To assert that an operation returns an Err
variant, don’t use the question mark operator on the Result<T, E>
value. Instead, use assert!(value.is_err())
.
Controlling How Tests Are Run
cargo test
compiles your code in test mode and runs the resulting test binary. The default behavior of the binary produced by cargo test is to run all the tests in parallel and capture output generated during test runs, preventing the output from being displayed and making it easier to read the output related to the test results. You can, however, specify command line options to change this default behavior.
Some command line options go to cargo test
, and some go to the resulting test binary. To separate these two types of arguments, you list the arguments that go to cargo test
followed by the separator --
and then the ones that go to the test binary. Running cargo test --help
displays the options you can use with cargo test
, and running cargo test -- --help
displays the options you can use after the separator.
Running Tests in Parallel or Consecutively
When you run multiple tests, by default they run in parallel using threads, meaning they finish running faster and you get feedback quicker. Because the tests are running at the same time, you must make sure your tests don’t depend on each other or on any shared state, including a shared environment, such as the current working directory or environment variables.
For example, say each of your tests runs some code that creates a file on disk named test-output.txt and writes some data to that file. Then each test reads the data in that file and asserts that the file contains a particular value, which is different in each test. Because the tests run at the same time, one test might overwrite the file in the time between another test writing and reading the file. The second test will then fail, not because the code is incorrect but because the tests have interfered with each other while running in parallel. One solution is to make sure each test writes to a different file; another solution is to run the tests one at a time.
If you don’t want to run the tests in parallel or if you want more fine-grained control over the number of threads used, you can send the --test-threads
flag and the number of threads you want to use to the test binary. Take a look at the following example:
cargo test -- --test-threads=1
We set the number of test threads to 1
, telling the program not to use any parallelism. Running the tests using one thread will take longer than running them in parallel, but the tests won’t interfere with each other if they share state.
Showing Function Output
By default, if a test passes, Rust’s test library captures anything printed to standard output. For example, if we call println!
in a test and the test passes, we won’t see the println!
output in the terminal; we’ll see only the line that indicates the test passed. If a test fails, we’ll see whatever was printed to standard output with the rest of the failure message.
Filename: src/lib.rs
fn prints_and_returns_10(a: i32) -> i32 {
println!("I got the value {a}");
10
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn this_test_will_pass() {
let value = prints_and_returns_10(4);
assert_eq!(value, 10);
}
#[test]
fn this_test_will_fail() {
let value = prints_and_returns_10(8);
assert_eq!(value, 5);
}
}
When we run these tests with cargo test
, we’ll see the following output:
$ cargo test
Compiling silly-function v0.1.0 (file:///projects/silly-function)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.58s
Running unittests src/lib.rs (target/debug/deps/silly_function-160869f38cff9166)
running 2 tests
test tests::this_test_will_fail ... FAILED
test tests::this_test_will_pass ... ok
failures:
---- tests::this_test_will_fail stdout ----
I got the value 8
thread 'tests::this_test_will_fail' panicked at src/lib.rs:19:9:
assertion `left == right` failed
left: 10
right: 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::this_test_will_fail
test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
Note that nowhere in this output do we see I got the value 4
, which is printed when the test that passes runs. That output has been captured. The output from the test that failed, I got the value 8
, appears in the section of the test summary output, which also shows the cause of the test failure.
If we want to see printed values for passing tests as well, we can tell Rust to also show the output of successful tests with --show-output
.
cargo test -- --show-output
$ cargo test -- --show-output
Compiling silly-function v0.1.0 (file:///projects/silly-function)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.60s
Running unittests src/lib.rs (target/debug/deps/silly_function-160869f38cff9166)
running 2 tests
test tests::this_test_will_fail ... FAILED
test tests::this_test_will_pass ... ok
successes:
---- tests::this_test_will_pass stdout ----
I got the value 4
successes:
tests::this_test_will_pass
failures:
---- tests::this_test_will_fail stdout ----
I got the value 8
thread 'tests::this_test_will_fail' panicked at src/lib.rs:19:9:
assertion `left == right` failed
left: 10
right: 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::this_test_will_fail
test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
Running a Subset of Tests by Name
Sometimes, running a full test suite can take a long time. If you’re working on code in a particular area, you might want to run only the tests pertaining to that code. You can choose which tests to run by passing cargo test
the name or names of the test(s) you want to run as an argument.
Filename: src/lib.rs
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_two_and_two() {
assert_eq!(4, add_two(2));
}
#[test]
fn add_three_and_two() {
assert_eq!(5, add_two(3));
}
#[test]
fn one_hundred() {
assert_eq!(102, add_two(100));
}
}
If we run the tests without passing any arguments, as we saw earlier, all the tests will run in parallel:
$ cargo test
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.62s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 3 tests
test tests::add_three_and_two ... ok
test tests::add_two_and_two ... ok
test tests::one_hundred ... ok
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running Single Tests
We can pass the name of any test function to cargo test
to run only that test:
$ cargo test one_hundred
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.69s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 1 test
test tests::one_hundred ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out; finished in 0.00s
The test output lets us know we had more tests that didn’t run by displaying 2 filtered
out at the end.
We can’t specify the names of multiple tests in this way; only the first value given to cargo test
will be used. But there is a way to run multiple tests.
Filtering to Run Multiple Tests
We can specify part of a test name, and any test whose name matches that value will be run. For example, because two of our tests’ names contain add
, we can run those two by running cargo test add
:
$ cargo test add
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.61s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 2 tests
test tests::add_three_and_two ... ok
test tests::add_two_and_two ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s
Also note that the module in which a test appears becomes part of the test’s name, so we can run all the tests in a module by filtering on the module’s name.
Ignoring Some Tests Unless Specifically Requested
Sometimes a few specific tests can be very time-consuming to execute, so you might want to exclude them during most runs of cargo test
. Rather than listing as arguments all tests you do want to run, you can instead annotate the time-consuming tests using the ignore
attribute to exclude them, as shown here:
Filename: src/lib.rs
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
#[test]
#[ignore]
fn expensive_test() {
}
}
After #[test]
we add the #[ignore]
line to the test we want to exclude. Now when we run our tests, it_works
runs, but expensive_test
doesn’t:
$ cargo test
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.60s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 2 tests
test tests::expensive_test ... ignored
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
The expensive_test
function is listed as ignored
. If we want to run only the ignored tests, we can use cargo test -- --ignored
:
$ cargo test -- --ignored
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.61s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 1 test
test expensive_test ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
By controlling which tests run, you can make sure your cargo test
results will be returned quickly. When you’re at a point where it makes sense to check the results of the ignored
tests and you have time to wait for the results, you can run cargo test -- --ignored
instead. If you want to run all tests whether they’re ignored or not, you can run cargo test -- --include-ignored
.
Test Organization
The Rust community thinks about tests in terms of two main categories: unit tests and integration tests. Unit tests are small and more focused, testing one module in isolation at a time, and can test private interfaces. Integration tests are entirely external to your library and use your code in the same way any other external code would, using only the public interface and potentially exercising multiple modules per test.
Unit Tests
The purpose of unit tests is to test each unit of code in isolation from the rest of the code to quickly pinpoint where code is and isn’t working as expected. You’ll put unit tests in the src directory in each file with the code that they’re testing. The convention is to create a module named tests
in each file to contain the test functions and to annotate the module with cfg(test)
.
The Tests Module and #[cfg(test)]
The #[cfg(test)]
annotation on the tests module tells Rust to compile and run the test code only when you run cargo test
, not when you run cargo build
. This saves compile time when you only want to build the library and saves space in the resulting compiled artifact because the tests are not included. You’ll see that because integration tests go in a different directory, they don’t need the #[cfg(test)]
annotation. However, because unit tests go in the same files as the code, you’ll use #[cfg(test)]
to specify that they shouldn’t be included in the compiled result.
The attribute cfg
stands for configuration and tells Rust that the following item should only be included given a certain configuration option. In this case, the configuration option is test
, which is provided by Rust for compiling and running tests. By using the cfg
attribute, Cargo compiles our test code only if we actively run the tests with cargo test
. This includes any helper functions that might be within this module, in addition to the functions annotated with #[test]
.
Testing Private Functions
Rust’s privacy rules do allow you to test private functions.
Filename: src/lib.rs
pub fn add_two(a: usize) -> usize {
internal_adder(a, 2)
}
fn internal_adder(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn internal() {
let result = internal_adder(2, 2);
assert_eq!(result, 4);
}
}
Note that the internal_adder
function is not marked as pub
. Tests are just Rust code, and the tests
module is just another module. Items in child modules can use the items in their ancestor modules. In this test, we bring all of the tests
module’s parent’s items into scope with use super::*
, and then the test can call internal_adder
.
Integration Tests
In Rust, integration tests are entirely external to your library. They use your library in the same way any other code would, which means they can only call functions that are part of your library’s public API. Their purpose is to test whether many parts of your library work together correctly. Units of code that work correctly on their own could have problems when integrated, so test coverage of the integrated code is important as well. To create integration tests, you first need a tests directory.
The tests Directory
We create a tests directory at the top level of our project directory, next to src. Cargo knows to look for integration test files in this directory. We can then make as many test files as we want, and Cargo will compile each of the files as an individual crate.
Let’s create an integration test. Your directory structure should look like this:
adder
├── Cargo.lock
├── Cargo.toml
├── src
│ └── lib.rs
└── tests
└── integration_test.rs
Filename: tests/integration_test.rs
use adder;
#[test]
fn it_adds_two() {
assert_eq!(4, adder::add_two(2));
}
Each file in the tests directory is a separate crate, so we need to bring our library into each test crate’s scope. For that reason we add use adder
at the top of the code, which we didn’t need in the unit tests.
We don’t need to annotate any code in tests/integration_test.rs with #[cfg(test)]
. Cargo treats the tests
directory specially and compiles files in this directory only when we run cargo test
. Run cargo test
now:
$ cargo test
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 1.31s
Running unittests src/lib.rs (target/debug/deps/adder-1082c4b063a8fbe6)
running 1 test
test tests::internal ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running tests/integration_test.rs (target/debug/deps/integration_test-1082c4b063a8fbe6)
running 1 test
test it_adds_two ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
The three sections of output include the unit tests, the integration test, and the doc tests. Note that if any test in a section fails, the following sections will not be run. For example, if a unit test fails, there won’t be any output for integration and doc tests because those tests will only be run if all unit tests are passing.
The first section for the unit tests is the same as we’ve been seeing: one line for each unit test and then a summary line for the unit tests.
The integration tests section starts with the line Running tests/integration_test.rs
. Next, there is a line for each test function in that integration test and a summary line for the results of the integration test just before the Doc-tests adder
section starts.
Each integration test file has its own section, so if we add more files in the tests directory, there will be more integration test sections.
We can still run a particular integration test function by specifying the test function’s name as an argument to cargo test
. To run all the tests in a particular integration test file, use the --test
argument of cargo test
followed by the name of the file:
$ cargo test --test integration_test
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.64s
Running tests/integration_test.rs (target/debug/deps/integration_test-82e7799c1bc62298)
running 1 test
test it_adds_two ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Submodules in Integration Tests
As mentioned earlier, each file in the tests directory is compiled as its own separate crate, which is useful for creating separate scopes to more closely imitate the way end users will be using your crate. However, this means files in the tests directory don’t share the same behavior as files in src do, as you learned in previous chapter regarding how to separate code into modules and files.
The different behavior of tests directory files is most noticeable when you have a set of helper functions to use in multiple integration test files and you try to follow the steps in previous chapter to extract them into a common module. For example, if we create tests/common.rs and place a function named setup
in it, we can add some code to setup
that we want to call from multiple test functions in multiple test files:
Filename: tests/common.rs
pub fn setup() {
}
When we run the tests again, we’ll see a new section in the test output for the common.rs file, even though this file doesn’t contain any test functions nor did we call the setup
function from anywhere:
$ cargo test
Compiling adder v0.1.0 (file:/
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.89s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 1 test
test tests::internal ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running tests/common.rs (target/debug/deps/common-92948b65e88960b4)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running tests/integration_test.rs (target/debug/deps/integration_test-92948b65e88960b4)
running 1 test
test it_adds_two ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Having common
appear in the test results with running 0 tests
displayed for it is not what we wanted. We just wanted to share some code with the other integration test files.
To avoid having common
appear in the test output, instead of creating tests/common.rs, we’ll create tests/common/mod.rs. The project directory now looks like this:
├── Cargo.lock
├── Cargo.toml
├── src
│ └── lib.rs
└── tests
├── common
│ └── mod.rs
└── integration_test.rs
This is the older naming convention that Rust also understands. Naming the file this way tells Rust not to treat the common
module as an integration test file. Files in subdirectories of the tests directory don’t get compiled as separate crates or have sections in the test output.
After we’ve created tests/common/mod.rs, we can use it from any of the integration test files as a module.
Filename: tests/integration_test.rs
use adder;
mod common;
#[test]
fn it_adds_two() {
common::setup();
assert_eq!(4, adder::add_two(2));
}
Integration Tests for Binary Crates
If our project is a binary crate that only contains a src/main.rs file and doesn’t have a src/lib.rs file, we can’t create integration tests in the tests directory and bring functions defined in the src/main.rs file into scope with a use
statement. Only library crates expose functions that other crates can use; binary crates are meant to be run on their own.
This is one of the reasons Rust projects that provide a binary have a straightforward src/main.rs file that calls logic that lives in the src/lib.rs file. Using that structure, integration tests can test the library crate with use
to make the important functionality available. If the important functionality works, the small amount of code in the src/main.rs file will work as well, and that small amount of code doesn’t need to be tested.
An I/O Project: Building a Command Line Program
Accepting Command Line Arguments
$ cargo new minigrep
Created binary (application) `minigrep` project
$ cd minigrep
The first task is to make minigrep
accept its two command line arguments: the file path and a string to search for. That is, we want to be able to run our program with cargo run
, two hyphens to indicate the following arguments are for our program rather than for cargo
, a string to search for, and a path to a file to search in, like so:
cargo run -- searchstring example-filename.txt
Reading the Argument Values
To enable minigrep
to read the values of command line arguments we pass to it, we’ll need the std::env::args
function provided in Rust’s standard library. This function returns an iterator of the command line arguments passed to minigrep
. For now, you only need to know two details about iterators: iterators produce a series of values, and we can call the collect
method on an iterator to turn it into a collection, such as a vector, that contains all the elements the iterator produces.
Filename: src/main.rs
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
dbg!(args);
}
The args Function and Invalid Unicode
Note that std::env::args
will panic if any argument contains invalid Unicode. If your program needs to accept arguments containing invalid Unicode, use std::env::args_os
instead. That function returns an iterator that produces OsString
values instead of String
values. We’ve chosen to use std::env::args
here for simplicity, because OsString
values differ per platform and are more complex to work with than String
values.
We can use the collect
function to create many kinds of collections, so we explicitly annotate the type of args
to specify that we want a vector of strings. Although we very rarely need to annotate types in Rust, collect
is one function you do often need to annotate because Rust isn’t able to infer the kind of collection you want.
Let’s try running the code first with no arguments and then with two arguments:
$ cargo run
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.61s
Running `target/debug/minigrep`
[src/main.rs:5:5] args = [
"target/debug/minigrep",
]
$ cargo run -- needle haystack
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.57s
Running `target/debug/minigrep needle haystack`
[src/main.rs:5:5] args = [
"target/debug/minigrep",
"needle",
"haystack",
]
Notice that the first value in the vector is "target/debug/minigrep"
, which is the name of our binary. This matches the behavior of the arguments list in C, letting programs use the name by which they were invoked in their execution. It’s often convenient to have access to the program name in case you want to print it in messages or change behavior of the program based on what command line alias was used to invoke the program.
Saving the Argument Values in Variables
Now we need to save the values of the two arguments in variables so we can use the values throughout the rest of the program.
Filename: src/main.rs
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let query = &args[1];
let file_path = &args[2];
println!("Searching for {query}");
println!("In file {file_path}");
}
We temporarily print the values of these variables to prove that the code is working as we intend. Let’s run this program again with the arguments test
and sample.txt
:
$ cargo run -- test sample.txt
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
Running `target/debug/minigrep test sample.txt`
Searching for test
In file sample.txt
Reading a File
Now we’ll add functionality to read the file specified in the file_path
argument. First, we need a sample file to test it with: we’ll use a file with a small amount of text over multiple lines with some repeated words. Create a file called poem.txt at the root level of your project, and enter the poem “I’m Nobody! Who are you?”
Filename: poem.txt
I'm nobody! Who are you?
Are you nobody, too?
Then there's a pair of us - don't tell!
They'd banish us, you know.
How dreary to be somebody!
How public, like a frog
To tell your name the livelong day
To an admiring bog!
With the text in place, edit src/main.rs and add code to read the file.
Filename: src/main.rs
use std::env;
use std::fs;
fn main() {
println!("In file {file_path}");
let contents = fs::read_to_string(file_path)
.expect("Should have been able to read the file");
println!("With text:\n{contents}");
}
In main
, the new statement fs::read_to_string
takes the file_path
, opens that file, and returns a value of type std::io::Result<String>
that contains the file’s contents.
Let’s run this code with any string as the first command line argument (because we haven’t implemented the searching part yet) and the poem.txt file as the second argument:
$ cargo run -- the poem.txt
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
Running `target/debug/minigrep the poem.txt`
Searching for the
In file poem.txt
With text:
I'm nobody! Who are you?
Are you nobody, too?
Then there's a pair of us - don't tell!
They'd banish us, you know.
How dreary to be somebody!
How public, like a frog
To tell your name the livelong day
To an admiring bog!
The code has a few flaws. At the moment, the main
function has multiple responsibilities: generally, functions are clearer and easier to maintain if each function is responsible for only one idea. The other problem is that we’re not handling errors as well as we could. The program is still small, so these flaws aren’t a big problem, but as the program grows, it will be harder to fix them cleanly. It’s good practice to begin refactoring early on when developing a program, because it’s much easier to refactor smaller amounts of code.
Refactoring to Improve Modularity and Error Handling
Separation of Concerns for Binary Projects
The Rust community has developed guidelines for splitting the separate concerns of a binary program when main
starts getting large. This process has the following steps:
Split your program into a main.rs and a lib.rs and move your program’s logic to lib.rs.
As long as your command line parsing logic is small, it can remain in main.rs.
When the command line parsing logic starts getting complicated, extract it from main.rs and move it to lib.rs.
The responsibilities that remain in the main
function after this process should be limited to the following:
Calling the command line parsing logic with the argument values
Setting up any other configuration
Calling a run
function in lib.rs
Handling the error if run
returns an error
This pattern is about separating concerns: main.rs handles running the program, and lib.rs handles all the logic of the task at hand. Because you can’t test the main
function directly, this structure lets you test all of your program’s logic by moving it into functions in lib.rs. The code that remains in main.rs will be small enough to verify its correctness by reading it.
We’ll extract the functionality for parsing arguments into a function that main
will call to prepare for moving the command line parsing logic to src/lib.rs.
Filename: src/main.rs
fn main() {
let args: Vec<String> = env::args().collect();
let (query, file_path) = parse_config(&args);
}
fn parse_config(args: &[String]) -> (&str, &str) {
let query = &args[1];
let file_path = &args[2];
(query, file_path)
}
Grouping Configuration Values
At the moment, we’re returning a tuple, but then we immediately break that tuple into individual parts again. This is a sign that perhaps we don’t have the right abstraction yet.
We’ll instead put the two values into one struct and give each of the struct fields a meaningful name. Doing so will make it easier for future maintainers of this code to understand how the different values relate to each other and what their purpose is.
Filename: src/main.rs
fn main() {
let args: Vec<String> = env::args().collect();
let config = parse_config(&args);
println!("Searching for {}", config.query);
println!("In file {}", config.file_path);
let contents = fs::read_to_string(config.file_path)
.expect("Should have been able to read the file");
}
struct Config {
query: String,
file_path: String,
}
fn parse_config(args: &[String]) -> Config {
let query = args[1].clone();
let file_path = args[2].clone();
Config { query, file_path }
}
The args
variable in main
is the owner of the argument values and is only letting the parse_config
function borrow them, which means we’d violate Rust’s borrowing rules if Config
tried to take ownership of the values in args
.
There are a number of ways we could manage the String
data; the easiest, though somewhat inefficient, route is to call the clone
method on the values.
The Trade-Offs of Using clone
There’s a tendency among many Rustaceans to avoid using clone
to fix ownership problems because of its runtime cost. But for now, it’s better to have a working program that’s a bit inefficient than to try to hyperoptimize code on your first pass.
Creating a Constructor for Config
So now that the purpose of the parse_config
function is to create a Config
instance, we can change parse_config
from a plain function to a function named new
that is associated with the Config
struct. Making this change will make the code more idiomatic.
Filename: src/main.rs
fn main() {
let args: Vec<String> = env::args().collect();
let config = Config::new(&args);
}
impl Config {
fn new(args: &[String]) -> Config {
let query = args[1].clone();
let file_path = args[2].clone();
Config { query, file_path }
}
}
Fixing the Error Handling
Recall that attempting to access the values in the args
vector at index 1 or index 2 will cause the program to panic if the vector contains fewer than three items.
Try running the program without any arguments. The line index out of bounds: the len is 1 but the index is 1
is an error message intended for programmers. It won’t help our end users understand what they should do instead. Let’s fix that now.
Improving the Error Message
We add a check in the new
function that will verify that the slice is long enough before accessing index 1 and 2.
Filename: src/main.rs
fn new(args: &[String]) -> Config {
if args.len() < 3 {
panic!("not enough arguments");
}
$ cargo run
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
Running `target/debug/minigrep`
thread 'main' panicked at src/main.rs:26:13:
not enough arguments
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
A call to panic!
is more appropriate for a programming problem than a usage problem.
Returning a Result Instead of Calling panic!
We can instead return a Result
value that will contain a Config
instance in the successful case and will describe the problem in the error case. We’re also going to change the function name from new
to build
because many programmers expect new
functions to never fail.
Filename: src/main.rs
impl Config {
fn build(args: &[String]) -> Result<Config, &'static str> {
if args.len() < 3 {
return Err("not enough arguments");
}
let query = args[1].clone();
let file_path = args[2].clone();
Ok(Config { query, file_path })
}
}
Our error values will always be string literals that have the 'static
lifetime.
Calling Config::build and Handling Errors
To handle the error case and print a user-friendly message, we need to update main
to handle the Result
being returned by Config::build
. We’ll also take the responsibility of exiting the command line tool with a nonzero error code away from panic!
and instead implement it by hand. A nonzero exit status is a convention to signal to the process that called our program that the program exited with an error state.
Filename: src/main.rs
use std::process;
fn main() {
let args: Vec<String> = env::args().collect();
let config = Config::build(&args).unwrap_or_else(|err| {
println!("Problem parsing arguments: {err}");
process::exit(1);
});
Using unwrap_or_else
allows us to define some custom, non-panic!
error handling. If the Result
is an Ok
value, this method’s behavior is similar to unwrap
: it returns the inner value Ok
is wrapping. However, if the value is an Err
value, this method calls the code in the closure, which is an anonymous function we define and pass as an argument to unwrap_or_else
.
The process::exit
function will stop the program immediately and return the number that was passed as the exit status code. This is similar to the panic!
-based handling we used, but we no longer get all the extra output. Let’s try it:
$ cargo run
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished dev [unoptimized + debuginfo] target(s) in 0.48s
Running `target/debug/minigrep`
Problem parsing arguments: not enough arguments
Extracting Logic from main
We’ll extract a function named run
that will hold all the logic currently in the main
function that isn’t involved with setting up configuration or handling errors.
For now, we’re just making the small, incremental improvement of extracting the function. We’re still defining the function in src/main.rs.
Filename: src/main.rs
fn main() {
println!("Searching for {}", config.query);
println!("In file {}", config.file_path);
run(config);
}
fn run(config: Config) {
let contents = fs::read_to_string(config.file_path)
.expect("Should have been able to read the file");
println!("With text:\n{contents}");
}
Returning Errors from the run Function
Instead of allowing the program to panic by calling expect
, the run
function will return a Result<T, E>
when something goes wrong.
Filename: src/main.rs
use std::error::Error;
fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;
println!("With text:\n{contents}");
Ok(())
}
For now, just know that Box<dyn Error>
means the function will return a type that implements the Error
trait, but we don’t have to specify what particular type the return value will be. This gives us flexibility to return error values that may be of different types in different error cases. The dyn
keyword is short for “dynamic.”
We’ve removed the call to expect
in favor of the ?
operator. Rather than panic!
on an error, ?
will return the error value from the current function for the caller to handle.
This Ok(())
syntax might look a bit strange at first, but using ()
like this is the idiomatic way to indicate that we’re calling run
for its side effects only; it doesn’t return a value we need.
When you run this code, it will compile but will display a warning:
$ cargo run -- the poem.txt
Compiling minigrep v0.1.0 (file:///projects/minigrep)
warning: unused `Result` that must be used
--> src/main.rs:19:5
|
19 | run(config);
| ^^^^^^^^^^^
|
= note: this `Result` may be an `Err` variant, which should be handled
= note: `` on by default
help: use `let _ = ...` to ignore the resulting value
|
19 | let _ = run(config);
| +++++++
warning: `minigrep` (bin "minigrep") generated 1 warning
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.71s
Running `target/debug/minigrep the poem.txt`
Searching for the
In file poem.txt
With text:
I'm nobody! Who are you?
Are you nobody, too?
Then there's a pair of us - don't tell!
They'd banish us, you know.
How dreary to be somebody!
How public, like a frog
To tell your name the livelong day
To an admiring bog!
Rust tells us that our code ignored the Result value and the Result value might indicate that an error occurred. But we’re not checking to see whether or not there was an error, and the compiler reminds us that we probably meant to have some error-handling code here!
Handling Errors Returned from run in main
Filename: src/main.rs
fn main() {
println!("Searching for {}", config.query);
println!("In file {}", config.file_path);
if let Err(e) = run(config) {
println!("Application error: {e}");
process::exit(1);
}
}
We use if let
rather than unwrap_or_else
to check whether run
returns an Err
value and call process::exit(1)
if it does. The run
function doesn’t return a value that we want to unwrap
in the same way that Config::build
returns the Config
instance. Because run
returns ()
in the success case, we only care about detecting an error, so we don’t need unwrap_or_else
to return the unwrapped value, which would only be ()
.
Splitting Code into a Library Crate
Let’s move all the code that isn’t the main
function from src/main.rs to src/lib.rs:
The run
function definition
The relevant use
statements
The definition of Config
The Config::build
function definition
Filename: src/lib.rs
use std::error::Error;
use std::fs;
pub struct Config {
pub query: String,
pub file_path: String,
}
impl Config {
pub fn build(args: &[String]) -> Result<Config, &'static str> {
}
}
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
}
Now we need to bring the code we moved to src/lib.rs into the scope of the binary crate in src/main.rs.
Filename: src/main.rs
use std::env;
use std::process;
use minigrep::Config;
fn main() {
if let Err(e) = minigrep::run(config) {
}
}
Developing the Library’s Functionality with Test-Driven Development
In this section, we’ll add the searching logic to the minigrep
program using the test-driven development (TDD) process with the following steps:
Write a test that fails and run it to make sure it fails for the reason you expect.
Write or modify just enough code to make the new test pass.
Refactor the code you just added or changed and make sure the tests continue to pass.
Repeat from step 1!
Writing a Failing Test
Because we don’t need them anymore, let’s remove the println!
statements from src/lib.rs and src/main.rs that we used to check the program’s behavior. Then, in src/lib.rs, add a tests
module with a test function. The test function specifies the behavior we want the search
function to have: it will take a query and the text to search, and it will return only the lines from the text that contain the query. The following listing shows this test, which won’t compile yet.
Filename: src/lib.rs
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_result() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick three.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
}
}
Note that the backslash after the opening double quote tells Rust not to put a newline character at the beginning of the contents of this string literal.
In accordance with TDD principles, we’ll add just enough code to get the test to compile and run by adding a definition of the search
function that always returns an empty vector.
Filename: src/lib.rs
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
vec![]
}
Notice that we need to define an explicit lifetime 'a
in the signature of search
and use that lifetime with the contents
argument and the return value.
In other words, we tell Rust that the data returned by the search
function will live as long as the data passed into the search
function in the contents argument.
Now let’s run the test:
$ cargo test
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.97s
Running unittests src/lib.rs (target/debug/deps/minigrep-9cd200e5fac0fc94)
running 1 test
test tests::one_result ... FAILED
failures:
---- tests::one_result stdout ----
thread 'tests::one_result' panicked at src/lib.rs:44:9:
assertion `left == right` failed
left: ["safe, fast, productive."]
right: []
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::one_result
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
Great, the test fails, exactly as we expected. Let’s get the test to pass!
Writing Code to Pass the Test
Currently, our test is failing because we always return an empty vector. To fix that and implement search
, our program needs to follow these steps:
Iterate through each line of the contents.
Check whether the line contains our query string.
If it does, add it to the list of values we’re returning.
If it doesn’t, do nothing.
Return the list of results that match.
Iterating Through Lines with the lines Method
Rust has a helpful method to handle line-by-line iteration of strings, conveniently named lines
. Note this won’t compile yet.
Filename: src/lib.rs
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
for line in contents.lines() {
}
}
Searching Each Line for the Query
Fortunately, strings have a helpful method named contains
that check whether the current line contains our query string for us! Note this still won’t compile yet.
Filename: src/lib.rs
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
for line in contents.lines() {
if line.contains(query) {
}
}
}
Storing Matching Lines
To finish this function, we need a way to store the matching lines that we want to return. For that, we can make a mutable vector before the for
loop and call the push
method to store a line
in the vector. After the for
loop, we return the vector, as shown in the following listing.
Filename: src/lib.rs
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let mut results = Vec::new();
for line in contents.lines() {
if line.contains(query) {
results.push(line);
}
}
results
}
Now the search
function should return only the lines that contain query
, and our test should pass. Let’s run the test:
$ cargo test
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished `test` profile [unoptimized + debuginfo] target(s) in 1.22s
Running unittests src/lib.rs (target/debug/deps/minigrep-9cd200e5fac0fc94)
running 1 test
test tests::one_result ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running unittests src/main.rs (target/debug/deps/minigrep-9cd200e5fac0fc94)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests minigrep
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
At this point, we could consider opportunities for refactoring the implementation of the search function while keeping the tests passing to maintain the same functionality.
Using the search Function in the run Function
We need to pass the config.query
value and the contents
that run
reads from the file to the search
function. Then run
will print each line returned from search
:
Filename: src/lib.rs
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;
for line in search(&config.query, &contents) {
println!("{line}");
}
Ok(())
}
Now the entire program should work! Let’s try it out:
$ cargo run -- frog poem.txt
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.38s
Running `target/debug/minigrep frog poem.txt`
How public, like a frog
$ cargo run -- body poem.txt
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
Running `target/debug/minigrep body poem.txt`
I'm nobody! Who are you?
Are you nobody, too?
How dreary to be somebody!
And finally, let’s make sure that we don’t get any lines when we search for a word that isn’t anywhere in the poem:
$ cargo run -- monomorphization poem.txt
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
Running `target/debug/minigrep monomorphization poem.txt`
Working with Environment Variables
We’ll improve minigrep
by adding an extra feature: an option for case-insensitive searching that the user can turn on via an environment variable.
Writing a Failing Test for the Case-Insensitive search Function
We first add a new search_case_insensitive
function that will be called when the environment variable has a value. We’ll continue to follow the TDD process, so the first step is again to write a failing test. We’ll add a new test for the new search_case_insensitive
function and rename our old test from one_result
to case_sensitive
to clarify the differences between the two tests.
Filename: src/lib.rs
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn case_sensitive() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Duct tape.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
}
#[test]
fn case_insensitive() {
let query = "rUsT";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Trust me.";
assert_eq!(
vec!["Rust:", "Trust me."],
search_case_insensitive(query, contents)
);
}
}
Implementing the search_case_insensitive Function
The search_case_insensitive
function will be almost the same as the search
function. The only difference is that we’ll lowercase the query
and each line
so whatever the case of the input arguments, they’ll be the same case when we check whether the line contains the query.
Filename: src/lib.rs
pub fn search_case_insensitive<'a>(
query: &str,
contents: &'a str,
) -> Vec<&'a str> {
let query = query.to_lowercase();
let mut results = Vec::new();
for line in contents.lines() {
if line.to_lowercase().contains(&query) {
results.push(line);
}
}
results
}
While to_lowercase
will handle basic Unicode, it won’t be 100% accurate. If we were writing a real application, we’d want to do a bit more work here, but this section is about environment variables, not Unicode, so we’ll leave it at that here.
Note that query
is now a String
rather than a string slice, because calling to_lowercase
creates new data rather than referencing existing data. When we pass query
as an argument to the contains
method now, we need to add an ampersand because the signature of contains
is defined to take a string slice.
Now, let’s call the new search_case_insensitive
function from the run
function. First, we’ll add a configuration option to the Config
struct to switch between case-sensitive and case-insensitive search. Adding this field will cause compiler errors because we aren’t initializing this field anywhere yet:
Filename: src/lib.rs
pub struct Config {
pub query: String,
pub file_path: String,
pub ignore_case: bool,
}
Next, we need the run
function to check the ignore_case
field’s value and use that to decide whether to call the search
function or the search_case_insensitive
function. This still won’t compile yet.
Filename: src/lib.rs
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;
let results = if config.ignore_case {
search_case_insensitive(&config.query, &contents)
} else {
search(&config.query, &contents)
};
for line in results {
println!("{line}");
}
Ok(())
}
Finally, we need to check for the environment variable. The functions for working with environment variables are in the env
module in the standard library, so we bring that module into scope at the top of src/lib.rs. Then we’ll use the var
function from the env
module to check to see if any value has been set for an environment variable named IGNORE_CASE
.
Filename: src/lib.rs
use std::env;
impl Config {
pub fn build(args: &[String]) -> Result<Config, &'static str> {
if args.len() < 3 {
return Err("not enough arguments");
}
let query = args[1].clone();
let file_path = args[2].clone();
let ignore_case = env::var("IGNORE_CASE").is_ok();
Ok(Config {
query,
file_path,
ignore_case,
})
}
}
The env::var
function returns a Result
that will be the successful Ok
variant that contains the value of the environment variable if the environment variable is set to any value. It will return the Err
variant if the environment variable is not set.
If the IGNORE_CASE
environment variable isn’t set to anything, is_ok
will return false
and the program will perform a case-sensitive search. We don’t care about the value of the environment variable, just whether it’s set or unset, so we’re checking is_ok
rather than using unwrap
, expect
, or any of the other methods we’ve seen on Result
.
Let’s give it a try! First, we’ll run our program without the environment variable set and with the query to
, which should match any line that contains the word “to” in all lowercase:
$ cargo run -- to poem.txt
Compiling minigrep v0.1.0 (file:///projects/minigrep)
Finished dev [unoptimized + debuginfo] target(s) in 0.0s
Running `target/debug/minigrep to poem.txt`
Are you nobody, too?
How dreary to be somebody!
Now, let’s run the program with IGNORE_CASE
set to 1
but with the same query to.
IGNORE_CASE=1 cargo run -- to poem.txt
If you’re using PowerShell, you will need to set the environment variable and run the program as separate commands:
PS> $Env:IGNORE_CASE=1; cargo run -- to poem.txt
This will make IGNORE_CASE
persist for the remainder of your shell session. It can be unset with the Remove-Item
cmdlet:
PS> Remove-Item Env:IGNORE_CASE
We should get lines that contain to that might have uppercase letters:
Are you nobody, too?
How dreary to be somebody!
To tell your name the livelong day
To an admiring bog!
Some programs allow arguments and environment variables for the same configuration. In those cases, the programs decide that one or the other takes precedence. For another exercise on your own, try controlling case sensitivity through either a command line argument or an environment variable. Decide whether the command line argument or the environment variable should take precedence if the program is run with one set to case sensitive and one set to ignore case.
Writing Error Messages to Standard Error Instead of Standard Output
In most terminals, there are two kinds of output: standard output (stdout
) for general information and standard error (stderr
) for error messages. This distinction enables users to choose to direct the successful output of a program to a file but still print error messages to the screen.
The println!
macro is only capable of printing to standard output, so we have to use something else to print to standard error.
Checking Where Errors Are Written
Command line programs are expected to send error messages to the standard error stream so we can still see error messages on the screen even if we redirect the standard output stream to a file. Our program is not currently well behaved: we’re about to see that it saves the error message output to a file instead!
To demonstrate this behavior, we’ll run the program with >
and the file path, output.txt, that we want to redirect the standard output stream to. We won’t pass any arguments, which should cause an error:
cargo run > output.txt
The >
syntax tells the shell to write the contents of standard output to output.txt instead of the screen. We didn’t see the error message we were expecting printed to the screen, so that means it must have ended up in the file. This is what output.txt contains:
Problem parsing arguments: not enough arguments
Printing Errors to Standard Error
The standard library provides the eprintln!
macro that prints to the standard error stream, so let’s change the two places we were calling println!
to print errors to use eprintln!
instead.
Filename: src/main.rs
fn main() {
let args: Vec<String> = env::args().collect();
let config = Config::build(&args).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {err}");
process::exit(1);
});
if let Err(e) = minigrep::run(config) {
eprintln!("Application error: {e}");
process::exit(1);
}
}
Let’s now run the program again in the same way, without any arguments and redirecting standard output with >
:
$ cargo run > output.txt
Problem parsing arguments: not enough arguments
Now we see the error onscreen and output.txt contains nothing, which is the behavior we expect of command line programs.
Let’s run the program again with arguments that don’t cause an error but still redirect standard output to a file, like so:
cargo run -- to poem.txt > output.txt
We won’t see any output to the terminal, and output.txt will contain our results:
Filename: output.txt
Are you nobody, too?
How dreary to be somebody!
Functional Language Features: Iterators and Closures
Closures: Anonymous Functions that Capture Their Environment
Rust’s closures are anonymous functions you can save in a variable or pass as arguments to other functions. You can create the closure in one place and then call the closure elsewhere to evaluate it in a different context.
Unlike functions, closures can capture values from the scope in which they’re defined.
Capturing the Environment with Closures
We’ll first examine how we can use closures to capture values from the environment they’re defined in for later use.
Here’s the scenario: Every so often, our t-shirt company gives away an exclusive, limited-edition shirt to someone on our mailing list as a promotion. People on the mailing list can optionally add their favorite color to their profile. If the person chosen for a free shirt has their favorite color set, they get that color shirt. If the person hasn’t specified a favorite color, they get whatever color the company currently has the most of.
Filename: src/main.rs
#[derive(Debug, PartialEq, Copy, Clone)]
enum ShirtColor {
Red,
Blue,
}
struct Inventory {
shirts: Vec<ShirtColor>,
}
impl Inventory {
fn giveaway(&self, user_preference: Option<ShirtColor>) -> ShirtColor {
user_preference.unwrap_or_else(|| self.most_stocked())
}
fn most_stocked(&self) -> ShirtColor {
let mut num_red = 0;
let mut num_blue = 0;
for color in &self.shirts {
match color {
ShirtColor::Red => num_red += 1,
ShirtColor::Blue => num_blue += 1,
}
}
if num_red > num_blue {
ShirtColor::Red
} else {
ShirtColor::Blue
}
}
}
fn main() {
let store = Inventory {
shirts: vec![ShirtColor::Blue, ShirtColor::Red, ShirtColor::Blue],
};
let user_pref1 = Some(ShirtColor::Red);
let giveaway1 = store.giveaway(user_pref1);
println!(
"The user with preference {:?} gets {:?}",
user_pref1, giveaway1
);
let user_pref2 = None;
let giveaway2 = store.giveaway(user_pref2);
println!(
"The user with preference {:?} gets {:?}",
user_pref2, giveaway2
);
}
We specify the closure expression || self.most_stocked()
as the argument to unwrap_or_else
. This is a closure that takes no parameters itself (if the closure had parameters, they would appear between the two vertical bars). The body of the closure calls self.most_stocked()
.
The closure captures an immutable reference to the self
Inventory
instance and passes it with the code we specify to the unwrap_or_else
method. Functions, on the other hand, are not able to capture their environment in this way.
Closure Type Inference and Annotation
There are more differences between functions and closures. Closures don’t usually require you to annotate the types of the parameters or the return value like fn
functions do. Type annotations are required on functions because the types are part of an explicit interface exposed to your users.
Closures are typically short and relevant only within a narrow context rather than in any arbitrary scenario. Within these limited contexts, the compiler can infer the types of the parameters and the return type, similar to how it’s able to infer the types of most variables (there are rare cases where the compiler needs closure type annotations too).
As with variables, we can add type annotations if we want to increase explicitness and clarity at the cost of being more verbose than is strictly necessary.
Filename: src/main.rs
let expensive_closure = |num: u32| -> u32 {
println!("calculating slowly...");
thread::sleep(Duration::from_secs(2));
num
};
With type annotations added, the syntax of closures looks more similar to the syntax of functions. Here we define a function that adds 1 to its parameter and a closure that has the same behavior, for comparison.
fn add_one_v1 (x: u32) -> u32 { x + 1 }
let add_one_v2 = |x: u32| -> u32 { x + 1 };
let add_one_v3 = |x| { x + 1 };
let add_one_v4 = |x| x + 1 ;
The add_one_v3
and add_one_v4
lines require the closures to be evaluated to be able to compile because the types will be inferred from their usage. This is similar to let v = Vec::new()
; needing either type annotations or values of some type to be inserted into the Vec
for Rust to be able to infer the type.
We can call the closure with any type, which we’ve done here with String
the first time. If we then try to call example_closure
with an integer, we’ll get an error.
Filename: src/main.rs
let example_closure = |x| x;
let s = example_closure(String::from("hello"));
let n = example_closure(5);
The first time we call example_closure
with the String
value, the compiler infers the type of x
and the return type of the closure to be String
. Those types are then locked into the closure in example_closure
, and we get a type error when we next try to use a different type with the same closure.
Capturing References or Moving Ownership
Closures can capture values from their environment in three ways, which directly map to the three ways a function can take a parameter: borrowing immutably, borrowing mutably, and taking ownership. The closure will decide which of these to use based on what the body of the function does with the captured values.
In the following listing, we define a closure that captures an immutable reference to the vector named list because it only needs an immutable reference to print the value:
Filename: src/main.rs
fn main() {
let list = vec![1, 2, 3];
println!("Before defining closure: {:?}", list);
let only_borrows = || println!("From closure: {:?}", list);
println!("Before calling closure: {:?}", list);
only_borrows();
println!("After calling closure: {:?}", list);
}
This example also illustrates that a variable can bind to a closure definition, and we can later call the closure by using the variable name and parentheses as if the variable name were a function name.
Next, in the following listing, we change the closure body so that it adds an element to the list
vector. The closure now captures a mutable reference:
Filename: src/main.rs
fn main() {
let mut list = vec![1, 2, 3];
println!("Before defining closure: {:?}", list);
let mut borrows_mutably = || list.push(7);
borrows_mutably();
println!("After calling closure: {:?}", list);
}
Note that there’s no longer a println!
between the definition and the call of the borrows_mutably
closure: when borrows_mutably
is defined, it captures a mutable reference to list
. We don’t use the closure again after the closure is called, so the mutable borrow ends. Between the closure definition and the closure call, an immutable borrow to print isn’t allowed because no other borrows are allowed when there’s a mutable borrow.
If you want to force the closure to take ownership of the values it uses in the environment even though the body of the closure doesn’t strictly need ownership, you can use the move
keyword before the parameter list.
This technique is mostly useful when passing a closure to a new thread to move the data so that it’s owned by the new thread.
Filename: src/main.rs
use std::thread;
fn main() {
let list = vec![1, 2, 3];
println!("Before defining closure: {:?}", list);
thread::spawn(move || println!("From thread: {:?}", list))
.join()
.unwrap();
}
The new thread might finish before the rest of the main thread finishes, or the main thread might finish first. If the main thread maintained ownership of list
but ended before the new thread did and dropped list
, the immutable reference in the thread would be invalid. Therefore, the compiler requires that list
be moved into the closure given to the new thread so the reference will be valid.
Moving Captured Values Out of Closures and the Fn Traits
A closure body can do any of the following: move a captured value out of the closure, mutate the captured value, neither move nor mutate the value, or capture nothing from the environment to begin with.
Closures will automatically implement one, two, or all three of these Fn
traits, in an additive fashion, depending on how the closure’s body handles the values:
FnOnce
applies to closures that can be called once. All closures implement at least this trait, because all closures can be called. A closure that moves captured values out of its body will only implement FnOnce
and none of the other Fn
traits, because it can only be called once.
FnMut
applies to closures that don’t move captured values out of their body, but that might mutate the captured values. These closures can be called more than once.
Fn
applies to closures that don’t move captured values out of their body and that don’t mutate captured values, as well as closures that capture nothing from their environment. These closures can be called more than once without mutating their environment, which is important in cases such as calling a closure multiple times concurrently.
Let’s look at the definition of the unwrap_or_else
method on Option<T>
:
impl<T> Option<T> {
pub fn unwrap_or_else<F>(self, f: F) -> T
where
F: FnOnce() -> T
{
match self {
Some(x) => x,
None => f(),
}
}
}
Using FnOnce
in the trait bound expresses the constraint that unwrap_or_else
is only going to call f
at most one time.
Note: If what we want to do doesn’t require capturing a value from the environment, we can use the name of a function rather than a closure. For example, we could call unwrap_or_else(Vec::new)
on a Option<Vec<T>>
value to get a new, empty vector if the value is None
. The compiler automatically implements whichever of the Fn
traits is applicable for a function definition.
Now let’s look at the standard library method sort_by_key
defined on slices, to see how that differs from unwrap_or_else
and why sort_by_key
uses FnMut
instead of FnOnce
for the trait bound.
Filename: src/main.rs
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let mut list = [
Rectangle { width: 10, height: 1 },
Rectangle { width: 3, height: 5 },
Rectangle { width: 7, height: 12 },
];
list.sort_by_key(|r| r.width);
println!("{:#?}", list);
}
The reason sort_by_key
is defined to take an FnMut
closure is that it calls the closure multiple times: once for each item in the slice. The closure |r| r.width
doesn’t capture, mutate, or move out anything from its environment, so it meets the trait bound requirements.
In contrast, the following listing shows an example of a closure that implements just the FnOnce
trait, because it moves a value out of the environment. The compiler won’t let us use this closure with sort_by_key
:
Filename: src/main.rs
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let mut list = [
Rectangle { width: 10, height: 1 },
Rectangle { width: 3, height: 5 },
Rectangle { width: 7, height: 12 },
];
let mut sort_operations = vec![];
let value = String::from("closure called");
list.sort_by_key(|r| {
sort_operations.push(value);
r.width
});
println!("{list:#?}");
}
This code attempts to do this counting by pushing value
- a String
from the closure’s environment - into the sort_operations
vector. The closure captures value
and then moves value
out of the closure by transferring ownership of value
to the sort_operations
vector. This closure can be called once; trying to call it a second time wouldn’t work because value
would no longer be in the environment to be pushed into sort_operations
again! Therefore, this closure only implements FnOnce
.
The closure in the following listing works with sort_by_key
because it is only capturing a mutable reference to the num_sort_operations
counter and can therefore be called more than once:
Filename: src/main.rs
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let mut list = [
Rectangle { width: 10, height: 1 },
Rectangle { width: 3, height: 5 },
Rectangle { width: 7, height: 12 },
];
let mut num_sort_operations = 0;
list.sort_by_key(|r| {
num_sort_operations += 1;
r.width
});
println!("{list:#?}, sorted in {num_sort_operations} operations");
}
Processing a Series of Items with Iterators
In Rust, iterators are lazy, meaning they have no effect until you call methods that consume the iterator to use it up.
let v1 = vec![1, 2, 3];
let v1_iter = v1.iter();
for val in v1_iter {
println!("Got: {val}");
}
The Iterator Trait and the next Method
All iterators implement a trait named Iterator
that is defined in the standard library. The definition of the trait looks like this:
pub trait Iterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
}
Notice this definition uses some new syntax: type Item
and Self::Item
, which are defining an associated type with this trait. For now, all you need to know is that this code says implementing the Iterator
trait requires that you also define an Item
type, and this Item
type is used in the return type of the next
method. In other words, the Item
type will be the type returned from the iterator.
The Iterator
trait only requires implementors to define one method: the next
method, which returns one item of the iterator at a time wrapped in Some
and, when iteration is over, returns None
.
We can call the next method on iterators directly:
Filename: src/lib.rs
#[test]
fn iterator_demonstration() {
let v1 = vec![1, 2, 3];
let mut v1_iter = v1.iter();
assert_eq!(v1_iter.next(), Some(&1));
assert_eq!(v1_iter.next(), Some(&2));
assert_eq!(v1_iter.next(), Some(&3));
assert_eq!(v1_iter.next(), None);
}
Note that we needed to make v1_iter
mutable: calling the next
method on an iterator changes internal state that the iterator uses to keep track of where it is in the sequence. In other words, this code consumes, or uses up, the iterator. Each call to next
eats up an item from the iterator. We didn’t need to make v1_iter
mutable when we used a for
loop because the loop took ownership of v1_iter
and made it mutable behind the scenes.
Also note that the values we get from the calls to next
are immutable references to the values in the vector. The iter
method produces an iterator over immutable references. If we want to create an iterator that takes ownership of v1
and returns owned values, we can call into_iter
instead of iter
. Similarly, if we want to iterate over mutable references, we can call iter_mut
instead of iter
.
Methods that Consume the Iterator
Methods that call next
are called consuming adaptors because calling them uses up the iterator. One example is the sum
method, which takes ownership of the iterator and iterates through the items by repeatedly calling next
, thus consuming the iterator. As it iterates through, it adds each item to a running total and returns the total when iteration is complete. The following listing has a test illustrating a use of the sum
method:
Filename: src/lib.rs
#[test]
fn iterator_sum() {
let v1 = vec![1, 2, 3];
let v1_iter = v1.iter();
let total: i32 = v1_iter.sum();
assert_eq!(total, 6);
}
We aren’t allowed to use v1_iter
after the call to sum
because sum
takes ownership of the iterator we call it on.
Methods that Produce Other Iterators
Iterator adaptors are methods defined on the Iterator
trait that don’t consume the iterator. Instead, they produce different iterators by changing some aspect of the original iterator.
The following listing shows an example of calling the iterator adaptor method map
, which takes a closure to call on each item as the items are iterated through. The map
method returns a new iterator that produces the modified items.
Filename: src/main.rs
let v1: Vec<i32> = vec![1, 2, 3];
v1.iter().map(|x| x + 1);
To fix this warning and consume the iterator, we’ll use the collect
method. This method consumes the iterator and collects the resulting values into a collection data type.
Filename: src/main.rs
let v1: Vec<i32> = vec![1, 2, 3];
let v2: Vec<_> = v1.iter().map(|x| x + 1).collect();
assert_eq!(v2, vec![2, 3, 4]);
Using Closures that Capture Their Environment
Many iterator adapters take closures as arguments, and commonly the closures we’ll specify as arguments to iterator adapters will be closures that capture their environment.
For this example, we’ll use the filter
method that takes a closure. The closure gets an item from the iterator and returns a bool
. If the closure returns true
, the value will be included in the iteration produced by filter
. If the closure returns false
, the value won’t be included.
Filename: src/lib.rs
#[derive(PartialEq, Debug)]
struct Shoe {
size: u32,
style: String,
}
fn shoes_in_size(shoes: Vec<Shoe>, shoe_size: u32) -> Vec<Shoe> {
shoes.into_iter().filter(|s| s.size == shoe_size).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn filters_by_size() {
let shoes = vec![
Shoe {
size: 10,
style: String::from("sneaker"),
},
Shoe {
size: 13,
style: String::from("sandal"),
},
Shoe {
size: 10,
style: String::from("boot"),
},
];
let in_my_size = shoes_in_size(shoes, 10);
assert_eq!(
in_my_size,
vec![
Shoe {
size: 10,
style: String::from("sneaker")
},
Shoe {
size: 10,
style: String::from("boot")
},
]
);
}
}
In the body of shoes_in_size
, we call into_iter
to create an iterator that takes ownership of the vector. Then we call filter
to adapt that iterator into a new iterator that only contains elements for which the closure returns true
.
The closure captures the shoe_size
parameter from the environment and compares the value with each shoe’s size, keeping only shoes of the size specified.
Finally, calling collect
gathers the values returned by the adapted iterator into a vector that’s returned by the function.
Improving Our I/O Project
Removing a clone Using an Iterator
Filename: src/lib.rs
impl Config {
pub fn build(args: &[String]) -> Result<Config, &'static str> {
if args.len() < 3 {
return Err("not enough arguments");
}
let query = args[1].clone();
let file_path = args[2].clone();
let ignore_case = env::var("IGNORE_CASE").is_ok();
Ok(Config {
query,
file_path,
ignore_case,
})
}
}
With our new knowledge about iterators, we can change the build
function to take ownership of an iterator as its argument instead of borrowing a slice.
Using the Returned Iterator Directly
Filename: src/main.rs
fn main() {
let args: Vec<String> = env::args().collect();
let config = Config::build(&args).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {err}");
process::exit(1);
});
}
We’ll first change the start of the main
function, which this time uses an iterator. This won’t compile until we update Config::build
as well.
Filename: src/main.rs
fn main() {
let config = Config::build(env::args()).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {err}");
process::exit(1);
});
}
The env::args
function returns an iterator! Rather than collecting the iterator values into a vector and then passing a slice to Config::build
, now we’re passing ownership of the iterator returned from env::args
to Config::build
directly.
In your I/O project’s src/lib.rs file, let’s change the signature of Config::build
to look like the following listing. This still won’t compile because we need to update the function body.
Filename: src/lib.rs
impl Config {
pub fn build(
mut args: impl Iterator<Item = String>,
) -> Result<Config, &'static str> {
The standard library documentation for the env::args
function shows that the type of the iterator it returns is std::env::Args
, and that type implements the Iterator
trait and returns String
values. (I think we can use std::env::Args
directly)
Using Iterator Trait Methods Instead of Indexing
Filename: src/lib.rs
impl Config {
pub fn build(
mut args: impl Iterator<Item = String>,
) -> Result<Config, &'static str> {
args.next();
let query = match args.next() {
Some(arg) => arg,
None => return Err("Didn't get a query string"),
};
let file_path = match args.next() {
Some(arg) => arg,
None => return Err("Didn't get a file path"),
};
let ignore_case = env::var("IGNORE_CASE").is_ok();
Ok(Config {
query,
file_path,
ignore_case,
})
}
}
Remember that the first value in the return value of env::args
is the name of the program.
Making Code Clearer with Iterator Adaptors
We can also take advantage of iterators in the search
function in our I/O project:
Filename: src/lib.rs
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let mut results = Vec::new();
for line in contents.lines() {
if line.contains(query) {
results.push(line);
}
}
results
}
We can write this code in a more concise way using iterator adaptor methods. Doing so also lets us avoid having a mutable intermediate results
vector. The functional programming style prefers to minimize the amount of mutable state to make code clearer. Removing the mutable state might enable a future enhancement to make searching happen in parallel, because we wouldn’t have to manage concurrent access to the results
vector. The following listing shows this change:
Filename: src/lib.rs
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
contents
.lines()
.filter(|line| line.contains(query))
.collect()
}
Iterators, although a high-level abstraction, get compiled down to roughly the same code as if you’d written the lower-level code yourself. Iterators are one of Rust’s zero-cost abstractions, by which we mean using the abstraction imposes no additional runtime overhead.
In general, C++ implementations obey the zero-overhead principle: What you don’t use, you don’t pay for. And further: What you do use, you couldn’t hand code any better.
As another example, the following code is taken from an audio decoder. The decoding algorithm uses the linear prediction mathematical operation to estimate future values based on a linear function of the previous samples. This code uses an iterator chain to do some math on three variables in scope: a buffer
slice of data, an array of 12 coefficients
, and an amount by which to shift data in qlp_shift
.
let buffer: &mut [i32];
let coefficients: [i64; 12];
let qlp_shift: i16;
for i in 12..buffer.len() {
let prediction = coefficients.iter()
.zip(&buffer[i - 12..i])
.map(|(&c, &s)| c * s as i64)
.sum::<i64>() >> qlp_shift;
let delta = buffer[i];
buffer[i] = prediction as i32 + delta;
}
To calculate the value of prediction
, this code iterates through each of the 12 values in coefficients
and uses the zip
method to pair the coefficient values with the previous 12 values in buffer
. Then, for each pair, we multiply the values together, sum all the results, and shift the bits in the sum qlp_shift
bits to the right.
Calculations in applications like audio decoders often prioritize performance most highly. There’s no loop at all corresponding to the iteration over the values in coefficients
: Rust knows that there are 12 iterations, so it “unrolls” the loop. Unrolling is an optimization that removes the overhead of the loop controlling code and instead generates repetitive code for each iteration of the loop.
All of the coefficients get stored in registers, which means accessing the values is very fast. There are no bounds checks on the array access at runtime. All these optimizations that Rust is able to apply make the resulting code extremely efficient.
More About Cargo and Crates.io
Customizing Builds with Release Profiles
Cargo has two main profiles: the dev
profile Cargo uses when you run cargo build
and the release
profile Cargo uses when you run cargo build --release
.
Cargo has default settings for each of the profiles that apply when you haven’t explicitly added any [profile.*]
sections in the project’s Cargo.toml file. By adding [profile.*]
sections for any profile you want to customize, you override any subset of the default settings. For example, here are the default values for the opt-level
setting for the dev
and release
profiles:
Filename: Cargo.toml
[profile.dev]
opt-level = 0
[profile.release]
opt-level = 3
The opt-level
setting controls the number of optimizations Rust will apply to your code, with a range of 0 to 3. Applying more optimizations extends compiling time, so if you’re in development and compiling your code often, you’ll want fewer optimizations to compile faster even if the resulting code runs slower. The default opt-level
for dev is therefore 0
. When you’re ready to release your code, it’s best to spend more time compiling. You’ll only compile in release mode once, but you’ll run the compiled program many times, so release mode trades longer compile time for code that runs faster. That is why the default opt-level
for the release
profile is 3
.
You can override a default setting by adding a different value for it in Cargo.toml.
Filename: Cargo.toml
[profile.dev]
opt-level = 1
Because we set opt-level
to 1
, Cargo will apply more optimizations than the default, but not as many as in a release build.
Publishing a Crate to Crates.io
In previous chapter, we discussed how to comment Rust code using two slashes, //
. Rust also has a particular kind of comment for documentation, known conveniently as a documentation comment, that will generate HTML documentation.
Documentation comments use three slashes, ///
, instead of two and support Markdown notation for formatting the text. Place documentation comments just before the item they’re documenting. The following listing shows documentation comments for an add_one
function in a crate named my_crate
.
Filename: src/lib.rs
pub fn add_one(x: i32) -> i32 {
x + 1
}
We can generate the HTML documentation from this documentation comment by running cargo doc
. This command runs the rustdoc
tool distributed with Rust and puts the generated HTML documentation in the target/doc directory.
For convenience, running cargo doc --open
will build the HTML for your current crate’s documentation (as well as the documentation for all of your crate’s dependencies) and open the result in a web browser.
Commonly Used Sections
We used the # Examples
Markdown heading to create a section in the HTML with the title “Examples.” Here are some other sections that crate authors commonly use in their documentation:
Panics: The scenarios in which the function being documented could panic. Callers of the function who don’t want their programs to panic should make sure they don’t call the function in these situations.
Errors: If the function returns a Result
, describing the kinds of errors that might occur and what conditions might cause those errors to be returned can be helpful to callers so they can write code to handle the different kinds of errors in different ways.
Safety: If the function is unsafe
to call (we discuss unsafety in later chapter), there should be a section explaining why the function is unsafe and covering the invariants that the function expects callers to uphold.
Most documentation comments don’t need all of these sections, but this is a good checklist to remind you of the aspects of your code users will be interested in knowing about.
Adding example code blocks in your documentation comments can help demonstrate how to use your library, and doing so has an additional bonus: running cargo test
will run the code examples in your documentation as tests! Nothing is better than documentation with examples. But nothing is worse than examples that don’t work because the code has changed since the documentation was written. If we run cargo test
with the documentation for the add_one
function from the preceding listing, we will see a section in the test results like this:
Doc-tests my_crate
running 1 test
test src/lib.rs - add_one (line 5) ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.27s
The style of doc comment //!
adds documentation to the item that contains the comments rather than to the items following the comments. We typically use these doc comments inside the crate root file (src/lib.rs by convention) or inside a module to document the crate or the module as a whole.
For example, to add documentation that describes the purpose of the my_crate
crate that contains the add_one
function, we add documentation comments that start with //!
to the beginning of the src/lib.rs file, as shown in the following listing:
Filename: src/lib.rs
Notice there isn’t any code after the last line that begins with //!
. Because we started the comments with //!
instead of ///
, we’re documenting the item that contains this comment rather than an item that follows this comment. In this case, that item is the src/lib.rs file, which is the crate root. These comments describe the entire crate.
When we run cargo doc --open
, these comments will display on the front page of the documentation for my_crate
above the list of public items in the crate
Documentation comments within items are useful for describing crates and modules especially. Use them to explain the overall purpose of the container to help your users understand the crate’s organization.
Exporting a Convenient Public API with pub use
The structure of your public API is a major consideration when publishing a crate.
However, the structure that makes sense to you while you’re developing a crate might not be very convenient for your users. You might want to organize your structs in a hierarchy containing multiple levels, but then people who want to use a type you’ve defined deep in the hierarchy might have trouble finding out that type exists. They might also be annoyed at having to enter use my_crate::some_module::another_module::UsefulType
; rather than use my_crate::UsefulType
;.
The good news is that if the structure isn’t convenient for others to use from another library, you don’t have to rearrange your internal organization: instead, you can re-export items to make a public structure that’s different from your private structure by using pub use
. Re-exporting takes a public item in one location and makes it public in another location, as if it were defined in the other location instead.
For example, say we made a library named art
for modeling artistic concepts. Within this library are two modules: a kinds
module containing two enums named PrimaryColor
and SecondaryColor
and a utils
module containing a function named mix
, as shown in the following listing:
Filename: src/lib.rs
pub mod kinds {
pub enum PrimaryColor {
Red,
Yellow,
Blue,
}
pub enum SecondaryColor {
Orange,
Green,
Purple,
}
}
pub mod utils {
use crate::kinds::*;
pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor {
}
}
Note that the PrimaryColor
and SecondaryColor
types aren’t listed on the front page by cargo doc --open
, nor is the mix
function. We have to click kinds
and utils
to see them.
Another crate that depends on this library would need use
statements that bring the items from art
into scope, specifying the module structure that’s currently defined:
Filename: src/main.rs
use art::kinds::PrimaryColor;
use art::utils::mix;
fn main() {
let red = PrimaryColor::Red;
let yellow = PrimaryColor::Yellow;
mix(red, yellow);
}
The module structure of the art
crate is more relevant to developers working on the art
crate than to those using it.
To remove the internal organization from the public API, we can modify the art
crate code to add pub use
statements to re-export the items at the top level, as shown in the following listing:
Filename: src/lib.rs
pub use self::kinds::PrimaryColor;
pub use self::kinds::SecondaryColor;
pub use self::utils::mix;
pub mod kinds {
}
pub mod utils {
}
The API documentation that cargo doc
generates for this crate will now list and link re-exports on the front page, making the PrimaryColor
and SecondaryColor
types and the mix
function easier to find.
The art
crate users can still see and use the internal structure, or they can use the more convenient structure:
Filename: src/main.rs
use art::mix;
use art::PrimaryColor;
fn main() {
}
In cases where there are many nested modules, re-exporting the types at the top level with pub use
can make a significant difference in the experience of people who use the crate. Another common use of pub use
is to re-export definitions of a dependency in the current crate to make that crate’s definitions part of your crate’s public API.
Setting Up a Crates.io Account
Before you can publish any crates, you need to create an account on crates.io and get an API token. To do so, visit the home page at crates.io and log in via a GitHub account. (The GitHub account is currently a requirement, but the site might support other ways of creating an account in the future.) Once you’re logged in, visit your account settings at https://crates.io/me/ and retrieve your API key. Then run the cargo login
command with your API key, like this:
$ cargo login
abcdefghijklmnopqrstuvwxyz012345
This command will inform Cargo of your API token and store it locally in ~/.cargo/credentials. Note that this token is a secret: do not share it with anyone else. If you do share it with anyone for any reason, you should revoke it and generate a new token on crates.io.
Before publishing, you’ll need to add some metadata in the [package]
section of the crate’s Cargo.toml file.
Your crate will need a unique name. While you’re working on a crate locally, you can name a crate whatever you’d like. However, crate names on crates.io are allocated on a first-come, first-served basis. Once a crate name is taken, no one else can publish a crate with that name. Before attempting to publish a crate, search for the name you want to use. If the name has been used, you will need to find another name and edit the name
field in the Cargo.toml file under the [package]
section to use the new name for publishing, like so:
Filename: Cargo.toml
[package]
name = "guessing_game"
Even if you’ve chosen a unique name, when you run cargo publish
to publish the crate at this point, you’ll get a warning and then an error:
$ cargo publish
Updating crates.io index
warning: manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html
--snip--
error: failed to publish to registry at https://crates.io
Caused by:
the remote server responded with an error: missing or empty metadata fields: description, license. Please see https://doc.rust-lang.org/cargo/reference/manifest.html for how to upload metadata
This errors because you’re missing some crucial information: a description and license are required so people will know what your crate does and under what terms they can use it. In Cargo.toml, add a description that’s just a sentence or two, because it will appear with your crate in search results. For the license
field, you need to give a license identifier value. The Linux Foundation’s Software Package Data Exchange (SPDX) lists the identifiers you can use for this value. For example, to specify that you’ve licensed your crate using the MIT License, add the MIT
identifier:
Filename: Cargo.toml
[package]
name = "guessing_game"
license = "MIT"
If you want to use a license that doesn’t appear in the SPDX, you need to place the text of that license in a file, include the file in your project, and then use license-file
to specify the name of that file instead of using the license
key.
Many people in the Rust community license their projects in the same way as Rust by using a dual license of MIT OR Apache-2.0
. This practice demonstrates that you can also specify multiple license identifiers separated by OR
to have multiple licenses for your project.
With a unique name, the version, your description, and a license added, the Cargo.toml file for a project that is ready to publish might look like this:
Filename: Cargo.toml
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"
description = "A fun game where you guess what number the computer has chosen."
license = "MIT OR Apache-2.0"
[dependencies]
Cargo’s documentation describes other metadata you can specify to ensure others can discover and use your crate more easily.
Publishing to Crates.io
Publishing a crate uploads a specific version to crates.io for others to use.
Be careful, because a publish is permanent. The version can never be overwritten, and the code cannot be deleted. One major goal of crates.io is to act as a permanent archive of code so that builds of all projects that depend on crates from crates.io will continue to work. Allowing version deletions would make fulfilling that goal impossible. However, there is no limit to the number of crate versions you can publish.
Run the cargo publish
command again. It should succeed now:
$ cargo publish
Updating crates.io index
Packaging guessing_game v0.1.0 (file:///projects/guessing_game)
Verifying guessing_game v0.1.0 (file:///projects/guessing_game)
Compiling guessing_game v0.1.0
(file:///projects/guessing_game/target/package/guessing_game-0.1.0)
Finished dev [unoptimized + debuginfo] target(s) in 0.19s
Uploading guessing_game v0.1.0 (file:///projects/guessing_game)
Publishing a New Version of an Existing Crate
When you’ve made changes to your crate and are ready to release a new version, you change the version
value specified in your Cargo.toml file and republish. Use the Semantic Versioning rules to decide what an appropriate next version number is based on the kinds of changes you’ve made. Then run cargo publish
to upload the new version.
Deprecating Versions from Crates.io with cargo yank
Although you can’t remove previous versions of a crate, you can prevent any future projects from adding them as a new dependency. This is useful when a crate version is broken for one reason or another. In such situations, Cargo supports yanking a crate version.
Yanking a version prevents new projects from depending on that version while allowing all existing projects that depend on it to continue. Essentially, a yank means that all projects with a Cargo.lock will not break, and any future Cargo.lock files generated will not use the yanked version.
To yank a version of a crate, in the directory of the crate that you’ve previously published, run cargo yank
and specify which version you want to yank. For example, if we’ve published a crate named guessing_game
version 1.0.1 and we want to yank it, in the project directory for guessing_game
we’d run:
$ cargo yank --vers 1.0.1
Updating crates.io index
Yank [email protected]
By adding --undo
to the command, you can also undo a yank and allow projects to start depending on a version again:
$ cargo yank --vers 1.0.1 --undo
Updating crates.io index
Unyank [email protected]
A yank does not delete any code. It cannot, for example, delete accidentally uploaded secrets. If that happens, you must reset those secrets immediately.
Cargo Workspaces
As your project develops, you might find that the library crate continues to get bigger and you want to split your package further into multiple library crates. Cargo offers a feature called workspaces that can help manage multiple related packages that are developed in tandem.
Creating a Workspace
A workspace is a set of packages that share the same Cargo.lock and output directory. We’ll have a workspace containing a binary and two libraries. The binary, which will provide the main functionality, will depend on the two libraries. These three crates will be part of the same workspace. We’ll start by creating a new directory for the workspace:
mkdir add
cd add
Next, in the add directory, we create the Cargo.toml file that will configure the entire workspace. This file won’t have a [package]
section. Instead, it will start with a [workspace]
section that will allow us to add members to the workspace. We also make a point to use the latest and greatest version of Cargo’s resolver algorithm in our workspace by setting the resolver
to "3"
.
Filename: Cargo.toml
[workspace]
resolver = "3"
Next, we’ll create the adder
binary crate by running cargo new
within the add directory:
$ cargo new adder
Creating binary (application) `adder` package
Adding `adder` as member of workspace at `file:///projects/add`
Running cargo new
inside a workspace also automatically adds the newly created package to the members
key in the [workspace]
definition in the workspace Cargo.toml
, like this:
[workspace]
resolver = "3"
members = ["adder"]
At this point, we can build the workspace by running cargo build
. The files in your add directory should look like this:
├── Cargo.lock
├── Cargo.toml
├── adder
│ ├── Cargo.toml
│ └── src
│ └── main.rs
└── target
The workspace has one target directory at the top level that the compiled artifacts will be placed into; the adder
package doesn’t have its own target directory. Even if we were to run cargo build
from inside the adder directory, the compiled artifacts would still end up in add/target rather than add/adder/target. Cargo structures the target directory in a workspace like this because the crates in a workspace are meant to depend on each other. If each crate had its own target directory, each crate would have to recompile each of the other crates in the workspace to place the artifacts in its own target directory. By sharing one target directory, the crates can avoid unnecessary rebuilding.
Creating the Second Package in the Workspace
Next, let’s create another member package in the workspace and call it add_one
. Generate a new library crate named add_one
:
$ cargo new add_one --lib
Creating library `add_one` package
Adding `add_one` as member of workspace at `file:///projects/add`
The top-level Cargo.toml will now include the add_one path in the members
list:
Filename: Cargo.toml
[workspace]
resolver = "3"
members = ["adder", "add_one"]
Your add directory should now have these directories and files:
├── Cargo.lock
├── Cargo.toml
├── add_one
│ ├── Cargo.toml
│ └── src
│ └── lib.rs
├── adder
│ ├── Cargo.toml
│ └── src
│ └── main.rs
└── target
In the add_one/src/lib.rs file, let’s add an add_one
function:
Filename: add_one/src/lib.rs
pub fn add_one(x: i32) -> i32 {
x + 1
}
Now we can have the adder
package with our binary depend on the add_one
package that has our library. First, we’ll need to add a path dependency on add_one
to adder/Cargo.toml.
Filename: adder/Cargo.toml
[dependencies]
add_one = { path = "../add_one" }
Cargo doesn’t assume that crates in a workspace will depend on each other, so we need to be explicit about the dependency relationships.
Next, let’s use the add_one
function (from the add_one
crate) in the adder
crate.
Filename: adder/src/main.rs
fn main() {
let num = 10;
println!("Hello, world! {num} plus one is {}!", add_one::add_one(num));
}
Let’s build the workspace by running cargo build
in the top-level add directory!
$ cargo build
Compiling add_one v0.1.0 (file:///projects/add/add_one)
Compiling adder v0.1.0 (file:///projects/add/adder)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.22s
To run the binary crate from the add directory, we can specify which package in the workspace we want to run by using the -p
argument and the package name with cargo run
:
$ cargo run -p adder
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/adder`
Hello, world! 10 plus one is 11!
This runs the code in adder/src/main.rs, which depends on the add_one
crate.
Depending on an External Package in a Workspace
Notice that the workspace has only one Cargo.lock file at the top level, rather than having a Cargo.lock in each crate’s directory. This ensures that all crates are using the same version of all dependencies. If we add the rand
package to the adder/Cargo.toml and add_one/Cargo.toml files, Cargo will resolve both of those to one version of rand
and record that in the one Cargo.lock. Making all crates in the workspace use the same dependencies means the crates will always be compatible with each other. Let’s add the rand
crate to the [dependencies]
section in the add_one/Cargo.toml file so we can use the rand
crate in the add_one
crate:
Filename: add_one/Cargo.toml
[dependencies]
rand = "0.8.5"
We can now add use rand
; to the add_one/src/lib.rs file, and building the whole workspace by running cargo build
in the add directory will bring in and compile the rand
crate. We will get one warning because we aren’t referring to the rand
we brought into scope:
$ cargo build
Updating crates.io index
Downloaded rand v0.8.5
--snip--
Compiling rand v0.8.5
Compiling add_one v0.1.0 (file:///projects/add/add_one)
warning: unused import: `rand`
--> add_one/src/lib.rs:1:5
|
1 | use rand;
| ^^^^
|
= note: `` on by default
warning: `add_one` (lib) generated 1 warning (run `cargo fix --lib -p add_one` to apply 1 suggestion)
Compiling adder v0.1.0 (file:///projects/add/adder)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.95s
The top-level Cargo.lock now contains information about the dependency of add_one
on rand
. However, even though rand
is used somewhere in the workspace, we can’t use it in other crates in the workspace unless we add rand
to their Cargo.toml files as well. For example, if we add use rand
; to the adder/src/main.rs file for the adder
package, we’ll get an error:
$ cargo build
--snip--
Compiling adder v0.1.0 (file:///projects/add/adder)
error[E0432]: unresolved import `rand`
--> adder/src/main.rs:2:5
|
2 | use rand;
| ^^^^ no external crate `rand`
To fix this, edit the Cargo.toml file for the adder
package and indicate that rand
is a dependency for it as well. Building the adder
package will add rand
to the list of dependencies for adder
in Cargo.lock, but no additional copies of rand
will be downloaded. Cargo has ensured that every crate in every package in the workspace using the rand
package will be using the same version, saving us space and ensuring that the crates in the workspace will be compatible with each other.
If crates in the workspace specify incompatible versions of the same dependency, Cargo will resolve each of them, but will still try to resolve as few versions as possible.
Adding a Test to a Workspace
For another enhancement, let’s add a test of the add_one::add_one
function within the add_one
crate:
Filename: add_one/src/lib.rs
pub fn add_one(x: i32) -> i32 {
x + 1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
assert_eq!(3, add_one(2));
}
}
Now run cargo test
in the top-level add directory. Running cargo test
in a workspace structured like this one will run the tests for all the crates in the workspace:
$ cargo test
Compiling add_one v0.1.0 (file:///projects/add/add_one)
Compiling adder v0.1.0 (file:///projects/add/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.20s
Running unittests src/lib.rs (target/debug/deps/add_one-93c49ee75dc46543)
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running unittests src/main.rs (target/debug/deps/adder-3a47283c568d2b6a)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests add_one
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
We can also run tests for one particular crate in a workspace from the top-level directory by using the -p
flag and specifying the name of the crate we want to test:
$ cargo test -p add_one
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.00s
Running unittests src/lib.rs (target/debug/deps/add_one-93c49ee75dc46543)
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests add_one
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
If you publish the crates in the workspace to crates.io, each crate in the workspace will need to be published separately. Like cargo test
, we can publish a particular crate in our workspace by using the -p
flag and specifying the name of the crate we want to publish.
Installing Binaries with cargo install
The cargo install
command allows you to install and use binary crates locally. Note that you can only install packages that have binary targets. A binary target is the runnable program that is created if the crate has a src/main.rs file or another file specified as a binary, as opposed to a library target that isn’t runnable on its own but is suitable for including within other programs. Usually, crates have information in the README file about whether a crate is a library, has a binary target, or both.
All binaries installed with cargo install
are stored in the installation root’s bin folder. If you installed Rust using rustup.rs and don’t have any custom configurations, this directory will be $HOME/.cargo/bin. Ensure that directory is in your $PATH
to be able to run programs you’ve installed with cargo install
.
For example, there’s a Rust implementation of the grep
tool called ripgrep
for searching files. To install ripgrep
, we can run the following:
$ cargo install ripgrep
Updating crates.io index
Downloaded ripgrep v14.1.1
Downloaded 1 crate (213.6 KB) in 0.40s
Installing ripgrep v14.1.1
--snip--
Compiling grep v0.3.2
Finished `release` profile [optimized + debuginfo] target(s) in 6.73s
Installing ~/.cargo/bin/rg
Installed package `ripgrep v14.1.1` (executable `rg`)
The second-to-last line of the output shows the location and the name of the installed binary, which in the case of ripgrep
is rg
. As long as the installation directory is in your $PATH
, as mentioned previously, you can then run rg --help
and start using a faster, Rustier tool for searching files!
Extending Cargo with Custom Commands
Cargo is designed so you can extend it with new subcommands without having to modify Cargo. If a binary in your $PATH
is named cargo-something
, you can run it as if it was a Cargo subcommand by running cargo something
. Custom commands like this are also listed when you run cargo --list
. Being able to use cargo install
to install extensions and then run them just like the built-in Cargo tools is a super convenient benefit of Cargo’s design!
Smart Pointers
A pointer is a general concept for a variable that contains an address in memory. This address refers to, or “points at,” some other data. The most common kind of pointer in Rust is a reference. References are indicated by the &
symbol and borrow the value they point to. They don’t have any special capabilities other than referring to data, and have no overhead.
Smart pointers, on the other hand, are data structures that act like a pointer but also have additional metadata and capabilities. Rust has a variety of smart pointers defined in the standard library that provide functionality beyond that provided by references.
Rust, with its concept of ownership and borrowing, has an additional difference between references and smart pointers: while references only borrow data, in many cases, smart pointers own the data they point to.
Though we didn’t call them as such at the time, we’ve already encountered a few smart pointers in this book, including String
and Vec<T>
. Both these types count as smart pointers because they own some memory and allow you to manipulate it. They also have metadata and extra capabilities or guarantees. String
, for example, stores its capacity as metadata and has the extra ability to ensure its data will always be valid UTF-8.
Smart pointers are usually implemented using structs. Unlike an ordinary struct, smart pointers implement the Deref
and Drop
traits. The Deref
trait allows an instance of the smart pointer struct to behave like a reference so you can write your code to work with either references or smart pointers. The Drop
trait allows you to customize the code that’s run when an instance of the smart pointer goes out of scope.
We’ll cover the most common smart pointers in the standard library:
Box<T>
for allocating values on the heap
Rc<T>
, a reference counting type that enables multiple ownership
Ref<T>
and RefMut<T>
, accessed through RefCell<T>
, a type that enforces the borrowing rules at runtime instead of compile time
Using Box<T>
to Point to Data on the Heap
The most straightforward smart pointer is a box, whose type is written Box<T>
. Boxes allow you to store data on the heap rather than the stack. What remains on the stack is the pointer to the heap data.
Boxes don’t have performance overhead, other than storing their data on the heap instead of on the stack. But they don’t have many extra capabilities either. You’ll use them most often in these situations:
When you have a type whose size can’t be known at compile time and you want to use a value of that type in a context that requires an exact size
When you have a large amount of data and you want to transfer ownership but ensure the data won’t be copied when you do so
When you want to own a value and you care only that it’s a type that implements a particular trait rather than being of a specific type
We’ll demonstrate the first situation in the “Enabling Recursive Types with Boxes” section. In the second case, transferring ownership of a large amount of data can take a long time because the data is copied around on the stack. To improve performance in this situation, we can store the large amount of data on the heap in a box. Then, only the small amount of pointer data is copied around on the stack, while the data it references stays in one place on the heap. The third case is known as a trait object, and Using Trait Objects That Allow for Values of Different Types in later Chapter is devoted to that topic.
Using a Box<T>
to Store Data on the Heap
The following listing shows how to use a box to store an i32
value on the heap:
Filename: src/main.rs
fn main() {
let b = Box::new(5);
println!("b = {b}");
}
In this case, we can access the data in the box similarly to how we would if this data were on the stack. Just like any owned value, when a box goes out of scope, as b
does at the end of main
, it will be deallocated. The deallocation happens both for the box (stored on the stack) and the data it points to (stored on the heap).
Enabling Recursive Types with Boxes
A value of recursive type can have another value of the same type as part of itself. Recursive types pose an issue because at compile time Rust needs to know how much space a type takes up. However, the nesting of values of recursive types could theoretically continue infinitely, so Rust can’t know how much space the value needs. Because boxes have a known size, we can enable recursive types by inserting a box in the recursive type definition.
As an example of a recursive type, let’s explore the cons list. This is a data type commonly found in functional programming languages. The cons list type we’ll define is straightforward except for the recursion; therefore, the concepts in the example we’ll work with will be useful any time you get into more complex situations involving recursive types.
A cons list is a data structure that comes from the Lisp programming language and its dialects and is made up of nested pairs, and is the Lisp version of a linked list. Its name comes from the cons
function (short for construct function) in Lisp that constructs a new pair from its two arguments. By calling cons
on a pair consisting of a value and another pair, we can construct cons lists made up of recursive pairs.
For example, here’s a pseudocode representation of a cons list containing the list 1, 2, 3
with each pair in parentheses:
(1, (2, (3, Nil)))
Each item in a cons list contains two elements: the value of the current item and the next item. The last item in the list contains only a value called Nil
without a next item. A cons list is produced by recursively calling the cons
function. The canonical name to denote the base case of the recursion is Nil
. Note that this is not the same as the “null” or “nil” concept in the previous chapter, which is an invalid or absent value.
The cons list isn’t a commonly used data structure in Rust. Most of the time when you have a list of items in Rust, Vec<T>
is a better choice to use. Other, more complex recursive data types are useful in various situations, but by starting with the cons list in this chapter, we can explore how boxes let us define a recursive data type without much distraction.
The following listing contains an enum definition for a cons list. Note that this code won’t compile yet because the List
type doesn’t have a known size, which we’ll demonstrate.
Filename: src/main.rs
enum List {
Cons(i32, List),
Nil,
}
Using the List
type to store the list 1, 2, 3
would look like the code in the following listing:
Filename: src/main.rs
use crate::List::{Cons, Nil};
fn main() {
let list = Cons(1, Cons(2, Cons(3, Nil)));
}
If we try to compile the code in the previous listing, we get the error shown in the following listing:
$ cargo run
Compiling cons-list v0.1.0 (file:///projects/cons-list)
error[E0072]: recursive type `List` has infinite size
--> src/main.rs:1:1
|
1 | enum List {
| ^^^^^^^^^
2 | Cons(i32, List),
| ---- recursive without indirection
|
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
|
2 | Cons(i32, Box<List>),
| ++++ +
error[E0391]: cycle detected when computing when `List` needs drop
--> src/main.rs:1:1
|
1 | enum List {
| ^^^^^^^^^
|
= note: ...which immediately requires computing when `List` needs drop again
= note: cycle used when computing whether `List` needs drop
= note: see https://rustc-dev-guide.rust-lang.org/overview.html
Some errors have detailed explanations: E0072, E0391.
For more information about an error, try `rustc --explain E0072`.
error: could not compile `cons-list` (bin "cons-list") due to 2 previous errors
The error shows this type “has infinite size.” The reason is that we’ve defined List
with a variant that is recursive: it holds another value of itself directly. As a result, Rust can’t figure out how much space it needs to store a List
value.
Computing the Size of a Non-Recursive Type
Recall the Message
enum we defined in the previous listing when we discussed enum definitions in the previous chapter:
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
To determine how much space to allocate for a Message
value, Rust goes through each of the variants to see which variant needs the most space. Rust sees that Message::Quit
doesn’t need any space, Message::Move
needs enough space to store two i32
values, and so forth. Because only one variant will be used, the most space a Message
value will need is the space it would take to store the largest of its variants.
Contrast this with what happens when Rust tries to determine how much space a recursive type like the List
enum in the previous listing needs. The compiler starts by looking at the Cons
variant, which holds a value of type i32
and a value of type List
. Therefore, Cons
needs an amount of space equal to the size of an i32
plus the size of a List
. To figure out how much memory the List
type needs, the compiler looks at the variants, starting with the Cons
variant. The Cons
variant holds a value of type i32
and a value of type List
, and this process continues infinitely.
Using Box<T>
to Get a Recursive Type with a Known Size
Because Rust can’t figure out how much space to allocate for recursively defined types, the compiler gives an error with this helpful suggestion:
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
|
2 | Cons(i32, Box<List>),
| ++++ +
In this suggestion, “indirection” means that instead of storing a value directly, we should change the data structure to store the value indirectly by storing a pointer to the value instead.
Because a Box<T>
is a pointer, Rust always knows how much space a Box<T>
needs: a pointer’s size doesn’t change based on the amount of data it’s pointing to. This means we can put a Box<T>
inside the Cons
variant instead of another List
value directly. The Box<T>
will point to the next List
value that will be on the heap rather than inside the Cons
variant. Conceptually, we still have a list, created with lists holding other lists, but this implementation is now more like placing the items next to one another rather than inside one another.
We can change the definition of the List
enum and the usage of the List
in previous listings to the code in the following listing, which will compile:
Filename: src/main.rs
enum List {
Cons(i32, Box<List>),
Nil,
}
use crate::List::{Cons, Nil};
fn main() {
let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil))))));
}
The Cons
variant needs the size of an i32
plus the space to store the box’s pointer data. The Nil
variant stores no values, so it needs less space than the Cons
variant. We now know that any List
value will take up the size of an i32
plus the size of a box’s pointer data. By using a box, we’ve broken the infinite, recursive chain, so the compiler can figure out the size it needs to store a List
value.
Boxes provide only the indirection and heap allocation; they don’t have any other special capabilities, like those we’ll see with the other smart pointer types. They also don’t have the performance overhead that these special capabilities incur, so they can be useful in cases like the cons list where the indirection is the only feature we need.
The Box<T>
type is a smart pointer because it implements the Deref
trait, which allows Box<T>
values to be treated like references. When a Box<T>
value goes out of scope, the heap data that the box is pointing to is cleaned up as well because of the Drop
trait implementation.
Treating Smart Pointers Like Regular References with Deref
Implementing the Deref
trait allows you to customize the behavior of the dereference operator *
(not to be confused with the multiplication or glob operator). By implementing Deref in such a way that a smart pointer can be treated like a regular reference, you can write code that operates on references and use that code with smart pointers too.
Following the Pointer to the Value
A regular reference is a type of pointer, and one way to think of a pointer is as an arrow to a value stored somewhere else. In the following listing, we create a reference to an i32
value and then use the dereference operator to follow the reference to the value.
Filename: src/main.rs
fn main() {
let x = 5;
let y = &x;
assert_eq!(5, x);
assert_eq!(5, *y);
}
However, if we want to make an assertion about the value in y
, we have to use *y
to follow the reference to the value it’s pointing to (hence dereference) so the compiler can compare the actual value. Once we dereference y
, we have access to the integer value y
is pointing to that we can compare with 5
.
If we tried to write assert_eq!(5, y);
instead, we would get this compilation error:
$ cargo run
Compiling deref-example v0.1.0 (file:///projects/deref-example)
error[E0277]: can't compare `{integer}` with `&{integer}`
--> src/main.rs:6:5
|
6 | assert_eq!(5, y);
| ^^^^^^^^^^^^^^^^ no implementation for `{integer} == &{integer}`
|
= help: the trait `PartialEq<&{integer}>` is not implemented for `{integer}`
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0277`.
error: could not compile `deref-example` (bin "deref-example") due to 1 previous error
Comparing a number and a reference to a number isn’t allowed because they’re different types. We must use the dereference operator to follow the reference to the value it’s pointing to.
Using Box<T> Like a Reference
We can rewrite the code in the previous Listing to use a Box<T>
instead of a reference; the dereference operator used on the Box<T>
in the following listing functions in the same way as the dereference operator used on the reference in the previous Listing:
Filename: src/main.rs
fn main() {
let x = 5;
let y = Box::new(x);
assert_eq!(5, x);
assert_eq!(5, *y);
}
The main difference between two Listings is that here we set y
to be an instance of a box pointing to a copied value of x
rather than a reference pointing to the value of x
. In the last assertion, we can use the dereference operator to follow the box’s pointer in the same way that we did when y
was a reference.
Defining Our Own Smart Pointer
Let’s build a smart pointer similar to the Box<T>
type provided by the standard library to experience how smart pointers behave differently from references by default. Then we’ll look at how to add the ability to use the dereference operator.
The Box<T>
type is ultimately defined as a tuple struct with one element, so the following Listing defines a MyBox<T>
type in the same way. We’ll also define a new
function to match the new
function defined on Box<T>
.
Filename: src/main.rs
struct MyBox<T>(T);
impl<T> MyBox<T> {
fn new(x: T) -> MyBox<T> {
MyBox(x)
}
}
Let’s try adding the main
function and changing it to use the MyBox<T>
type we’ve defined instead of Box<T>
. The code in the following Listing won’t compile because Rust doesn’t know how to dereference MyBox
.
Filename: src/main.rs
fn main() {
let x = 5;
let y = MyBox::new(x);
assert_eq!(5, x);
assert_eq!(5, *y);
}
Here’s the resultant compilation error:
$ cargo run
Compiling deref-example v0.1.0 (file:///projects/deref-example)
error[E0614]: type `MyBox<{integer}>` cannot be dereferenced
--> src/main.rs:14:19
|
14 | assert_eq!(5, *y);
| ^^
For more information about this error, try `rustc --explain E0614`.
error: could not compile `deref-example` (bin "deref-example") due to 1 previous error
Our MyBox<T>
type can’t be dereferenced because we haven’t implemented that ability on our type. To enable dereferencing with the *
operator, we implement the Der`ef trait.
Implementing the Deref Trait
The Deref
trait, provided by the standard library, requires us to implement one method named deref
that borrows self
and returns a reference to the inner data. The following Listing contains an implementation of Deref
to add to the definition of MyBox<T>
.
Filename: src/main.rs
use std::ops::Deref;
impl<T> Deref for MyBox<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
The type Target = T;
syntax defines an associated type for the Deref
trait to use. Associated types are a slightly different way of declaring a generic parameter, but you don’t need to worry about them for now; we’ll cover them in more detail in later Chapter.
We fill in the body of the deref
method with &self.0
so deref
returns a reference to the value we want to access with the *
operator; and .0
accesses the first value in a tuple struct. The main
function that calls *
on the MyBox<T>
value now compiles, and the assertions pass!
Without the Deref
trait, the compiler can only dereference &
references. The deref
method gives the compiler the ability to take a value of any type that implements Deref
and call the deref
method to get an &
reference that it knows how to dereference.
When we entered *y
, behind the scenes Rust actually ran this code:
*(y.deref())
The reason the deref
method returns a reference to a value, and that the plain dereference outside the parentheses in *(y.deref())
is still necessary, has to do with the ownership system. If the deref
method returned the value directly instead of a reference to the value, the value would be moved out of self
. We don’t want to take ownership of the inner value inside MyBox<T>
in this case or in most cases where we use the dereference operator.
Note that the *
operator is replaced with a call to the deref
method and then a call to the *
operator just once, each time we use a *
in our code. Because the substitution of the *
operator does not recurse infinitely, we end up with data of type i32
, which matches the 5
in assert_eq!
.
Implicit Deref Coercions with Functions and Methods
Deref coercion converts a reference to a type that implements the Deref
trait into a reference to another type. For example, deref coercion can convert &String
to &str
because String
implements the Deref
trait such that it returns &str
. Deref coercion is a convenience Rust performs on arguments to functions and methods, and works only on types that implement the Deref
trait. It happens automatically when we pass a reference to a particular type’s value as an argument to a function or method that doesn’t match the parameter type in the function or method definition. A sequence of calls to the deref
method converts the type we provided into the type the parameter needs.
Deref coercion was added to Rust so that programmers writing function and method calls don’t need to add as many explicit references and dereferences with &
and *
. The deref coercion feature also lets us write more code that can work for either references or smart pointers.
Filename: src/main.rs
fn hello(name: &str) {
println!("Hello, {name}!");
}
Deref coercion makes it possible to call hello
with a reference to a value of type MyBox<String>
, as shown in the following Listing.
Filename: src/main.rs
fn main() {
let m = MyBox::new(String::from("Rust"));
hello(&m);
}
Because we implemented the Deref
trait on MyBox<T>
, Rust can turn &MyBox<String>
into &String
by calling deref
. The standard library provides an implementation of Deref
on String
that returns a string slice. Rust calls deref
again to turn the &String
into &str
, which matches the hello
function’s definition.
If Rust didn’t implement deref coercion, we would have to write the code in the following Listing instead of the code in the previous Listing to call hello
with a value of type &MyBox<String>
.
Filename: src/main.rs
fn main() {
let m = MyBox::new(String::from("Rust"));
hello(&(*m)[..]);
}
The (*m)
dereferences the MyBox<String>
into a String
. Then the &
and [..]
take a string slice of the String
that is equal to the whole string to match the signature of hello
. This code without deref coercions is harder to read, write, and understand with all of these symbols involved. Deref coercion allows Rust to handle these conversions for us automatically.
When the Deref
trait is defined for the types involved, Rust will analyze the types and use Deref::deref
as many times as necessary to get a reference to match the parameter’s type. The number of times that Deref::deref
needs to be inserted is resolved at compile time, so there is no runtime penalty for taking advantage of deref coercion!
How Deref Coercion Interacts with Mutability
Similar to how you use the Deref
trait to override the *
operator on immutable references, you can use the DerefMut
trait to override the *
operator on mutable references.
Rust does deref coercion when it finds types and trait implementations in three cases:
- From
&T
to &U
when T: Deref<Target=U>
- From
&mut T
to &mut U
when T: DerefMut<Target=U>
- From
&mut T
to &U
when T: Deref<Target=U>
The first two cases are the same except that the second implements mutability. The first case states that if you have a &T
, and T
implements Deref
to some type U
, you can get a &U
transparently. The second case states that the same deref coercion happens for mutable references.
The third case is trickier: Rust will also coerce a mutable reference to an immutable one. But the reverse is not possible: immutable references will never coerce to mutable references. Because of the borrowing rules, if you have a mutable reference, that mutable reference must be the only reference to that data (otherwise, the program wouldn’t compile). Converting one mutable reference to one immutable reference will never break the borrowing rules. Converting an immutable reference to a mutable reference would require that the initial immutable reference is the only immutable reference to that data, but the borrowing rules don’t guarantee that. Therefore, Rust can’t make the assumption that converting an immutable reference to a mutable reference is possible.
Running Code on Cleanup with the Drop Trait
The second trait important to the smart pointer pattern is Drop
, which lets you customize what happens when a value is about to go out of scope. You can provide an implementation for the Drop
trait on any type, and that code can be used to release resources like files or network connections.
In Rust, you can specify that a particular bit of code be run whenever a value goes out of scope, and the compiler will insert this code automatically. As a result, you don’t need to be careful about placing cleanup code everywhere in a program that an instance of a particular type is finished with - you still won’t leak resources!
You specify the code to run when a value goes out of scope by implementing the Drop
trait. The Drop
trait requires you to implement one method named drop
that takes a mutable reference to self
. To see when Rust calls drop
, let’s implement drop
with println!
statements for now.
The following Listing shows a CustomSmartPointer
struct whose only custom functionality is that it will print Dropping CustomSmartPointer!
when the instance goes out of scope, to show when Rust runs the drop
method.
Filename: src/main.rs
struct CustomSmartPointer {
data: String,
}
impl Drop for CustomSmartPointer {
fn drop(&mut self) {
println!("Dropping CustomSmartPointer with data `{}`!", self.data);
}
}
fn main() {
let c = CustomSmartPointer {
data: String::from("my stuff"),
};
let d = CustomSmartPointer {
data: String::from("other stuff"),
};
println!("CustomSmartPointers created.");
}
The Drop trait is included in the prelude, so we don’t need to bring it into scope. The body of the drop
method is where you would place any logic that you wanted to run when an instance of your type goes out of scope. We’re printing some text here to demonstrate visually when Rust will call drop
.
When we run this program, we’ll see the following output:
$ cargo run
Compiling drop-example v0.1.0 (file:///projects/drop-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.60s
Running `target/debug/drop-example`
CustomSmartPointers created.
Dropping CustomSmartPointer with data `other stuff`!
Dropping CustomSmartPointer with data `my stuff`!
Rust automatically called drop
for us when our instances went out of scope, calling the code we specified. Variables are dropped in the reverse order of their creation, so d
was dropped before c
.
Unfortunately, it’s not straightforward to disable the automatic drop
functionality.
Occasionally, however, you might want to clean up a value early. One example is when using smart pointers that manage locks: you might want to force the drop
method that releases the lock so that other code in the same scope can acquire the lock. Rust doesn’t let you call the Drop
trait’s drop
method manually; instead, you have to call the std::mem::drop
function provided by the standard library if you want to force a value to be dropped before the end of its scope.
If we try to call the Drop
trait’s drop
method manually, as shown in the following Listing, we’ll get a compiler error.
Filename: src/main.rs
fn main() {
let c = CustomSmartPointer {
data: String::from("some data"),
};
println!("CustomSmartPointer created.");
c.drop();
println!("CustomSmartPointer dropped before the end of main.");
}
When we try to compile this code, we’ll get this error:
$ cargo run
Compiling drop-example v0.1.0 (file:///projects/drop-example)
error[E0040]: explicit use of destructor method
--> src/main.rs:16:7
|
16 | c.drop();
| ^^^^ explicit destructor calls not allowed
|
help: consider using `drop` function
|
16 | drop(c);
| +++++ ~
For more information about this error, try `rustc --explain E0040`.
error: could not compile `drop-example` (bin "drop-example") due to 1 previous error
Rust doesn’t let us call drop
explicitly because Rust would still automatically call drop
on the value at the end of main
. This would cause a double free error because Rust would be trying to clean up the same value twice.
We can’t disable the automatic insertion of drop
when a value goes out of scope, and we can’t call the drop
method explicitly. So, if we need to force a value to be cleaned up early, we use the std::mem::drop
function.
The std::mem::drop
function is different from the drop
method in the Drop
trait. We call it by passing as an argument the value we want to force-drop. The function is in the prelude, so we can modify main
to call the drop
function, as shown in the following Listing.
Filename: src/main.rs
fn main() {
let c = CustomSmartPointer {
data: String::from("some data"),
};
println!("CustomSmartPointer created.");
drop(c);
println!("CustomSmartPOinter dropped before the end of main.");
}
Running this code will print the following:
$ cargo run
Compiling drop-example v0.1.0 (file:///projects/drop-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.73s
Running `target/debug/drop-example`
CustomSmartPointer created.
Dropping CustomSmartPointer with data `some data`!
CustomSmartPointer dropped before the end of main.
You can use code specified in a Drop
trait implementation in many ways to make cleanup convenient and safe: for instance, you could use it to create your own memory allocator! With the Drop
trait and Rust’s ownership system, you don’t have to remember to clean up because Rust does it automatically.
You also don’t have to worry about problems resulting from accidentally cleaning up values still in use: the ownership system that makes sure references are always valid also ensures that drop
gets called only once when the value is no longer being used.
Rc<T>, the Reference Counted Smart Pointer
In the majority of cases, ownership is clear: you know exactly which variable owns a given value. However, there are cases when a single value might have multiple owners. For example, in graph data structures, multiple edges might point to the same node, and that node is conceptually owned by all of the edges that point to it. A node shouldn’t be cleaned up unless it doesn’t have any edges pointing to it and so has no owners.
You have to enable multiple ownership explicitly by using the Rust type Rc<T>
, which is an abbreviation for reference counting. The Rc<T>
type keeps track of the number of references to a value to determine whether or not the value is still in use. If there are zero references to a value, the value can be cleaned up without any references becoming invalid.
We use the Rc<T>
type when we want to allocate some data on the heap for multiple parts of our program to read and we can’t determine at compile time which part will finish using the data last. If we knew which part would finish last, we could just make that part the data’s owner, and the normal ownership rules enforced at compile time would take effect.
Note that Rc<T>
is only for use in single-threaded scenarios.
Using Rc<T> to Share Data
Let’s return to our cons list example in previous Listing. Recall that we defined it using Box<t>
. This time, we’ll create two lists that both share ownership of a third list.
We’ll create list a
that contains 5
and then 10
. Then we’ll make two more lists: b
that starts with 3
and c
that starts with 4
. Both b
and c
lists will then continue on to the first a
list containing 5
and 10
. In other words, both lists will share the first list containing 5
and 10
.
Trying to implement this scenario using our definition of List with Box<T>
won’t work, as shown in the following Listing:
Filename: src/main.rs
enum List {
Cons(i32, Box<List>),
Nil,
}
use crate::List::{Cons, Nil};
fn main() {
let a = Cons(5, Box::new(Cons(10, Box::new(Nil))));
let b = Cons(3, Box::new(a));
let c = Cons(4, Box::new(a));
}
When we compile this code, we get this error:
$ cargo run
Compiling cons-list v0.1.0 (file:///projects/cons-list)
error[E0382]: use of moved value: `a`
--> src/main.rs:11:30
|
9 | let a = Cons(5, Box::new(Cons(10, Box::new(Nil))));
| - move occurs because `a` has type `List`, which does not implement the `Copy` trait
10 | let b = Cons(3, Box::new(a));
| - value moved here
11 | let c = Cons(4, Box::new(a));
| ^ value used here after move
For more information about this error, try `rustc --explain E0382`.
error: could not compile `cons-list` (bin "cons-list") due to 1 previous error
We could change the definition of Cons
to hold references instead, but then we would have to specify lifetime parameters. By specifying lifetime parameters, we would be specifying that every element in the list will live at least as long as the entire list. This is the case for the elements and lists, but not in every scenario.
Instead, we’ll change our definition of List
to use Rc<T>
in place of Box<T>
, as shown in the following Listing. Each Cons
variant will now hold a value and an Rc<T>
pointing to a List
. When we create b
, instead of taking ownership of a
, we’ll clone the Rc<List>
that a
is holding, thereby increasing the number of references from one to two and letting a
and b
share ownership of the data in that Rc<List>
. We’ll also clone a
when creating c
, increasing the number of references from two to three. Every time we call Rc::clone
, the reference count to the data within the Rc<List>
will increase, and the data won’t be cleaned up unless there are zero references to it.
Filename: src/main.rs
enum List {
Cons(i32, Rc<List>),
Nil,
}
use crate::List::{Cons, Nil};
use std::rc::Rc;
fn main() {
let a = Rc::new(Cons(5, Rc::new(Cons(10, Rc::new(Nil)))));
let b = Cons(3, Rc::clone(&a));
let c = Cons(4, Rc::clone(&a));
}
We need to add a use
statement to bring Rc<T>
into scope because it’s not in the prelude.
We could have called a.clone()
rather than Rc::clone(&a)
, but Rust’s convention is to use Rc::clone
in this case. The implementation of Rc::clone
doesn’t make a deep copy of all the data like most types’ implementations of clone
do. The call to Rc::clone
only increments the reference count, which doesn’t take much time. Deep copies of data can take a lot of time. By using Rc::clone
for reference counting, we can visually distinguish between the deep-copy kinds of clones and the kinds of clones that increase the reference count. When looking for performance problems in the code, we only need to consider the deep-copy clones and can disregard calls to Rc::clone
.
Cloning an Rc<T> Increases the Reference Count
Let’s change our working example so we can see the reference counts changing as we create and drop references to the Rc<List>
in a
.
In the following Listing, we’ll change main
so it has an inner scope around list c
; then we can see how the reference count changes when c
goes out of scope.
Filename: src/main.rs
fn main() {
let a = Rc::new(Cons(5, Rc::new(Cons(10, Rc:new(Nil)))));
println!("count after creating a = {}", Rc::strong_count(&a));
let b =Cons(3, Rc::clone(&a));
println!("count after creating b = {}", Rc::strong_count(&a));
{
let c = Cons(4, Rc::clone(&a));
println!("count after creating c = {}", Rc::strong_count(&a));
}
println!("count after c goes out of scope = {}", Rc::strong_count(&a));
}
At each point in the program where the reference count changes, we print the reference count, which we get by calling the Rc::strong_count
function. This function is named strong_count
rather than count
because the Rc<T>
type also has a weak_count
; we’ll see what weak_count
is used for in “Preventing Reference Cycles Using Weak<T>“.
This code prints the following:
$ cargo run
Compiling cons-list v0.1.0 (file:///projects/cons-list)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.45s
Running `target/debug/cons-list`
count after creating a = 1
count after creating b = 2
count after creating c = 3
count after c goes out of scope = 2
We don’t have to call a function to decrease the reference count like we have to call Rc::clone
to increase the reference count: the implementation of the Drop
trait decreases the reference count automatically when an Rc<T>
value goes out of scope.
What we can’t see in this example is that when b
and then a
go out of scope at the end of main
, the count is then 0, and the Rc<List>
is cleaned up completely. Using Rc<T>
allows a single value to have multiple owners, and the count ensures that the value remains valid as long as any of the owners still exist.
Via immutable references, Rc<T>
allows you to share data between multiple parts of your program for reading only. If Rc<T>
allowed you to have multiple mutable references too, you might violate one of the borrowing rules: multiple mutable borrows to the same place can cause data races and inconsistencies.
RefCell<T> and the Interior Mutability Pattern
Interior mutability is a design pattern in Rust that allows you to mutate data even when there are immutable references to that data; normally, this action is disallowed by the borrowing rules. To mutate data, the pattern uses unsafe
code inside a data structure to bend Rust’s usual rules that govern mutation and borrowing. Unsafe code indicates to the compiler that we’re checking the rules manually instead of relying on the compiler to check them for us.
We can use types that use the interior mutability pattern only when we can ensure that the borrowing rules will be followed at runtime, even though the compiler can’t guarantee that. The unsafe
code involved is then wrapped in a safe API, and the outer type is still immutable.
Enforcing Borrowing Rules at Runtime with RefCell<T>
Unlike Rc<T>
, the RefCell<T>
type represents single ownership over the data it holds. So what makes RefCell<T>
different from a type like Box<T>
? Recall the borrowing rules you learned:
At any given time, you can have either one mutable reference or any number of immutable references (but not both).
References must always be valid.
With references and Box<T>
, the borrowing rules’ invariants are enforced at compile time. With RefCell<T>
, these invariants are enforced at runtime. With references, if you break these rules, you’ll get a compiler error. With RefCell<T>
, if you break these rules, your program will panic and exit.
The advantages of checking the borrowing rules at compile time are that errors will be caught sooner in the development process, and there is no impact on runtime performance because all the analysis is completed beforehand. For those reasons, checking the borrowing rules at compile time is the best choice in the majority of cases, which is why this is Rust’s default.
The advantage of checking the borrowing rules at runtime instead is that certain memory-safe scenarios are then allowed, where they would’ve been disallowed by the compile-time checks. Static analysis, like the Rust compiler, is inherently conservative.
Because some analysis is impossible, if the Rust compiler can’t be sure the code complies with the ownership rules, it might reject a correct program; in this way, it’s conservative. If Rust accepted an incorrect program, users wouldn’t be able to trust in the guarantees Rust makes. However, if Rust rejects a correct program, the programmer will be inconvenienced, but nothing catastrophic can occur. The RefCell<T>
type is useful when you’re sure your code follows the borrowing rules but the compiler is unable to understand and guarantee that.
Similar to Rc<T>
, RefCell<T>
is only for use in single-threaded scenarios and will give you a compile-time error if you try using it in a multithreaded context.
Here is a recap of the reasons to choose Box<T>
, Rc<T>
, or RefCell<T>
:
Rc<T>
enables multiple owners of the same data; Box<T>
and RefCell<T>
have single owners.
Box<T>
allows immutable or mutable borrows checked at compile time; Rc<T>
allows only immutable borrows checked at compile time; RefCell<T>
allows immutable or mutable borrows checked at runtime.
- Because
RefCell<T>
allows mutable borrows checked at runtime, you can mutate the value inside the RefCell<T>
even when the RefCell<T>
is immutable.
Mutating the value inside an immutable value is the interior mutability pattern.
Interior Mutability: A Mutable Borrow to an Immutable Value
A consequence of the borrowing rules is that when you have an immutable value, you can’t borrow it mutably. For example, this code won’t compile:
fn main() {
let x = 5;
let y = &mut x;
}
If you tried to compile this code, you’d get the following error:
$ cargo run
Compiling borrowing v0.1.0 (file:///projects/borrowing)
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> src/main.rs:3:13
|
3 | let y = &mut x;
| ^^^^^^ cannot borrow as mutable
|
help: consider changing this to be mutable
|
2 | let mut x = 5;
| +++
For more information about this error, try `rustc --explain E0596`.
error: could not compile `borrowing` (bin "borrowing") due to 1 previous error
However, there are situations in which it would be useful for a value to mutate itself in its methods but appear immutable to other code. Code outside the value’s methods would not be able to mutate the value. Using RefCell<T>
is one way to get the ability to have interior mutability, but RefCell<T>
doesn’t get around the borrowing rules completely: the borrow checker in the compiler allows this interior mutability, and the borrowing rules are checked at runtime instead. If you violate the rules, you’ll get a panic!
instead of a compiler error.
A Use Case for Interior Mutability: Mock Objects
Sometimes during testing a programmer will use a type in place of another type, in order to observe particular behavior and assert that it’s implemented correctly. This placeholder type is called a test double. Test doubles stand in for other types when we’re running tests. Mock objects are specific types of test doubles that record what happens during a test so you can assert that the correct actions took place.
Here’s the scenario we’ll test: we’ll create a library that tracks a value against a maximum value and sends messages based on how close to the maximum value the current value is. This library could be used to keep track of a user’s quota for the number of API calls they’re allowed to make, for example.
Filename: src/lib.rs
pub trait Messenger {
fn send(&self, msg: &str);
}
pub struct LimitTracker<'a, T: Messenger> {
messenger: &'a T,
value: usize,
max: usize,
}
impl<'a, T> LimitTracker<'a, T>
where
T: Messenger,
{
pub fn new(messenger: &'a T, max: usize) -> LimitTracker<'a, T> {
LimitTracker {
messenger,
value: 0,
max,
}
}
pub fn set_value(&mut self, value: usize) {
self.value = value;
let percentage_of_max = self.value as f64 / self.max as f64;
if percentage_of_max >= 1.0 {
self.messenger.send("Error: You are over your quota!");
} else if percentage_of_max >= 0.9 {
self.messenger
.send("Urgent warning: You've used up over 90% of your quota!");
} else if percentage_of_max >= 0.75 {
self.messenger
.send("Warning: You've used up over 75% of your quota!");
}
}
}
We need a mock object that, instead of sending an email or text message when we call send
, will only keep track of the messages it’s told to send. We can create a new instance of the mock object, create a LimitTracker
that uses the mock object, call the set_value
method on LimitTracker
, and then check that the mock object has the messages we expect. The following Listing shows an attempt to implement a mock object to do just that, but the borrow checker won’t allow it.
Filename: src/lib.rs
#[cfg(test)]
mod tests {
use super::*;
struct MockMessenger {
sent_messages: Vec<String>,
}
impl MockMessenger {
fn new() -> MockMessenger {
MockMessenger {
sent_messages: vec![],
}
}
}
impl Messenger for MockMessenger {
fn send(&self, message: &str) {
self.sent_messages.push(String::from(message));
}
}
#[test]
fn it_sends_an_over_75_percent_warning_message() {
let mock_messenger = MockMessenger::new();
let mut limit_tracker = LimitTracker::new(&mock_messenger, 100);
limit_tracker.set_value(80);
assert_eq!(mock_messenger.sent_messages.len(), 1);
}
}
However, there’s one problem with this test, as shown here:
$ cargo test
Compiling limit-tracker v0.1.0 (file:///projects/limit-tracker)
error[E0596]: cannot borrow `self.sent_messages` as mutable, as it is behind a `&` reference
--> src/lib.rs:58:13
|
58 | self.sent_messages.push(String::from(message));
| ^^^^^^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference in the `impl` method and the `trait` definition
|
2 ~ fn send(&mut self, msg: &str);
3 | }
...
56 | impl Messenger for MockMessenger {
57 ~ fn send(&mut self, message: &str) {
|
For more information about this error, try `rustc --explain E0596`.
error: could not compile `limit-tracker` (lib test) due to 1 previous error
We can’t modify the MockMessenger
to keep track of the messages, because the send
method takes an immutable reference to self
. We also can’t take the suggestion from the error text to use &mut self
in both the impl
method and the trait
definition. We do not want to change the Messenger
trait solely for the sake of testing. Instead, we need to find a way to make our test code work correctly with our existing design.
This is a situation in which interior mutability can help! We’ll store the sent_messages
within a RefCell<T>
, and then the send
method will be able to modify sent_messages
to store the messages we’ve seen. The following Listing shows what that looks like.
Filename: src/lib.rs
#[cfg(test)]
mod tests {
use super::*;
use std::cell::RefCell;
struct MockMessenger {
sent_messages: RefCell<Vec<String>>,
}
impl MockMessenger {
fn new() -> MockMessenger {
MockMessenger {
sent_messages: RefCell::new(vec![]),
}
}
}
impl Messenger for MockMessenger {
fn send(&self, message: &str) {
self.sent_messages.borrow_mut().push(String::from(message));
}
}
#[test]
fn it_sends_an_over_75_percent_warning_message() {
assert_eq!(mock_messenger.sent_messages.borrow().len(), 1);
}
}
For the implementation of the send
method, the first parameter is still an immutable borrow of self
, which matches the trait definition. We call borrow_mut
on the RefCell<Vec<String>>
in self.sent_messages
to get a mutable reference to the value inside the RefCell<Vec<String>>
, which is the vector.
The last change we have to make is in the assertion: to see how many items are in the inner vector, we call borrow
on the RefCell<Vec<String>>
to get an immutable reference to the vector.
Keeping Track of Borrows at Runtime with RefCell<T>
When creating immutable and mutable references, we use the &
and &mut
syntax, respectively. With RefCell<T>
, we use the borrow
and borrow_mut
methods, which are part of the safe API that belongs to RefCell<T>
. The borrow
method returns the smart pointer type Ref<T>
, and borrow_mut
returns the smart pointer type RefMut<T>
. Both types implement Deref
, so we can treat them like regular references.
The RefCell<T>
keeps track of how many Ref<T>
and RefMut<T>
smart pointers are currently active. Every time we call borrow
, the RefCell<T>
increases its count of how many immutable borrows are active. When a Ref<T>
value goes out of scope, the count of immutable borrows goes down by 1. Just like the compile-time borrowing rules, RefCell<T>
lets us have many immutable borrows or one mutable borrow at any point in time.
If we try to violate these rules, rather than getting a compiler error as we would with references, the implementation of RefCell<T>
will panic at runtime.
Filename: src/lib.rs
impl Messenger for MockMessenger {
fn send(&self, message: &str) {
let mut one_borrow = self.sent_messages.borrow_mut();
let mut two_borrow = self.sent_messages.borrow_mut();
one_borrow.push(String::from(message));
two_borrow.push(String::from(message));
}
}
This makes two mutable references in the same scope, which isn’t allowed. When we run the tests for our library, the code will compile without any errors, but the test will fail:
$ cargo test
Compiling limit-tracker v0.1.0 (file:///projects/limit-tracker)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.91s
Running unittests src/lib.rs (target/debug/deps/limit_tracker-e599811fa246dbde)
running 1 test
test tests::it_sends_an_over_75_percent_warning_message ... FAILED
failures:
---- tests::it_sends_an_over_75_percent_warning_message stdout ----
thread 'tests::it_sends_an_over_75_percent_warning_message' panicked at src/lib.rs:60:53:
already borrowed: BorrowMutError
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::it_sends_an_over_75_percent_warning_message
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--lib`
Notice that the code panicked with the message already borrowed: BorrowMutError
. This is how RefCell<T>
handles violations of the borrowing rules at runtime.
Choosing to catch borrowing errors at runtime rather than compile time, as we’ve done here, means you’d potentially be finding mistakes in your code later in the development process: possibly not until your code was deployed to production.
Also, your code would incur a small runtime performance penalty as a result of keeping track of the borrows at runtime rather than compile time. However, using RefCell<T>
makes it possible to write a mock object that can modify itself to keep track of the messages it has seen while you’re using it in a context where only immutable values are allowed. You can use RefCell<T>
despite its trade-offs to get more functionality than regular references provide.
Allowing Multiple Owners of Mutable Data with Rc<T> and RefCell<T>
A common way to use RefCell<T>
is in combination with Rc<T>
. Recall that Rc<T>
lets you have multiple owners of some data, but it only gives immutable access to that data. If you have an Rc<T>
that holds a RefCell<T>
, you can get a value that can have multiple owners and that you can mutate!
For example, recall the cons list example in the previous Listing where we used Rc<T>
to allow multiple lists to share ownership of another list. Because Rc<T>
holds only immutable values, we can’t change any of the values in the list once we’ve created them. Let’s add in RefCell<T>
for its ability to change the values in the lists. The following Listing shows that by using a RefCell<T>
in the Cons
definition, we can modify the value stored in all the lists.
Filename: src/main.rs
#[derive(Debug)]
enum List {
Cons(Rc<RefCell<i32>>, Rc<List>),
Nil,
}
use crate::List::{Cons, Nil};
use std::cell::RefCell;
use std::rc::Rc;
fn main() {
let value = Rc::new(RefCell::new(5));
let a = Rc::new(Cons(Rc::clone(&value), Rc::new(Nil)));
let b = Cons(Rc::new(RefCell::new(3)), Rc::clone(&a));
let c = Cons(Rc::new(RefCell::new(4)), Rc::clone(&a));
*value.borrow_mut() += 10;
println!("a after = {a:?}");
println!("b after = {b:?}");
println!("c after = {c:?}");
}
When we print a
, b
, and c
, we can see that they all have the modified value of 15
rather than 5
:
$ cargo run
Compiling cons-list v0.1.0 (file:///projects/cons-list)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.63s
Running `target/debug/cons-list`
a after = Cons(RefCell { value: 15 }, Nil)
b after = Cons(RefCell { value: 3 }, Cons(RefCell { value: 15 }, Nil))
c after = Cons(RefCell { value: 4 }, Cons(RefCell { value: 15 }, Nil))
This technique is pretty neat! By using RefCell<T>
, we have an outwardly immutable List
value. But we can use the methods on RefCell<T>
that provide access to its interior mutability so we can modify our data when we need to. The runtime checks of the borrowing rules protect us from data races, and it’s sometimes worth trading a bit of speed for this flexibility in our data structures. Note that RefCell<T>
does not work for multithreaded code! Mutex<T>
is the thread-safe version of RefCell<T>
, and we’ll discuss Mutex<T>
in next Chapter.
Reference Cycles Can Leak Memory
Rust’s memory safety guarantees make it difficult, but not impossible, to accidentally create memory that is never cleaned up (known as a memory leak). Preventing memory leaks entirely is not one of Rust’s guarantees, meaning memory leaks are memory safe in Rust. We can see that Rust allows memory leaks by using Rc<T>
and RefCell<T>
: it’s possible to create references where items refer to each other in a cycle. This creates memory leaks because the reference count of each item in the cycle will never reach 0, and the values will never be dropped.
Creating a Reference Cycle
Let’s look at how a reference cycle might happen and how to prevent it, starting with the definition of the List
enum and a tail
method in the following Listing.
Filename: src/main.rs
use crate::List::{Cons, Nil};
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug)]
enum List {
Cons(i32, RefCell<Rc<List>>),
Nil,
}
impl List {
fn tail(&self) -> Option<&RefCell<Rc<List>>> {
match self {
Cons(_, item) => Some(item),
Nil => None,
}
}
}
fn main() {}
We want to modify the List
value a Cons
variant is pointing to. We’re also adding a tail
method to make it convenient for us to access the second item if we have a Cons
variant.
In the following Listing, The code creates a list in a
and a list in b
that points to the list in a
. Then it modifies the list in a
to point to b
, creating a reference cycle. There are println!
statements along the way to show what the reference counts are at various points in this process.
Filename: src/main.rs
fn main() {
let a = Rc::new(Cons(5, RefCell::new(Rc::new(Nil))));
println!("a initial rc count = {}", Rc::strong_count(&a));
println!("a next item = {:?}", a.tail());
let b = Rc::new(Cons(10, RefCell::new(Rc::clone(&a))));
println!("a rc count after b creation = {}", Rc::strong_count(&a));
println!("b initial rc count = {}", Rc::strong_count(&b));
println!("b next item = {:?}", b.tail());
if let Some(link) = a.tail() {
*link.borrow_mut() = Rc::clone(&b);
}
println!("b rc count after changing a = {}", Rc::strong_count(&b));
println!("a rc count after changing a = {}", Rc::strong_count(&a));
}
When we run this code, keeping the last println!
commented out for the moment, we’ll get this output:
$ cargo run
Compiling cons-list v0.1.0 (file:///projects/cons-list)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.53s
Running `target/debug/cons-list`
a initial rc count = 1
a next item = Some(RefCell { value: Nil })
a rc count after b creation = 2
b initial rc count = 1
b next item = Some(RefCell { value: Cons(5, RefCell { value: Nil }) })
b rc count after changing a = 2
a rc count after changing a = 2
The reference count of the Rc<List>
instances in both a
and b
is 2 after we change the list in a
to point to b
. At the end of main
, Rust drops the variable b
, which decreases the reference count of the b
Rc<List>
instance from 2 to 1. The memory that Rc<List>
has on the heap won’t be dropped at this point, because its reference count is 1, not 0. Then Rust drops a
, which decreases the reference count of the a
Rc<List>
instance from 2 to 1 as well. This instance’s memory can’t be dropped either, because the other Rc<List>
instance still refers to it. The memory allocated to the list will remain uncollected forever.
If you uncomment the last println!
and run the program, Rust will try to print this cycle with a
pointing to b
pointing to a
and so forth until it overflows the stack.
However, if a more complex program allocated lots of memory in a cycle and held onto it for a long time, the program would use more memory than it needed and might overwhelm the system, causing it to run out of available memory.
If you have RefCell<T>
values that contain Rc<T>
values or similar nested combinations of types with interior mutability and reference counting, you must ensure that you don’t create cycles; you can’t rely on Rust to catch them. Creating a reference cycle would be a logic bug in your program that you should use automated tests, code reviews, and other software development practices to minimize.
Another solution for avoiding reference cycles is reorganizing your data structures so that some references express ownership and some references don’t. As a result, you can have cycles made up of some ownership relationships and some non-ownership relationships, and only the ownership relationships affect whether or not a value can be dropped.
In the previous Listing, we always want Cons
variants to own their list, so reorganizing the data structure isn’t possible. Let’s look at an example using graphs made up of parent nodes and child nodes to see when non-ownership relationships are an appropriate way to prevent reference cycles.
Preventing Reference Cycles Using Weak<T>
So far, we’ve demonstrated that calling Rc::clone
increases the strong_count
of an Rc<T>
instance, and an Rc<T>
instance is only cleaned up if its strong_count
is 0. You can also create a weak reference to the value within an Rc<T>
instance by calling Rc::downgrade
and passing a reference to the Rc<T>
. Strong references are how you can share ownership of an Rc<T>
instance. Weak references don’t express an ownership relationship, and their count doesn’t affect when an Rc<T>
instance is cleaned up. They won’t cause a reference cycle because any cycle involving some weak references will be broken once the strong reference count of values involved is 0.
When you call Rc::downgrade
, you get a smart pointer of type Weak<T>
. Instead of increasing the strong_count
in the Rc<T>
instance by 1, calling Rc::downgrade
increases the weak_count
by 1. The Rc<T>
type uses weak_count
to keep track of how many Weak<T>
references exist, similar to strong_count
. The difference is the weak_count
doesn’t need to be 0 for the Rc<T>
instance to be cleaned up.
Because the value that Weak<T>
references might have been dropped, to do anything with the value that a Weak<T>
is pointing to you must make sure the value still exists. Do this by calling the upgrade
method on a Weak<T>
instance, which will return an Option<Rc<T>>
. You’ll get a result of Some
if the Rc<T>
value has not been dropped yet and a result of None
if the Rc<T>
value has been dropped. Because upgrade
returns an Option<Rc<T>>
, Rust will ensure that the Some
case and the None
case are handled, and there won’t be an invalid pointer.
Creating a Tree Data Structure: A Node with Child Nodes
To start, we’ll build a tree with nodes that know about their child nodes. We’ll create a struct named Node
that holds its own i32
value as well as references to its children Node
values:
Filename: src/main.rs
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug)]
struct Node {
value: i32,
children: RefCell<Vec<Rc<Node>>>,
}
Next, we’ll use our struct definition and create one Node
instance named leaf
with the value 3
and no children, and another instance named branch
with the value 5
and leaf
as one of its children, as shown in the following Listing.
Filename: src/main.rs
fn main() {
let leaf = Rc::new(Node {
value: 3,
children: RefCell::new(vec![]),
});
let branch = Rc::new(Node {
value: 5,
children: RefCell::new(vec![Rc::clone(&leaf)]),
});
}
We clone the Rc<Node>
in leaf
and store that in branch
, meaning the Node
in leaf
now has two owners: leaf
and branch
. We can get from branch
to leaf
through branch.children
, but there’s no way to get from leaf
to branch
. The reason is that leaf
has no reference to branch
and doesn’t know they’re related. We want leaf
to know that branch
is its parent. We’ll do that next.
Adding a Reference from a Child to Its Parent
To make the child node aware of its parent, we need to add a parent
field to our Node
struct definition. The trouble is in deciding what the type of parent
should be. We know it can’t contain an Rc<T>
because that would create a reference cycle with leaf.parent
pointing to branch
and branch.children
pointing to leaf
, which would cause their strong_count
values to never be 0.
Thinking about the relationships another way, a parent node should own its children: if a parent node is dropped, its child nodes should be dropped as well. However, a child should not own its parent: if we drop a child node, the parent should still exist. This is a case for weak references!
So, instead of Rc<T>
, we’ll make the type of parent
use Weak<T>
, specifically a RefCell<Weak<Node>>
. Now our Node
struct definition looks like this:
Filename: src/main.rs
use std::cell::RefCell;
use std::rc::{Rc, Weak};
#[derive(Debug)]
struct Node {
value: i32,
parent: RefCell<Weak<Node>>,
children: RefCell<Vec<Rc<Node>>>,
}
A node will be able to refer to its parent node but doesn’t own its parent. In the following Listing, we update main
to use this new definition so the leaf
node will have a way to refer to its parent, branch
.
Filename: src/main.rs
fn main() {
let leaf = Rc::new(Node {
value: 3,
parent: RefCell::new(Weak::new()),
children: RefCell::new(vec![]),
});
println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
let branch = Rc::new(Node {
value: 5,
parent: RefCell::new(Weak::new()),
children: RefCell::new(vec![Rc::clone(&leaf)]),
});
*leaf.parent.borrow_mut() = Rc::downgrade(&branch);
println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
}
The lack of infinite output indicates that this code didn’t create a reference cycle. We can also tell this by looking at the values we get from calling Rc::strong_count
and Rc::weak_count
.
Visualizing Changes to strong_count and weak_count
Let’s look at how the strong_count
and weak_count
values of the Rc<Node>
instances change by creating a new inner scope and moving the creation of branch
into that scope. By doing so, we can see what happens when branch
is created and then dropped when it goes out of scope. The modifications are shown in the following Listing.
Filename: src/main.rs
fn main() {
let leaf = Rc::new(Node {
value: 3,
parent: RefCell::new(Weak::new()),
children: RefCell::new(vec![]),
});
println!(
"leaf strong = {}, weak = {}",
Rc::strong_count(&leaf),
Rc::weak_count(&leaf),
);
{
let branch = Rc::new(Node {
value: 5,
parent: RefCell::new(Weak::new()),
children: RefCell::new(vec![Rc::clone(&leaf)]),
});
*leaf.parent.borrow_mut() = Rc::downgrade(&branch);
println!(
"branch strong = {}, weak = {}",
Rc::strong_count(&branch),
Rc::weak_count(&branch),
);
println!(
"leaf strong = {}, weak = {}",
Rc::strong_count(&leaf),
Rc::weak_count(&leaf),
);
}
println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
println!(
"leaf strong = {}, weak = {}",
Rc::strong_count(&leaf),
Rc::weak_count(&leaf),
);
}
After leaf
is created, its Rc<Node>
has a strong count of 1 and a weak count of 0. In the inner scope, we create branch
and associate it with leaf
, at which point when we print the counts, the Rc<Node>
in branch
will have a strong count of 1 and a weak count of 1 (for leaf.parent
pointing to branch
with a Weak<Node>
). When we print the counts in leaf
, we’ll see it will have a strong count of 2 because branch
now has a clone of the Rc<Node>
of leaf
stored in branch.children
, but will still have a weak count of 0.
When the inner scope ends, branch
goes out of scope and the strong count of the Rc<Node>
decreases to 0, so its Node
is dropped. The weak count of 1 from leaf.parent
has no bearing on whether or not Node
is dropped, so we don’t get any memory leaks!
If we try to access the parent of leaf
after the end of the scope, we’ll get None
again. At the end of the program, the Rc<Node>
in leaf
has a strong count of 1 and a weak count of 0, because the variable leaf
is now the only reference to the Rc<Node>
again.
Fearless Concurrency
Using Threads to Run Code Simultaneously
Splitting the computation in your program into multiple threads to run multiple tasks at the same time can improve performance, but it also adds complexity. Because threads can run simultaneously, there’s no inherent guarantee about the order in which parts of your code on different threads will run. This can lead to problems, such as:
- Race conditions, in which threads are accessing data or resources in an inconsistent order
- Deadlocks, in which two threads are waiting for each other, preventing both threads from continuing
- Bugs that happen only in certain situations and are hard to reproduce and fix reliably
The Rust standard library uses a 1:1 model of thread implementation, whereby a program uses one operating system thread per one language thread. There are crates that implement other models of threading that make different tradeoffs to the 1:1 model. (Rust’s async system, which we will see in the next chapter, provides another approach to concurrency as well.)
Creating a New Thread with spawn
To create a new thread, we call the thread::spawn
function and pass it a closure containing the code we want to run in the new thread. The example in the following Listing prints some text from a main thread and other text from a new thread:
Filename: src/main.rs
use std::thread;
use std::time::Duration;
fn main() {
thread::spawn(|| {
for i in 1..10 {
println!("hi number {i} from the spawned thread!");
thread::sleep(Duration::from_millis(1));
}
});
for i in 1..5 {
println!("hi number {i} from the main thread!");
thread::sleep(Duration::from_millis(1));
}
}
Note that when the main thread of a Rust program completes, all spawned threads are shut down, whether or not they have finished running. The output from this program might be a little different every time, but it will look similar to the following:
hi number 1 from the main thread!
hi number 1 from the spawned thread!
hi number 2 from the main thread!
hi number 2 from the spawned thread!
hi number 3 from the main thread!
hi number 3 from the spawned thread!
hi number 4 from the main thread!
hi number 4 from the spawned thread!
hi number 5 from the spawned thread!
The calls to thread::sleep
force a thread to stop its execution for a short duration, allowing a different thread to run. The threads will probably take turns, but that isn’t guaranteed: it depends on how your operating system schedules the threads. In this run, the main thread printed first, even though the print statement from the spawned thread appears first in the code. And even though we told the spawned thread to print until i
is 9
, it only got to 5
before the main thread shut down.
If you run this code and only see output from the main thread, or don’t see any overlap, try increasing the numbers in the ranges to create more opportunities for the operating system to switch between the threads.
Waiting for All Threads to Finish Using join Handles
The code in the previous Listing not only stops the spawned thread prematurely most of the time due to the main thread ending, but because there is no guarantee on the order in which threads run, we also can’t guarantee that the spawned thread will get to run at all!
We can fix the problem of the spawned thread not running or ending prematurely by saving the return value of thread::spawn
in a variable. The return type of thread::spawn
is JoinHandle<T>
. A JoinHandle<T>
is an owned value that, when we call the join method on it, will wait for its thread to finish. The following Listing shows how to use the JoinHandle<T>
of the thread and how to call join
to make sure the spawned thread finishes before main
exits.
Filename: src/main.rs
use std::thread;
use std::time::Duration;
fn main() {
let handle = thread::spawn(|| {
for i in 1..10 {
println!("hi number {i} from the spawned thread!");
thread::sleep(Duration::from_millis(1));
}
});
for i in 1..5 {
println!("hi number {i} from the main thread!");
thread::sleep(Duration::from_millis(1));
}
handle.join().unwrap();
}
Calling join
on the handle blocks the thread currently running until the thread represented by the handle terminates. Blocking a thread means that thread is prevented from performing work or exiting.
hi number 1 from the main thread!
hi number 2 from the main thread!
hi number 1 from the spawned thread!
hi number 3 from the main thread!
hi number 2 from the spawned thread!
hi number 4 from the main thread!
hi number 3 from the spawned thread!
hi number 4 from the spawned thread!
hi number 5 from the spawned thread!
hi number 6 from the spawned thread!
hi number 7 from the spawned thread!
hi number 8 from the spawned thread!
hi number 9 from the spawned thread!
The two threads continue alternating, but the main thread waits because of the call to handle.join()
and does not end until the spawned thread is finished.
But let’s see what happens when we instead move handle.join()
before the for
loop in main
, like this:
Filename: src/main.rs
use std::thread;
use std::time::Duration;
fn main() {
let handle = thread::spawn(|| {
for i in 1..10 {
println!("hi number {i} from the spawned thread!");
thread::sleep(Duration::from_millis(1));
}
});
handle.join().unwrap();
for i in 1..5 {
println!("hi number {i} from the main thread!");
thread::sleep(Duration::from_millis(1));
}
}
The main thread will wait for the spawned thread to finish and then run its for
loop, so the output won’t be interleaved anymore, as shown here:
hi number 1 from the spawned thread!
hi number 2 from the spawned thread!
hi number 3 from the spawned thread!
hi number 4 from the spawned thread!
hi number 5 from the spawned thread!
hi number 6 from the spawned thread!
hi number 7 from the spawned thread!
hi number 8 from the spawned thread!
hi number 9 from the spawned thread!
hi number 1 from the main thread!
hi number 2 from the main thread!
hi number 3 from the main thread!
hi number 4 from the main thread!
Small details, such as where join
is called, can affect whether or not your threads run at the same time.
Using move Closures with Threads
We’ll often use the move
keyword with closures passed to thread::spawn
because the closure will then take ownership of the values it uses from the environment, thus transferring ownership of those values from one thread to another.
Notice that the closure we pass to thread::spawn
takes no arguments: we’re not using any data from the main thread in the spawned thread’s code. To use data from the main thread in the spawned thread, the spawned thread’s closure must capture the values it needs. The following Listing shows an attempt to create a vector in the main thread and use it in the spawned thread. However, this won’t work yet, as you’ll see in a moment.
Filename: src/main.rs
use std::thread;
fn main() {
let v = vec![1, 2, 3];
let handle = thread::spawn(|| {
println!("Here's a vector: {v:?}");
});
handle.join().unwrap();
}
The closure uses v
, so it will capture v
and make it part of the closure’s environment. Because thread::spawn
runs this closure in a new thread, we should be able to access v
inside that new thread. But when we compile this example, we get the following error:
$ cargo run
Compiling threads v0.1.0 (file:///projects/threads)
error[E0373]: closure may outlive the current function, but it borrows `v`, which is owned by the current function
--> src/main.rs:6:32
|
6 | let handle = thread::spawn(|| {
| ^^ may outlive borrowed value `v`
7 | println!("Here's a vector: {v:?}");
| - `v` is borrowed here
|
note: function requires argument type to outlive `'static`
--> src/main.rs:6:18
|
6 | let handle = thread::spawn(|| {
| __________________^
7 | | println!("Here's a vector: {v:?}");
8 | | });
| |______^
help: to force the closure to take ownership of `v` (and any other referenced variables), use the `move` keyword
|
6 | let handle = thread::spawn(move || {
| ++++
For more information about this error, try `rustc --explain E0373`.
error: could not compile `threads` (bin "threads") due to 1 previous error
Rust infers how to capture v
, and because println!
only needs a reference to v
, the closure tries to borrow v
. However, there’s a problem: Rust can’t tell how long the spawned thread will run, so it doesn’t know whether the reference to v
will always be valid.
The following Listing provides a scenario that’s more likely to have a reference to v
that won’t be valid:
Filename: src/main.rs
use std::thread;
fn main() {
let v = vec![1, 2, 3];
let handle = thread::spawn(|| {
println!("Here's a vector: {v:?}");
});
drop(v);
handle.join().unwrap();
}
If Rust allowed us to run this code, there’s a possibility that the spawned thread would be immediately put in the background without running at all. The spawned thread has a reference to v
inside, but the main thread immediately drops v
, using the drop
function. Then, when the spawned thread starts to execute, v
is no longer valid, so a reference to it is also invalid. Oh no!
By adding the move
keyword before the closure, we force the closure to take ownership of the values it’s using rather than allowing Rust to infer that it should borrow the values. The modification will compile and run as we intend.
Filename: src/main.rs
use std::thread;
fn main() {
let v = vec![1, 2, 3];
let handle = thread::spawn(move || {
println!("Here's a vector: {v:?}");
});
handle.join().unwrap();
}
We might be tempted to try the same thing to fix the code in the Listing where the main thread called drop
by using a move
closure. However, this fix will not work because what the Listing is trying to do is disallowed for a different reason. If we added move
to the closure, we would move v
into the closure’s environment, and we could no longer call drop
on it in the main thread. We would get this compiler error instead:
$ cargo run
Compiling threads v0.1.0 (file:///projects/threads)
error[E0382]: use of moved value: `v`
--> src/main.rs:10:10
|
4 | let v = vec![1, 2, 3];
| - move occurs because `v` has type `Vec<i32>`, which does not implement the `Copy` trait
5 |
6 | let handle = thread::spawn(move || {
| ------- value moved into closure here
7 | println!("Here's a vector: {v:?}");
| - variable moved due to use in closure
...
10 | drop(v); // oh no!
| ^ value used here after move
For more information about this error, try `rustc --explain E0382`.
error: could not compile `threads` (bin "threads") due to 1 previous error
We got an error from the code in because Rust was being conservative and only borrowing v
for the thread, which meant the main thread could theoretically invalidate the spawned thread’s reference.. By telling Rust to move ownership of v
to the spawned thread, we’re guaranteeing to Rust that the main thread won’t use v
anymore. If we change the Listing in the same way, we’re then violating the ownership rules when we try to use v
in the main thread. The move
keyword overrides Rust’s conservative default of borrowing; it doesn’t let us violate the ownership rules.
Using Message Passing to Transfer Data Between Threads
One increasingly popular approach to ensuring safe concurrency is message passing, where threads or actors communicate by sending each other messages containing data.
A channel is a general programming concept by which data is sent from one thread to another.
A channel has two halves: a transmitter and a receiver. One part of your code calls methods on the transmitter with the data you want to send, and another part checks the receiving end for arriving messages. A channel is said to be closed if either the transmitter or receiver half is dropped.
First, in the following Listing, we’ll create a channel but not do anything with it. Note that this won’t compile yet because Rust can’t tell what type of values we want to send over the channel.
Filename: src/main.rs
use std::sync::mpsc;
fn main() {
let (tx, rx) = mpsc::channel();
}
We create a new channel using the mpsc::channel
function; mpsc
stands for multiple producer, single consumer. In short, the way Rust’s standard library implements channels means a channel can have multiple sending ends that produce values but only one receiving end that consumes those values.
The mpsc::channel
function returns a tuple, the first element of which is the sending end - the transmitter - and the second element of which is the receiving end - the receiver. The abbreviations tx
and rx
are traditionally used in many fields for transmitter and receiver, respectively, so we name our variables as such to indicate each end.
Let’s move the transmitting end into a spawned thread and have it send one string so the spawned thread is communicating with the main thread, as shown in the following Listing.
Filename: src/main.rs
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let val = String::from("hi");
tx.send(val).unwrap();
});
}
The transmitter has a send
method that takes the value we want to send. The send
method returns a Result<T, E>
type, so if the receiver has already been dropped and there’s nowhere to send a value, the send operation will return an error. In this example, we’re calling unwrap
to panic in case of an error. But in a real application, we would handle it properly.
In the following Listing, we’ll get the value from the receiver in the main thread.
Filename: src/main.rs
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let val = String::from("hi");
tx.send(val).unwrap();
});
let received = rx.recv().unwrap();
println!("Got: {received}");
}
The receiver has two useful methods: recv
and try_recv
. We’re using recv
, short for receive, which will block the main thread’s execution and wait until a value is sent down the channel. Once a value is sent, recv
will return it in a Result<T, E>
. When the transmitter closes, recv
will return an error to signal that no more values will be coming.
The try_recv
method doesn’t block, but will instead return a Result<T, E>
immediately: an Ok
value holding a message if one is available and an Err
value if there aren’t any messages this time. Using try_recv
is useful if this thread has other work to do while waiting for messages: we could write a loop that calls try_recv
every so often, handles a message if one is available, and otherwise does other work for a little while until checking again.
When we run the code in the previous Listing, we’ll see the value printed from the main thread:
Got: hi
Channels and Ownership Transference
Let’s do an experiment to show how channels and ownership work together to prevent problems: we’ll try to use a val
value in the spawned thread after we’ve sent it down the channel. Try compiling the code in the following Listing to see why this code isn’t allowed.
Filename: src/main.rs
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let val = String::from("hi");
tx.send(val).unwrap();
println!("val is {val}");
});
let received = rx.recv().unwrap();
println!("Got: {received}");
}
Here, we try to print val after we’ve sent it down the channel via tx.send. Allowing this would be a bad idea: once the value has been sent to another thread, that thread could modify or drop it before we try to use the value again. Potentially, the other thread’s modifications could cause errors or unexpected results due to inconsistent or nonexistent data. However, Rust gives us an error if we try to compile the code in the previous Listing:
$ cargo run
Compiling message-passing v0.1.0 (file:///projects/message-passing)
error[E0382]: borrow of moved value: `val`
--> src/main.rs:10:26
|
8 | let val = String::from("hi");
| --- move occurs because `val` has type `String`, which does not implement the `Copy` trait
9 | tx.send(val).unwrap();
| --- value moved here
10 | println!("val is {val}");
| ^^^^^ value borrowed here after move
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0382`.
error: could not compile `message-passing` (bin "message-passing") due to 1 previous error
Our concurrency mistake has caused a compile time error. The send
function takes ownership of its parameter, and when the value is moved, the receiver takes ownership of it. This stops us from accidentally using the value again after sending it; the ownership system checks that everything is okay.
Sending Multiple Values and Seeing the Receiver Waiting
The spawned thread will now send multiple messages and pause for a second between each message.
Filename: src/main.rs
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("thread"),
];
for val in vals {
tx.send(val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
for received in rx {
println!("Got: {received}");
}
}
When running the code in Listing, you should see the following output with a one-second pause in between each line:
Got: hi
Got: from
Got: the
Got: thread
Because we don’t have any code that pauses or delays in the for
loop in the main thread, we can tell that the main thread is waiting to receive values from the spawned thread.
Creating Multiple Producers by Cloning the Transmitter
Let’s put mpsc
to use and expand the code in the previous Listing to create multiple threads that all send values to the same receiver. We can do so by cloning the transmitter, as shown in the following Listing.
Filename: src/main.rs
let (tx, rx) = mpsc::channel();
let tx1 = tx.clone();
thread::spawn(move || {
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("thread"),
];
for val in vals {
tx1.send(val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
thread::spawn(move || {
let vals = vec![
String::from("more"),
String::from("messages"),
String::from("for"),
String::from("you"),
];
for val in vals {
tx.send(val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
for received in rx {
println!("Got: {received}");
}
This time, before we create the first spawned thread, we call clone
on the transmitter. This will give us a new transmitter we can pass to the first spawned thread. We pass the original transmitter to a second spawned thread. This gives us two threads, each sending different messages to the one receiver.
When you run the code, your output should look something like this:
Got: hi
Got: more
Got: from
Got: messages
Got: for
Got: the
Got: thread
Got: you
You might see the values in another order, depending on your system. This is what makes concurrency interesting as well as difficult. If you experiment with thread::sleep
, giving it various values in the different threads, each run will be more nondeterministic and create different output each time.
Shared-State Concurrency
Another method would be for multiple threads to access the same shared data.
In a way, channels in any programming language are similar to single ownership, because once you transfer a value down a channel, you should no longer use that value. Shared-memory concurrency is like multiple ownership: multiple threads can access the same memory location at the same time.
Using Mutexes to Allow Access to Data from One Thread at a Time
Mutex is an abbreviation for mutual exclusion, as in a mutex allows only one thread to access some data at any given time. To access the data in a mutex, a thread must first signal that it wants access by asking to acquire the mutex’s lock. The lock is a data structure that is part of the mutex that keeps track of who currently has exclusive access to the data. Therefore, the mutex is described as guarding the data it holds via the locking system.
Mutexes have a reputation for being difficult to use because you have to remember two rules:
- You must attempt to acquire the lock before using the data.
- When you’re done with the data that the mutex guards, you must unlock the data so other threads can acquire the lock.
The API of Mutex<T>
As an example of how to use a mutex, let’s start by using a mutex in a single-threaded context, as shown in the following Listing.
Filename: src/main.rs
use std::sync::Mutex;
fn main() {
let m = Mutex::new(5);
{
let mut num = m.lock().unwrap();
*num = 6;
}
println!("m = {m:?}");
}
As with many types, we create a Mutex<T>
using the associated function new
. To access the data inside the mutex, we use the lock
method to acquire the lock. This call will block the current thread so it can’t do any work until it’s our turn to have the lock.
The call to lock
would fail if another thread holding the lock panicked. In that case, no one would ever be able to get the lock, so we’ve chosen to unwrap
and have this thread panic if we’re in that situation.
After we’ve acquired the lock, we can treat the return value, named num
in this case, as a mutable reference to the data inside.
As you might suspect, Mutex<T>
is a smart pointer. More accurately, the call to lock
returns a smart pointer called MutexGuard
, wrapped in a LockResult
that we handled with the call to unwrap
. The MutexGuard
smart pointer implements Deref
to point at our inner data; the smart pointer also has a Drop
implementation that releases the lock automatically when a MutexGuard
goes out of scope, which happens at the end of the inner scope. As a result, we don’t risk forgetting to release the lock and blocking the mutex from being used by other threads, because the lock release happens automatically.
Sharing a Mutex<T> Between Multiple Threads
Now let’s try to share a value between multiple threads using Mutex<T>
. We’ll spin up 10 threads and have them each increment a counter value by 1, so the counter goes from 0 to 10. The example in the following Listing will have a compiler error, and we’ll use that error to learn more about using Mutex<T>
and how Rust helps us use it correctly.
Filename: src/main.rs
use std::sync::Mutex;
use std::thread;
fn main() {
let counter = Mutex::new(0);
let mut handles = vec![];
for _ in 0..10 {
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();
*num += 1;
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("Result: {}", *counter.lock().unwrap());
}
We hinted that this example wouldn’t compile. Now let’s find out why!
$ cargo run
Compiling shared-state v0.1.0 (file:///projects/shared-state)
error[E0382]: borrow of moved value: `counter`
--> src/main.rs:21:29
|
5 | let counter = Mutex::new(0);
| ------- move occurs because `counter` has type `Mutex<i32>`, which does not implement the `Copy` trait
...
8 | for _ in 0..10 {
| -------------- inside of this loop
9 | let handle = thread::spawn(move || {
| ------- value moved into closure here, in previous iteration of loop
...
21 | println!("Result: {}", *counter.lock().unwrap());
| ^^^^^^^ value borrowed here after move
|
help: consider moving the expression out of the loop so it is only moved once
|
8 ~ let mut value = counter.lock();
9 ~ for _ in 0..10 {
10 | let handle = thread::spawn(move || {
11 ~ let mut num = value.unwrap();
|
For more information about this error, try `rustc --explain E0382`.
error: could not compile `shared-state` (bin "shared-state") due to 1 previous error
The error message states that the counter
value was moved in the previous iteration of the loop. Rust is telling us that we can’t move the ownership of lock counter
into multiple threads. Let’s fix the compiler error with the multiple-ownership method.
Multiple Ownership with Multiple Threads
We’ll wrap the Mutex<T>
in Rc<T>
in the following Listing and clone the Rc<T>
before moving ownership to the thread.
Filename: src/main.rs
use std::rc::Rc;
use std::sync::Mutex;
use std::thread;
fn main() {
let counter = Rc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let counter = Rc::clone(&counter);
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();
*num += 1;
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("Result: {}", *counter.lock().unwrap());
}
Once again, we compile and get… different errors! The compiler is teaching us a lot.
$ cargo run
Compiling shared-state v0.1.0 (file:///projects/shared-state)
error[E0277]: `Rc<Mutex<i32>>` cannot be sent between threads safely
--> src/main.rs:11:36
|
11 | let handle = thread::spawn(move || {
| ------------- ^------
| | |
| ______________________|_____________within this `{closure@src/main.rs:11:36: 11:43}`
| | |
| | required by a bound introduced by this call
12 | | let mut num = counter.lock().unwrap();
13 | |
14 | | *num += 1;
15 | | });
| |_________^ `Rc<Mutex<i32>>` cannot be sent between threads safely
|
= help: within `{closure@src/main.rs:11:36: 11:43}`, the trait `Send` is not implemented for `Rc<Mutex<i32>>`
note: required because it's used within this closure
--> src/main.rs:11:36
|
11 | let handle = thread::spawn(move || {
| ^^^^^^^
note: required by a bound in `spawn`
--> /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/thread/mod.rs:728:1
For more information about this error, try `rustc --explain E0277`.
error: could not compile `shared-state` (bin "shared-state") due to 1 previous error
Here’s the important part to focus on: `Rc<Mutex<i32>>` cannot be sent between threads safely
. The compiler is also telling us the reason why: the trait `Send` is not implemented for `Rc<Mutex<i32>>`
. We’ll talk about Send
in the next section: it’s one of the traits that ensures the types we use with threads are meant for use in concurrent situations.
Unfortunately, Rc<T>
is not safe to share across threads. When Rc<T>
manages the reference count, it adds to the count for each call to clone
and subtracts from the count when each clone is dropped. But it doesn’t use any concurrency primitives to make sure that changes to the count can’t be interrupted by another thread. This could lead to wrong counts - subtle bugs that could in turn lead to memory leaks or a value being dropped before we’re done with it. What we need is a type that is exactly like Rc<T>
but one that makes changes to the reference count in a thread-safe way.
Atomic Reference Counting with Arc<T>
Fortunately, Arc<T>
is a type like Rc<T>
that is safe to use in concurrent situations. The a stands for atomic, meaning it’s an atomically reference-counted type. At this point, you just need to know that atomics work like primitive types but are safe to share across threads.
You might then wonder why all primitive types aren’t atomic and why standard library types aren’t implemented to use Arc<T>
by default. The reason is that thread safety comes with a performance penalty that you only want to pay when you really need to. If you’re just performing operations on values within a single thread, your code can run faster if it doesn’t have to enforce the guarantees atomics provide.
The code in the following Listing will finally compile and run.
Filename: src/main.rs
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let counter = Arc::clone(&counter);
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();
*num += 1;
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("Result: {}", *counter.lock().unwrap());
}
This code will print the following:
Result: 10
Using this strategy, you can divide a calculation into independent parts, split those parts across threads, and then use a Mutex<T>
to have each thread update the final result with its part.
Note that if you are doing simple numerical operations, there are types simpler than Mutex<T>
types provided by the std::sync::atomic
module of the standard library. These types provide safe, concurrent, atomic access to primitive types. We chose to use Mutex<T>
with a primitive type for this example so we could concentrate on how Mutex<T>
works.
Similarities Between RefCell<T>/Rc<T> and Mutex<T>/Arc<T>
You might have noticed that counter
is immutable but we could get a mutable reference to the value inside it; this means Mutex<T>
provides interior mutability, as the Cell
family does. In the same way we used RefCell<T>
to allow us to mutate contents inside an Rc<T>
, we use Mutex<T>
to mutate contents inside an Arc<T>
.
Another detail to note is that Rust can’t protect you from all kinds of logic errors when you use Mutex<T>
. Using Rc<T>
came with the risk of creating reference cycles, where two Rc<T>
values refer to each other, causing memory leaks. Similarly, Mutex<T>
comes with the risk of creating deadlocks. These occur when an operation needs to lock two resources and two threads have each acquired one of the locks, causing them to wait for each other forever.
Extensible Concurrency with the Send and Sync Traits
Interestingly, almost every concurrency feature we’ve talked about so far in this chapter has been part of the standard library, not the language. However, among the key concurrency concepts that are embedded in the language rather than the standard library are the std::marker
traits Send
and Sync
.
Allowing Transference of Ownership Between Threads with Send
The Send
marker trait indicates that ownership of values of the type implementing Send
can be transferred between threads. Almost every Rust type is Send
, but there are some exceptions, including Rc<T>
: this cannot implement Send
because if you cloned an Rc<T>
value and tried to transfer ownership of the clone to another thread, both threads might update the reference count at the same time. For this reason, Rc<T>
is implemented for use in single-threaded situations where you don’t want to pay the thread-safe performance penalty.
Therefore, Rust’s type system and trait bounds ensure that you can never accidentally send an Rc<T>
value across threads unsafely. When we switched to Arc<T>
, which does implement Send
, the code compiled.
Any type composed entirely of Send
types is automatically marked as Send
as well. Almost all primitive types are Send
, aside from raw pointers.
Allowing Access from Multiple Threads with Sync
The Sync
marker trait indicates that it is safe for the type implementing Sync
to be referenced from multiple threads. In other words, any type T
implements Sync
if &T
(an immutable reference to T
) implements Send
, meaning the reference can be sent safely to another thread. Similar to Send
, primitive types all implement Sync
, and types composed entirely of types that implement Sync
also implement Sync
.
The smart pointer Rc<T>
also doesn’t implement Sync
for the same reasons that it doesn’t implement Send
. The RefCell<T>
type and the family of related Cell<T>
types don’t implement Sync
. The implementation of borrow checking that RefCell<T>
does at runtime is not thread-safe. The smart pointer Mutex<T>
implements Sync
and can be used to share access with multiple threads.
Implementing Send and Sync Manually Is Unsafe
Because types composed entirely of other types that implement the Send
and Sync
traits also automatically implement Send
and Sync
, we don’t have to implement those traits manually. As marker traits, they don’t even have any methods to implement. They’re just useful for enforcing invariants related to concurrency.
Manually implementing these traits involves implementing unsafe Rust code. For now, the important information is that building new concurrent types not made up of Send
and Sync
parts requires careful thought to uphold the safety guarantees.
Fundamentals of Asynchronous Programming: Async, Await, Futures, and Streams
Modern computers offer two techniques for working on more than one operation at a time: parallelism and concurrency. Once we start writing programs that involve parallel or concurrent operations, though, we quickly encounter new challenges inherent to asynchronous programming, where operations may not finish sequentially in the order they were started.
The video export is an example of a CPU-bound or compute-bound operation. It’s limited by the computer’s potential data processing speed within the CPU or GPU, and how much of that speed it can dedicate to the operation. The video download is an example of an IO-bound operation, because it’s limited by the speed of the computer’s input and output; it can only go as fast as the data can be sent across the network.
Note: This is how most function calls work, if you think about it. However, the term blocking is usually reserved for function calls that interact with files, the network, or other resources on the computer, because those are the cases where an individual program would benefit from the operation being non-blocking.
We could avoid blocking our main thread by spawning a dedicated thread to download each file. However, the overhead of those threads would eventually become a problem. It would be preferable if the call didn’t block in the first place.
Parallelism and Concurrency
We’ve treated parallelism and concurrency as mostly interchangeable so far. Now we need to distinguish between them more precisely, because the differences will show up as we start working.
Consider the different ways a team could split up work on a software project. You could assign a single member multiple tasks, assign each member one task, or use a mix of the two approaches.
When an individual works on several different tasks before any of them is complete, this is concurrency. Maybe you have two different projects checked out on your computer, and when you get bored or stuck on one project, you switch to the other. You’re just one person, so you can’t make progress on both tasks at the exact same time, but you can multi-task, making progress on one at a time by switching between them.
When the team splits up a group of tasks by having each member take one task and work on it alone, this is parallelism. Each person on the team can make progress at the exact same time.
In both of these workflows, you might have to coordinate between different tasks. Maybe you thought the task assigned to one person was totally independent from everyone else’s work, but it actually requires another person on the team to finish their task first. Some of the work could be done in parallel, but some of it was actually serial: it could only happen in a series, one task after the other.
On a machine with a single CPU core, the CPU can perform only one operation at a time, but it can still work concurrently. Using tools such as threads, processes, and async, the computer can pause one activity and switch to others before eventually cycling back to that first activity again. On a machine with multiple CPU cores, it can also do work in parallel. One core can be performing one task while another core performs a completely unrelated one, and those operations actually happen at the same time.
When working with async in Rust, we’re always dealing with concurrency. Depending on the hardware, the operating system, and the async runtime we are using (more on async runtimes shortly), that concurrency may also use parallelism under the hood.
Futures and the Async Syntax
The key elements of asynchronous programming in Rust are futures and Rust’s async
and await
keywords.
A future is a value that may not be ready now but will become ready at some point in the future. (This same concept shows up in many languages, sometimes under other names such as task or promise.) Rust provides a Future
trait as a building block so that different async operations can be implemented with different data structures but with a common interface. In Rust, futures are types that implement the Future
trait. Each future holds its own information about the progress that has been made and what “ready” means.
You can apply the async
keyword to blocks and functions to specify that they can be interrupted and resumed. Within an async block or async function, you can use the await
keyword to await a future (that is, wait for it to become ready). Any point where you await a future within an async block or function is a potential spot for that async block or function to pause and resume. The process of checking with a future to see if its value is available yet is called polling.
When writing async Rust, we use the async
and await
keywords most of the time. Rust compiles them into equivalent code using the Future
trait, much as it compiles for
loops into equivalent code using the Iterator
trait. Because Rust provides the Future
trait, though, you can also implement it for your own data types when you need to. Many of the functions we’ll see throughout this chapter return types with their own implementations of Future
.
Our First Async Program
To keep the focus of this chapter on learning async rather than juggling parts of the ecosystem, we’ve created the trpl
crate (trpl
is short for “The Rust Programming Language”). It re-exports all the types, traits, and functions you’ll need, primarily from the futures and tokio crates. The futures
crate is an official home for Rust experimentation for async code, and it’s actually where the Future
trait was originally designed. Tokio is the most widely used async runtime in Rust today, especially for web applications. There are other great runtimes out there, and they may be more suitable for your purposes. We use the tokio
crate under the hood for trpl
because it’s well tested and widely used.
Create a new binary project named hello-async
and add the trpl
crate as a dependency:
cargo new hello-async
cd hello-async
cargo add trpl
Now we can use the various pieces provided by trpl
to write our first async program. We’ll build a little command line tool that fetches two web pages, pulls the <title>
element from each, and prints out the title of whichever page finishes that whole process first.
Defining the page_title Function
Let’s start by writing a function that takes one page URL as a parameter, makes a request to it, and returns the text of the title element.
Filename: src/main.rs
use trpl::Html;
async fn page_title(url: &str) -> Option<String> {
let response = trpl::get(url).await;
let response_text = response.text().await;
Html::parse(&response_text)
.select_first("title")
.map(|title_element| title_element.inner_html())
}
We have to explicitly await both of these futures, because futures in Rust are lazy: they don’t do anything until you ask them to with the await
keyword. (In fact, Rust will show a compiler warning if you don’t use a future.) Iterators do nothing unless you call their next
method-whether directly or by using for
loops or methods such as map
that use next
under the hood. Likewise, futures do nothing unless you explicitly ask them to. This laziness allows Rust to avoid running async code until it’s actually needed.
Note: This is different from the behavior we saw in the previous chapter when using thread::spawn
, where the closure we passed to another thread started running immediately. It’s also different from how many other languages approach async. But it’s important for Rust to be able to provide its performance guarantees, just as it is with iterators.
Notice that Rust’s await
keyword goes after the expression you’re awaiting, not before it. That is, it’s a postfix keyword. This may differ from what you’re used to if you’ve used async
in other languages, but in Rust it makes chains of methods much nicer to work with. As a result, we can change the body of page_title
to chain the trpl::get
and text
function calls together with await
between them, as shown in the following Listing.
Filename: src/main.rs
let response_text = trpl::get(url).await.text().await;
When Rust sees a block marked with the async
keyword, it compiles it into a unique, anonymous data type that implements the Future
trait. When Rust sees a function marked with async
, it compiles it into a non-async function whose body is an async block. An async function’s return type is the type of the anonymous data type the compiler creates for that async block.
Thus, writing async fn
is equivalent to writing a function that returns a future of the return type. To the compiler, a function definition such as the async fn page_title
is equivalent to a non-async function defined like this:
use std::future::Future;
use trpl::Html;
fn page_title(url: &str) -> impl Future<Output = Option<String>> {
async move {
let text = trpl::get(url).await.text().await;
Html::parse(&text)
.select_first("title")
.map(|title| title.inner_html())
}
}
Let’s walk through each part of the transformed version:
- It uses the
impl Trait
syntax.
- The returned trait is a
Future
with an associated type of Output
. Notice that the Output
type is Option<String>
, which is the same as the original return type from the async fn
version of page_title
.
- All of the code called in the body of the original function is wrapped in an
async move
block. Remember that blocks are expressions. This whole block is the expression returned from the function.
- This async block produces a value with the type
Option<String>
, as just described. That value matches the Output
type in the return type. This is just like other blocks you have seen.
- The new function body is an async move block because of how it uses the url parameter.
Actually this piece of codes doesn’t compile. We will get an error: hidden type for `impl Future<Output = Option<String>>` captures lifetime that does not appear in bounds
.
We can solve it by two solutions:
fn page_title<'a>(url: &'a str) -> impl Future<Output = Option<String>> + 'a {
fn page_title(url: &str) -> impl Future<Output = Option<String>> + use<'_> {
Determining a Single Page’s Title
To start, we’ll just get the title for a single page. Then we pass the first URL page_title
and await the result. Because the value produced by the future is an Option<String>
, we use a match
expression to print different messages to account for whether the page had a <title>
.
Filename: src/main.rs
async fn main() {
let args: Vec<String> = std::env::args().collect();
let url = &args[1];
match page_title(url).await {
Some(title) => println!("The title for {url} was {title}"),
None => println!("{url} had no title"),
}
}
Unfortunately, this code doesn’t compile. The only place we can use the await
keyword is in async functions or blocks, and Rust won’t let us mark the special main
function as async
.
error[E0752]: `main` function is not allowed to be `async`
--> src/main.rs:6:1
|
6 | async fn main() {
| ^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`
The reason main
can’t be marked async
is that async code needs a runtime: a Rust crate that manages the details of executing asynchronous code. A program’s main
function can initialize a runtime, but it’s not a runtime itself. (We’ll see more about why this is the case in a bit.) Every Rust program that executes async code has at least one place where it sets up a runtime and executes the futures.
Most languages that support async bundle a runtime, but Rust does not. Instead, there are many different async runtimes available, each of which makes different tradeoffs suitable to the use case it targets. For example, a high-throughput web server with many CPU cores and a large amount of RAM has very different needs than a microcontroller with a single core, a small amount of RAM, and no heap allocation ability. The crates that provide those runtimes also often supply async versions of common functionality such as file or network I/O.
Here, and throughout the rest of this chapter, we’ll use the run
function from the trpl
crate, which takes a future as an argument and runs it to completion. Behind the scenes, calling run
sets up a runtime that’s used to run the future passed in. Once the future completes, run
returns whatever value the future produced.
We could pass the future returned by page_title
directly to run
, and once it completed, we could match on the resulting Option<String>
. However, for most of the examples in the chapter (and most async code in the real world), we’ll be doing more than just one async function call, so instead we’ll pass an async
block and explicitly await the result of the page_title
call, as in the following Listing.
Filename: src/main.rs
fn main() {
let args: Vec<String> = std::env::args().collect();
trpl::run(async {
let url = &args[1];
match page_title(url).await {
Some(title) => println!("The title for {url} was {title}"),
None => println!("{url} had no title"),
}
})
}
When we run this code, we get the behavior we expected initially:
$ cargo run -- https://www.rust-lang.org
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s
Running `target/debug/async_await 'https://www.rust-lang.org'`
The title for https://www.rust-lang.org was
Rust Programming Language
Each await point - that is, every place where the code uses the await
keyword - represents a place where control is handed back to the runtime. To make that work, Rust needs to keep track of the state involved in the async block so that the runtime can kick off some other work and then come back when it’s ready to try advancing the first one again. This is an invisible state machine, as if you’d written an enum like this to save the current state at each await point:
enum PageTitleFuture<'a> {
Initial {url: &'a str},
GetAwaitPoint {url: &'a str},
TextAwaitPoint {response: trpl::Response}
}
Writing the code to transition between each state by hand would be tedious and error-prone, however, especially when you need to add more functionality and more states to the code later. Fortunately, the Rust compiler creates and manages the state machine data structures for async code automatically. The normal borrowing and ownership rules around data structures all still apply, and happily, the compiler also handles checking those for us and provides useful error messages.
Ultimately, something has to execute this state machine, and that something is a runtime. (This is why you may come across references to executors when looking into runtimes: an executor is the part of a runtime responsible for executing the async code.)
Now you can see why the compiler stopped us from making main
itself an async function back. If main
were an async function, something else would need to manage the state machine for whatever future main
returned, but main
is the starting point for the program! Instead, we called the trpl::run
function in main
to set up a runtime and run the future returned by the async
block until it is done.
Note: Some runtimes provide macros so you can write an async main
function. Those macros rewrite async fn main() { ... }
to be a normal fn main
, which does the same thing we did by hand: call a function that runs a future to completion the way trpl::run
does.
Racing Our Two URLs Against Each Other
In the following Listing, we call page_title
with two different URLs passed in from the command line and race them.
Filename: src/main.rs
use trpl::{Either, Html};
fn main() {
let args: Vec<String> = std::env::args().collect();
trpl::run(async {
let title_fut_1 = page_title(&args[1]);
let title_fut_2 = page_title(&args[2]);
let (url, maybe_title) =
match trpl::race(title_fut_1, title_fut_2).await {
Either::Left(left) => left,
Either::Right(right) => right,
};
println!("{url} returned first");
match maybe_title {
Some(title) => println!("Its page title is: '{title}'"),
None => println!("Its title could not be parsed."),
}
})
}
async fn page_title(url: &str) -> (&str, Option<String>) {
let text = trpl::get(url).await.text().await;
let title = Html::parse(&text)
.select_first("title")
.map(|title| title.inner_html());
(url, title)
}
We pass the futures to trpl::race
, which returns a value to indicate which of the futures passed to it finishes first.
Note: Under the hood, race
is built on a more general function, select
, which you will encounter more often in real-world Rust code. A select
function can do a lot of things that the trpl::race
function can’t, but it also has some additional complexity that we can skip over for now.
Either future can legitimately “win,” so it doesn’t make sense to return a Result
. Instead, race
returns a type we haven’t seen before, trpl::Either
. The Either
type is somewhat similar to a Result
in that it has two cases. Unlike Result
, though, there is no notion of success or failure baked into Either
. Instead, it uses Left
and Right
to indicate “one or the other”:
enum Either<A, B> {
Left(A),
Right(B),
}
The race
function returns Left
with the output from the first future argument it finishes first, or Right
with the output of the second future argument if that one finishes first. This matches the order the arguments appear in when calling the function: the first argument is to the left of the second argument.
Applying Concurrency with Async
Creating a New Task with spawn_task
The trpl
crate supplies a spawn_task
function that looks very similar to the thread::spawn
API, and a sleep
function that is an async version of the thread::sleep
API. We can use these together to implement the counting example, as shown in the following Listing.
Filename: src/main.rs
use std::time::Duration;
fn main() {
trpl::run(async {
trpl::spawn_task(async {
for i in 1..10 {
println!("hi number {i} from the first task!");
trpl::sleep(Duration::from_millis(500)).await;
}
});
for i in 1..5 {
println!("hi number {i} from the second task!");
trpl::sleep(Duration::from_millis(500)).await;
}
});
}
Note: From this point forward in the chapter, every example will include this exact same wrapping code with trpl::run
in main
, so we’ll often skip it just as we do with main
. Don’t forget to include it in your code!
This code behaves similarly to the thread-based implementation - including the fact that you may see the messages appear in a different order in your own terminal when you run it:
hi number 1 from the second task!
hi number 1 from the first task!
hi number 2 from the first task!
hi number 2 from the second task!
hi number 3 from the first task!
hi number 3 from the second task!
hi number 4 from the first task!
hi number 4 from the second task!
hi number 5 from the first task!
This version stops as soon as the for
loop in the body of the main async block finishes, because the task spawned by spawn_task
is shut down when the main
function ends. If you want it to run all the way to the task’s completion, you will need to use a join handle to wait for the first task to complete. With threads, we used the join
method to “block” until the thread was done running. In the following Listing, we can use await
to do the same thing, because the task handle itself is a future. Its Output
type is a Result
, so we also unwrap it after awaiting it.
Filename: src/main.rs
let handle = trpl::spawn_task(async {
for i in 1..10 {
println!("hi number {i} from the first task!");
trpl::sleep(Duration::from_millis(500)).await;
}
});
for i in 1..5 {
println!("hi number {i} from the second task!");
trpl::sleep(Duration::from_millis(500)).await;
}
handle.await.unwrap();
This updated version runs until both loops finish.
hi number 1 from the second task!
hi number 1 from the first task!
hi number 2 from the first task!
hi number 2 from the second task!
hi number 3 from the first task!
hi number 3 from the second task!
hi number 4 from the first task!
hi number 4 from the second task!
hi number 5 from the first task!
hi number 6 from the first task!
hi number 7 from the first task!
hi number 8 from the first task!
hi number 9 from the first task!
So far, it looks like async and threads give us the same basic outcomes, just with different syntax: using await
instead of calling join
on the join handle, and awaiting the sleep
calls.
The bigger difference is that we didn’t need to spawn another operating system thread to do this. In fact, we don’t even need to spawn a task here. Because async blocks compile to anonymous futures, we can put each loop in an async block and have the runtime run them both to completion using the trpl::join
function.
We showed how to use the join
method on the JoinHandle
type returned when you call std::thread::spawn
. The trpl::join
function is similar, but for futures. When you give it two futures, it produces a single new future whose output is a tuple containing the output of each future you passed in once they both complete. Thus, in the following Listing, we use trpl::join
to wait for both fut1
and fut2
to finish. We do not await fut1
and fut2
but instead the new future produced by trpl::join
. We ignore the output, because it’s just a tuple containing two unit values.
Filename: src/main.rs
let fut1 = async {
for i in 1..10 {
println!("hi number {i} from the first task!");
trpl::sleep(Duration::from_millis(500)).await;
}
};
let fut2 = async {
for i in 1..5 {
println!("hi number {i} from the second task!");
trpl::sleep(Duration::from_millis(500)).await;
}
};
trpl::join(fut1, fut2).await;
When we run this, we see both futures run to completion:
hi number 1 from the first task!
hi number 1 from the second task!
hi number 2 from the first task!
hi number 2 from the second task!
hi number 3 from the first task!
hi number 3 from the second task!
hi number 4 from the first task!
hi number 4 from the second task!
hi number 5 from the first task!
hi number 6 from the first task!
hi number 7 from the first task!
hi number 8 from the first task!
hi number 9 from the first task!
Now, you’ll see the exact same order every time, which is very different from what we saw with threads. That is because the trpl::join
function is fair, meaning it checks each future equally often, alternating between them, and never lets one race ahead if the other is ready. With threads, the operating system decides which thread to check and how long to let it run. With async Rust, the runtime decides which task to check. (In practice, the details get complicated because an async runtime might use operating system threads under the hood as part of how it manages concurrency, so guaranteeing fairness can be more work for a runtime - but it’s still possible!) Runtimes don’t have to guarantee fairness for any given operation, and they often offer different APIs to let you choose whether or not you want fairness.
Try some of these variations on awaiting the futures and see what they do:
- Remove the async block from around either or both of the loops. (the first loop executes first and the second executes second)
- Await each async block immediately after defining it. (the first loop executes first and the second executes second)
- Wrap only the first loop in an async block, and await the resulting future after the body of second loop. (the second loop executes first and the first loop executes after it)
Counting Up on Two Tasks Using Message Passing
Sharing data between futures will also be familiar: we’ll use message passing again, but this time with async versions of the types and functions. In the following Listing, we’ll begin with just a single async block - not spawning a separate task as we spawned a separate thread.
Filename: src/main.rs
let (tx, mut rx) = trpl::channel();
let val = String::from("hi");
tx.send(val).unwrap();
let received = rx.recv().await.unwrap();
println!("Got: {received}");
Here, we use trpl::channel
, an async version of the multiple-producer, single-consumer channel API we used with threads back in the previous Chapter. The async version of the API is only a little different from the thread-based version: it uses a mutable rather than an immutable receiver rx
, and its recv
method produces a future we need to await rather than producing the value directly. Now we can send messages from the sender to the receiver. Notice that we don’t have to spawn a separate thread or even a task; we merely need to await the rx.recv
call.
The synchronous Receiver::recv
method in std::mpsc::channel
blocks until it receives a message. The trpl::Receiver::recv
method does not, because it is async. Instead of blocking, it hands control back to the runtime until either a message is received or the send side of the channel closes. By contrast, we don’t await the send
call, because it doesn’t block. It doesn’t need to, because the channel we’re sending it into is unbounded.
Note: Because all of this async code runs in an async block in a trpl::run
call, everything within it can avoid blocking. However, the code outside it will block on the run
function returning. That’s the whole point of the trpl::run
function: it lets you choose where to block on some set of async code, and thus where to transition between sync and async code. In most async runtimes, run
is actually named block_on
for exactly this reason.
Let’s address the first part by sending a series of messages and sleeping in between them, as shown in the follwoing Listing.
Filename: src/main.rs
let (tx, mut rx) = trpl::channel();
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("future"),
];
for val in vals {
tx.send(val).unwrap();
trpl::sleep(Duration::from_millis(500)).await;
}
while let Some(value) = rx.recv().await {
println!("received '{value}'");
}
In addition to sending the messages, we need to receive them. In this case, because we know how many messages are coming in, we could do that manually by calling rx.recv().await
four times. In the real world, though, we’ll generally be waiting on some unknown number of messages, so we need to keep waiting until we determine that there are no more messages.
We used a for
loop to process all the items received from a synchronous channel. Rust doesn’t yet have a way to write a for
loop over an asynchronous series of items, however, so we need to use a loop we haven’t seen before: the while let
conditional loop. This is the loop version of the if let
construct. The loop will continue executing as long as the pattern it specifies continues to match the value.
The rx.recv
call produces a future, which we await. The runtime will pause the future until it is ready. Once a message arrives, the future will resolve to Some(message)
as many times as a message arrives. When the channel closes, regardless of whether any messages have arrived, the future will instead resolve to None
to indicate that there are no more values and thus we should stop polling - that is, stop awaiting.
The while let
loop pulls all of this together. If the result of calling rx.recv().await
is Some(message)
, we get access to the message and we can use it in the loop body, just as we could with if let
. If the result is None
, the loop ends. Every time the loop completes, it hits the await point again, so the runtime pauses it again until another message arrives.
The code now successfully sends and receives all of the messages. Unfortunately, there are still a couple of problems. For one thing, the messages do not arrive at half-second intervals. They arrive all at once, 2 seconds (2,000 milliseconds) after we start the program. For another, this program never exits! Instead, it waits forever for new messages. You will need to shut it down using ctrl-c.
Let’s start by examining why the messages come in all at once after the full delay, rather than coming in with delays between each one. Within a given async block, the order in which await keywords appear in the code is also the order in which they’re executed when the program runs.
There’s only one async block, so everything in it runs linearly. There’s still no concurrency. All the tx.send
calls happen, interspersed with all of the trpl::sleep
calls and their associated await points. Only then does the while let
loop get to go through any of the await
points on the recv
calls.
To get the behavior we want, where the sleep delay happens between each message, we need to put the tx
and rx
operations in their own async blocks, as shown in the following Listing. Then the runtime can execute each of them separately using trpl::join
, just as in the counting example. Once again, we await the result of calling trpl::join
, not the individual futures. If we awaited the individual futures in sequence, we would just end up back in a sequential flow - exactly what we’re trying not to do.
Filename: src/main.rs
let tx_fut = async {
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("future"),
];
for val in vals {
tx.send(val).unwrap();
trpl::sleep(Duration::from_millis(500)).await;
}
};
let rx_fut = async {
while let Some(value) = rx.recv().await {
println!("received '{value}'");
}
};
trpl::join(tx_fut, rx_fut).await;
The program still never exits, though, because of the way the while let
loop interacts with trpl::join
:
- The future returned from
trpl::join
completes only once both futures passed to it have completed.
- The
tx
future completes once it finishes sleeping after sending the last message in vals
.
- The
rx
future won’t complete until the while let
loop ends.
- The
while let
loop won’t end until awaiting rx.recv
produces None
.
- Awaiting
rx.recv
will return None
only once the other end of the channel is closed.
- The channel will close only if we call
rx.close
or when the sender side, tx
, is dropped.
- We don’t call
rx.close
anywhere, and tx
won’t be dropped until the outermost async block passed to trpl::run
ends.
- The block can’t end because it is blocked on
trpl::join
completing, which takes us back to the top of this list.
We could manually close rx
by calling rx.close
somewhere, but that doesn’t make much sense. Stopping after handling some arbitrary number of messages would make the program shut down, but we could miss messages. We need some other way to make sure that tx
gets dropped before the end of the function.
Right now, the async block where we send the messages only borrows tx
because sending a message doesn’t require ownership, but if we could move tx
into that async block, it would be dropped once that block ends. The same basic dynamics apply to async blocks, so the move
keyword works with async blocks just as it does with closures.
In the following Listing, we change the block used to send messages from async
to async move
. When we run this version of the code, it shuts down gracefully after the last message is sent and received.
Filename: src/main.rs
let (tx, mut rx) = trpl::channel();
let tx_fut = async move {
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("future"),
];
for val in vals {
tx.send(val).unwrap();
trpl::sleep(Duration::from_millis(500)).await;
}
};
let rx_fut = async {
while let Some(value) = rx.recv().await {
println!("received '{value}'");
}
};
trpl::join(tx_fut, rx_fut).await;
This async channel is also a multiple-producer channel, so we can call clone
on tx
if we want to send messages from multiple futures, as shown in the following Listing.
Filename: src/main.rs
let (tx, mut rx) = trpl::channel();
let tx1 = tx.clone();
let tx1_fut = async move {
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("future"),
];
for val in vals {
tx1.send(val).unwrap();
trpl::sleep(Duration::from_millis(500)).await;
}
};
let rx_fut = async {
while let Some(value) = rx.recv().await {
println!("received '{value}'");
}
};
let tx_fut = async move {
let vals = vec![
String::from("more"),
String::from("messages"),
String::from("for"),
String::from("you"),
];
for val in vals {
tx.send(val).unwrap();
trpl::sleep(Duration::from_millis(1500)).await;
}
};
trpl::join3(tx1_fut, tx_fut, rx_fut).await;
Now we see all the messages from both sending futures, and because the sending futures use slightly different delays after sending, the messages are also received at those different intervals.
received 'hi'
received 'more'
received 'from'
received 'the'
received 'messages'
received 'future'
received 'for'
received 'you'
Working with Any Number of Futures
Happily, we have a macro form of join
to which we can pass an arbitrary number of arguments. It also handles awaiting the futures itself. Thus, we could rewrite the code to use join!
instead of join3
, as in the following Listing.
Filename: src/main.rs
trpl::join!(tx1_fut, tx_fut, rx_fut);
However, even this macro form only works when we know the number of futures ahead of time. In real-world Rust, though, pushing futures into a collection and then waiting on some or all the futures of them to complete is a common pattern.
To check all the futures in some collection, we’ll need to iterate over and join on all of them. The trpl::join_all
function accepts any type that implements the Iterator
trait, so it seems like just the ticket. Let’s try putting our futures in a vector and replacing join!
with join_all
as shown in the following Listing.
let futures = vec![tx1_fut, rx_fut, tx_fut];
trpl::join_all(futures).await;
Unfortunately, this code doesn’t compile. Instead, we get this error:
error[E0308]: mismatched types
--> src/main.rs:45:37
|
10 | let tx1_fut = async move {
| ---------- the expected `async` block
...
24 | let rx_fut = async {
| ----- the found `async` block
...
45 | let futures = vec![tx1_fut, rx_fut, tx_fut];
| ^^^^^^ expected `async` block, found a different `async` block
|
= note: expected `async` block `{async block@src/main.rs:10:23: 10:33}`
found `async` block `{async block@src/main.rs:24:22: 24:27}`
= note: no two async blocks, even if identical, have the same type
= help: consider pinning your async block and casting it to a trait object
This might be surprising. After all, none of the async blocks returns anything, so each one produces a Future<Output = ()>
. Remember that Future
is a trait, though, and that the compiler creates a unique enum for each async block. You can’t put two different hand-written structs in a Vec
, and the same rule applies to the different enums generated by the compiler.
To make this work, we need to use trait objects. Using trait objects lets us treat each of the anonymous futures produced by these types as the same type, because all of them implement the Future
trait.
Note: In the previous Chapter, we discussed another way to include multiple types in a Vec
: using an enum to represent each type that can appear in the vector. We can’t do that here, though. For one thing, we have no way to name the different types, because they are anonymous. For another, the reason we reached for a vector and join_all
in the first place was to be able to work with a dynamic collection of futures where we only care that they have the same output type.
We start by wrapping each future in the vec!
in a Box::new
, as shown in the following Listing.
Filename: src/main.rs
let futures =
vec![Box::new(tx1_fut), Box::new(rx_fut), Box::new(tx_fut)];
trpl::join_all(futures).await;
Unfortunately, this code still doesn’t compile. In fact, we get the same basic error we got before for both the second and third Box::new
calls, as well as new errors referring to the Unpin
trait. We’ll come back to the Unpin
errors in a moment. First, let’s fix the type errors on the Box::new
calls by explicitly annotating the type of the futures
variable (see the following Listing).
Filename: src/main.rs
let futures: Vec<Box<dyn Future<Output = ()>>> =
vec![Box::new(tx1_fut), Box::new(rx_fut), Box::new(tx_fut)];
This type declaration is a little involved, so let’s walk through it:
- The innermost type is the future itself. We note explicitly that the output of the future is the unit type
()
by writing Future<Output = ()>
.
- Then we annotate the trait with
dyn
to mark it as dynamic.
- The entire trait reference is wrapped in a
Box
.
- Finally, we state explicitly that
futures
is a Vec
containing these items.
That already made a big difference. Now when we run the compiler, we get only the errors mentioning Unpin
. Although there are three of them, their contents are very similar.
error[E0277]: `dyn Future<Output = ()>` cannot be unpinned
--> src/main.rs:49:24
|
49 | trpl::join_all(futures).await;
| -------------- ^^^^^^^ the trait `Unpin` is not implemented for `dyn Future<Output = ()>`
| |
| required by a bound introduced by this call
|
= note: consider using the `pin!` macro
consider using `Box::pin` if you need to access the pinned value outside of the current scope
= note: required for `Box<dyn Future<Output = ()>>` to implement `Future`
note: required by a bound in `join_all`
--> file:///home/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.30/src/future/join_all.rs:105:14
|
102 | pub fn join_all<I>(iter: I) -> JoinAll<I::Item>
| -------- required by a bound in this function
...
105 | I::Item: Future,
| ^^^^^^ required by this bound in `join_all`
error[E0277]: `dyn Future<Output = ()>` cannot be unpinned
--> src/main.rs:49:9
|
49 | trpl::join_all(futures).await;
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Unpin` is not implemented for `dyn Future<Output = ()>`
|
= note: consider using the `pin!` macro
consider using `Box::pin` if you need to access the pinned value outside of the current scope
= note: required for `Box<dyn Future<Output = ()>>` to implement `Future`
note: required by a bound in `futures_util::future::join_all::JoinAll`
--> file:///home/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.30/src/future/join_all.rs:29:8
|
27 | pub struct JoinAll<F>
| ------- required by a bound in this struct
28 | where
29 | F: Future,
| ^^^^^^ required by this bound in `JoinAll`
error[E0277]: `dyn Future<Output = ()>` cannot be unpinned
--> src/main.rs:49:33
|
49 | trpl::join_all(futures).await;
| ^^^^^ the trait `Unpin` is not implemented for `dyn Future<Output = ()>`
|
= note: consider using the `pin!` macro
consider using `Box::pin` if you need to access the pinned value outside of the current scope
= note: required for `Box<dyn Future<Output = ()>>` to implement `Future`
note: required by a bound in `futures_util::future::join_all::JoinAll`
--> file:///home/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.30/src/future/join_all.rs:29:8
|
27 | pub struct JoinAll<F>
| ------- required by a bound in this struct
28 | where
29 | F: Future,
| ^^^^^^ required by this bound in `JoinAll`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `async_await` (bin "async_await") due to 3 previous errors
That is a lot to digest, so let’s pull it apart. The first part of the message tell us that the first async block (src/main.rs:8:23: 20:10
) does not implement the Unpin
trait and suggests using pin!
or Box::pin
to resolve it. Later in the chapter, we’ll dig into a few more details about Pin
and Unpin
. For the moment, though, we can just follow the compiler’s advice to get unstuck. In the following Listing, we start by importing Pin
from std::pin
. Next we update the type annotation for futures
, with a Pin
wrapping each Box
. Finally, we use Box::pin
to pin the futures themselves.
Filename: src/main.rs
use std::pin::Pin;
let futures: Vec<Pin<Box<dyn Future<Output = ()>>>> =
vec![Box::pin(tx1_fut), Box::pin(rx_fut), Box::pin(tx_fut)];
If we compile and run this, we finally get the output we hoped for:
received 'hi'
received 'more'
received 'from'
received 'messages'
received 'the'
received 'for'
received 'future'
received 'you'
There’s a bit more to explore here. For one thing, using Pin<Box<T>>
adds a small amount of overhead from putting these futures on the heap with Box
- and we’re only doing that to get the types to line up. We don’t actually need the heap allocation, after all: these futures are local to this particular function. As noted before, Pin
is itself a wrapper type, so we can get the benefit of having a single type in the Vec
- the original reason we reached for Box
- without doing a heap allocation. We can use Pin
directly with each future, using the std::pin::pin
macro.
However, we must still be explicit about the type of the pinned reference; otherwise, Rust will still not know to interpret these as dynamic trait objects, which is what we need them to be in the Vec
. We therefore add pin
to our list of imports from std::pin
. Then we can pin!
each future when we define it and define futures
as a Vec
containing pinned mutable references to the dynamic future type, as in the following Listing.
Filename: src/main.rs
use std::pin::{Pin, pin};
let tx1_fut = pin!(async move {
});
let rx_fut = pin!(async {
});
let tx_fut = pin!(async move {
});
let futures: Vec<Pin<&mut dyn Future<Output = ()>>> =
vec![tx1_fut, rx_fut, tx_fut];
We got this far by ignoring the fact that we might have different Output
types. For example, in the following Listing, the anonymous future for a
implements Future<Output = u32>
, the anonymous future for b
implements Future<Output = &str>
, and the anonymous future for c
implements Future<Output = bool>
.
Filename: src/main.rs
let a = async { 1u32 };
let b = async { "Hello!" };
let c = async { true };
let (a_result, b_result, c_result) = trpl::join!(a, b, c);
println!("{a_result}, {b_result}, {c_result}");
We can use trpl::join!
to await them, because it allows us to pass in multiple future types and produces a tuple of those types. We cannot use trpl::join_all
, because it requires all of the futures passed in to have the same type. Remember, that error is what got us started on this adventure with Pin
!
This is a fundamental tradeoff: we can either deal with a dynamic number of futures with join_all
, as long as they all have the same type, or we can deal with a set number of futures with the join
functions or the join!
macro, even if they have different types. This is the same scenario we’d face when working with any other types in Rust. Futures are not special, even though we have some nice syntax for working with them, and that’s a good thing.
Racing Futures
When we “join” futures with the join
family of functions and macros, we require all of them to finish before we move on. Sometimes, though, we only need some future from a set to finish before we move on - kind of similar to racing one future against another.
In the following Listing, we once again use trpl::race
to run two futures, slow
and fast
, against each other.
Filename: src/main.rs
let slow = async {
println!("'slow' started.");
trpl::sleep(Duration::from_millis(100)).await;
println!("'slow' finished.");
};
let fast = async {
println!("'fast' started.");
trpl::sleep(Duration::from_millis(50)).await;
println!("'fast' finished.");
};
trpl::race(slow, fast).await;
Notice that if you flip the order of the arguments to race
, the order of the “started” messages changes, even though the fast
future always completes first. That’s because the implementation of this particular race
function is not fair. It always runs the futures passed in as arguments in the order in which they’re passed. Other implementations are fair and will randomly choose which future to poll first. Regardless of whether the implementation of race we’re using is fair, though, one of the futures will run up to the first await
in its body before another task can start.
Rust gives a runtime a chance to pause the task and switch to another one if the future being awaited isn’t ready. The inverse is also true: Rust only pauses async blocks and hands control back to a runtime at an await point. Everything between await points is synchronous.
That means if you do a bunch of work in an async block without an await point, that future will block any other futures from making progress. You may sometimes hear this referred to as one future starving other futures. In some cases, that may not be a big deal. However, if you are doing some kind of expensive setup or long-running work, or if you have a future that will keep doing some particular task indefinitely, you’ll need to think about when and where to hand control back to the runtime.
By the same token, if you have long-running blocking operations, async can be a useful tool for providing ways for different parts of the program to relate to each other.
Yielding Control to the Runtime
Let’s simulate a long-running operation. The following Listing introduces a slow function.
Filename: src/main.rs
fn slow(name: &str, ms: u64) {
thread::sleep(Duration::from_millis(ms));
println!("'{name}' ran for {ms}ms");
}
This code uses std::thread::sleep
instead of trpl::sleep
so that calling slow will block the current thread for some number of milliseconds. We can use slow
to stand in for real-world operations that are both long-running and blocking.
In the following Listing, we use slow
to emulate doing this kind of CPU-bound work in a pair of futures.
Filename: src/main.rs
let a = async {
println!("'a' started.");
slow("a", 30);
slow("a", 10);
slow("a", 20);
trpl::sleep(Duration::from_millis(50)).await;
println!("'a' finished.");
};
let b = async {
println!("'b' started.");
slow("b", 75);
slow("b", 10);
slow("b", 15);
slow("b", 350);
trpl::sleep(Duration::from_millis(50)).await;
println!("'b' finished.");
};
trpl::race(a, b).await;
To begin, each future only hands control back to the runtime after carrying out a bunch of slow operations. If you run this code, you will see this output:
'a' started.
'a' ran for 30ms
'a' ran for 10ms
'a' ran for 20ms
'b' started.
'b' ran for 75ms
'b' ran for 10ms
'b' ran for 15ms
'b' ran for 350ms
'a' finished.
We can already see this kind of handoff happening: if we removed the trpl::sleep
at the end of the a
future, it would complete without the b
future running at all. Let’s try using the sleep
function as a starting point for letting operations switch off making progress, as shown in the following Listing.
Filename: src/main.rs
let one_ms = Duration::from_millis(1);
let a = async {
println!("'a' started.");
slow("a", 30);
trpl::sleep(one_ms).await;
slow("a", 10);
trpl::sleep(one_ms).await;
slow("a", 20);
trpl::sleep(one_ms).await;
println!("'a' finished.");
};
let b = async {
println!("'b' started.");
slow("b", 75);
trpl::sleep(one_ms).await;
slow("b", 10);
trpl::sleep(one_ms).await;
slow("b", 15);
trpl::sleep(one_ms).await;
slow("b", 350);
trpl::sleep(one_ms).await;
println!("'b' finished.");
};
We add trpl::sleep
calls with await points between each call to slow
. Now the two futures’ work is interleaved:
'a' started.
'a' ran for 30ms
'b' started.
'b' ran for 75ms
'a' ran for 10ms
'b' ran for 10ms
'a' ran for 20ms
'b' ran for 15ms
'a' finished.
We don’t really want to sleep here, though: we want to make progress as fast as we can. We just need to hand back control to the runtime. We can do that directly, using the yield_now
function. In the following Listing, we replace all those sleep
calls with yield_now
.
Filename: src/main.rs
let a = async {
println!("'a' started.");
slow("a", 30);
trpl::yield_now().await;
slow("a", 10);
trpl::yield_now().await;
slow("a", 20);
trpl::yield_now().await;
println!("'a' finished.");
};
let b = async {
println!("'b' started.");
slow("b", 75);
trpl::yield_now().await;
slow("b", 10);
trpl::yield_now().await;
slow("b", 15);
trpl::yield_now().await;
slow("b", 350);
trpl::yield_now().await;
println!("'b' finished.");
};
This code is both clearer about the actual intent and can be significantly faster than using sleep
, because timers such as the one used by sleep
often have limits on how granular they can be. The version of sleep
we are using, for example, will always sleep for at least a millisecond, even if we pass it a Duration
of one nanosecond. Again, modern computers are fast: they can do a lot in one millisecond!
You can see this for yourself by setting up a little benchmark, such as the one in the following Listing. (This isn’t an especially rigorous way to do performance testing, but it suffices to show the difference here.)
Filename: src/main.rs
let one_ns = Duration::from_nanos(1);
let start = Instant::now();
async {
for _ in 1..1000 {
trpl::sleep(one_ns).await;
}
}
.await;
let time = Instant::now() - start;
println!(
"'sleep' version finished after {} seconds.",
time.as_secs_f32()
);
let start = Instant::now();
async {
for _ in 1..1000 {
trpl::yield_now().await;
}
}
.await;
let time = Instant::now() - start;
println!(
"'yield' version finished after {} seconds.",
time.as_secs_f32()
);
The version with yield_now
is way faster!
This means that async can be useful even for compute-bound tasks, depending on what else your program is doing, because it provides a useful tool for structuring the relationships between different parts of the program. This is a form of cooperative multitasking, where each future has the power to determine when it hands over control via await points. Each future therefore also has the responsibility to avoid blocking for too long. In some Rust-based embedded operating systems, this is the only kind of multitasking!
Building Our Own Async Abstractions
We can also compose futures together to create new patterns. For example, we can build a timeout
function with async building blocks we already have. When we’re done, the result will be another building block we could use to create still more async abstractions.
The following Listing shows how we would expect this timeout
to work with a slow future.
Filename: src/main.rs
let slow = async {
trpl::sleep(Duration::from_millis(100)).await;
"I finished!"
};
match timeout(slow, Duration::from_millis(10)).await {
Ok(message) => println!("Succeeded with '{message}'"),
Err(duration) => {
println!("Failed after {} seconds", duration.as_secs())
}
}
Let’s implement this! To begin, let’s think about the API for timeout
:
- It needs to be an async function itself so we can await it.
- Its first parameter should be a future to run. We can make it generic to allow it to work with any future.
- Its second parameter will be the maximum time to wait. If we use a
Duration
, that will make it easy to pass along to trpl::sleep
.
- It should return a
Result
. If the future completes successfully, the Result
will be Ok
with the value produced by the future. If the timeout elapses first, the Result
will be Err
with the duration that the timeout waited for.
The following Listing shows this declaration.
Filename: src/main.rs
async fn timeout<F: Future>(
future_to_try: F,
max_time: Duration,
) -> Result<F::Output, Duration> {
}
We can use trpl::sleep
to make a timer future from the duration, and use trpl::race
to run that timer with the future the caller passes in.
We also know that race
is not fair, polling arguments in the order in which they are passed. Thus, we pass future_to_try
to race
first so it gets a chance to complete even if max_time
is a very short duration. If future_to_try
finishes first, race
will return Left
with the output from future_to_try
. If timer
finishes first, race
will return Right
with the timer’s output of ()
.
In the following Listing, we match on the result of awaiting trpl::race
.
Filename: src/main.rs
use trpl::Either;
fn main() {
trpl::run(async {
let slow = async {
trpl::sleep(Duration::from_secs(5)).await;
"Finally finished"
};
match timeout(slow, Duration::from_secs(2)).await {
Ok(message) => println!("Succeeded with '{message}'"),
Err(duration) => {
println!("Failed after {} seconds", duration.as_secs())
}
}
});
}
async fn timeout<F: Future>(
future_to_try: F,
max_time: Duration,
) -> Result<F::Output, Duration> {
match trpl::race(future_to_try, trpl::sleep(max_time)).await {
Either::Left(output) => Ok(output),
Either::Right(_) => Err(max_time),
}
With that, we have a working timeout
built out of two other async helpers. If we run our code, it will print the failure mode after the timeout:
Failed after 2 seconds
Because futures compose with other futures, you can build really powerful tools using smaller async building blocks. For example, you can use this same approach to combine timeouts with retries, and in turn use those with operations such as network calls.
In practice, you’ll usually work directly with async
and await
, and secondarily with functions and macros such as join
, join_all
, race
, and so on. You’ll only need to reach for pin
now and again to use futures with those APIs.
Streams: Futures in Sequence
The async recv
method produces a sequence of items over time. This is an instance of a much more general pattern known as a stream.
A stream is like an asynchronous form of iteration. Whereas the trpl::Receiver
specifically waits to receive messages, though, the general-purpose stream API is much broader: it provides the next item the way Iterator
does, but asynchronously.
The similarity between iterators and streams in Rust means we can actually create a stream from any iterator. As with an iterator, we can work with a stream by calling its next
method and then awaiting the output, as in the following Listing.
Filename: src/main.rs
let values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let iter = values.iter().map(|n| n * 2);
let mut stream = trpl::stream_from_iter(iter);
while let Some(value) = stream.next().await {
println!("The value was: {value}");
}
Unfortunately, when we try to run the code, it doesn’t compile, but instead it reports that there’s no next
method available:
error[E0599]: no method named `next` found for struct `Iter` in the current scope
--> src/main.rs:10:40
|
10 | while let Some(value) = stream.next().await {
| ^^^^
|
= note: the full type name has been written to 'file:///projects/async-await/target/debug/deps/async_await-575db3dd3197d257.long-type-14490787947592691573.txt'
= note: consider using `--verbose` to print the full type name to the console
= help: items from traits can only be used if the trait is in scope
help: the following traits which provide `next` are implemented but not in scope; perhaps you want to import one of them
|
1 + use crate::trpl::StreamExt;
|
1 + use futures_util::stream::stream::StreamExt;
|
1 + use std::iter::Iterator;
|
1 + use std::str::pattern::Searcher;
|
help: there is a method `try_next` with a similar name
|
10 | while let Some(value) = stream.try_next().await {
| ~~~~~~~~
As this output explains, the reason for the compiler error is that we need the right trait in scope to be able to use the next
method. Given our discussion so far, you might reasonably expect that trait to be Stream
, but it’s actually StreamExt
. Short for extension, Ext
is a common pattern in the Rust community for extending one trait with another.
The Stream
trait defines a low-level interface that effectively combines the Iterator
and Future
traits. StreamExt
supplies a higher-level set of APIs on top of Stream
, including the next
method as well as other utility methods similar to those provided by the Iterator
trait. Stream
and StreamExt
are not yet part of Rust’s standard library, but most ecosystem crates use the same definition.
The fix to the compiler error is to add a use
statement for trpl::StreamExt
, as in the following Listing.
Filename: src/main.rs
use trpl::StreamExt;
fn main() {
trpl::run(async {
let values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let iter = values.iter().map(|n| n * 2);
let mut stream = trpl::stream_from_iter(iter);
while let Some(value) = stream.next().await {
println!("The value was: {value}");
}
});
}
Composing Stream
Many concepts are naturally represented as streams: items becoming available in a queue, chunks of data being pulled incrementally from the filesystem when the full data set is too large for the computer’s memory, or data arriving over the network over time. Because streams are futures, we can use them with any other kind of future and combine them in interesting ways. For example, we can batch up events to avoid triggering too many network calls, set timeouts on sequences of long-running operations, or throttle user interface events to avoid doing needless work.
Let’s start by building a little stream of messages as a stand-in for a stream of data we might see from a WebSocket or another real-time communication protocol, as shown in the following Listing.
Filename: src/main.rs
use trpl::{ReceiverStream, Stream, StreamExt};
fn main() {
trpl::run(async {
let mut messages = get_messages();
while let Some(message) = messages.next().await {
println!("{message}");
}
});
}
fn get_messages() -> impl Stream<Item = String> {
let (tx, rx) = trpl::channel();
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
for message in messages {
tx.send(format!("Message: '{message}'")).unwrap();
}
ReceiverStream::new(rx)
}
When we run this code, we get exactly the results we would expect:
Message: 'a'
Message: 'b'
Message: 'c'
Message: 'd'
Message: 'e'
Message: 'f'
Message: 'g'
Message: 'h'
Message: 'i'
Message: 'j'
Again, we could do this with the regular Receiver
API or even the regular Iterator
API, though, so let’s add a feature that requires streams: adding a timeout that applies to every item in the stream, and a delay on the items we emit, as shown in the following Listing.
Filename: src/main.rs
use std::{pin::pin, time::Duration};
use trpl::{ReceiverStream, Stream, StreamExt};
fn main() {
trpl::run(async {
let mut messages =
pin!(get_messages().timeout(Duration::from_millis(200)));
while let Some(result) = messages.next().await {
match result {
Ok(message) => println!("{message}"),
Err(reason) => eprintln!("Problem: {reason:?}"),
}
}
})
}
We start by adding a timeout to the stream with the timeout
method, which comes from the StreamExt
trait. Finally, notice that we pin the messages after applying the timeout to them, because the timeout helper produces a stream that needs to be pinned to be polled.
However, because there are no delays between messages, this timeout does not change the behavior of the program. Let’s add a variable delay to the messages we send, as shown in the following Listing.
Filename: src/main.rs
fn get_messages() -> impl Stream<Item = String> {
let (tx, rx) = trpl::channel();
trpl::spawn_task(async move {
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
for (index, message) in messages.into_iter().enumerate() {
let time_to_sleep = if index % 2 == 0 { 100 } else { 300 };
trpl::sleep(Duration::from_millis(time_to_sleep)).await;
tx.send(format!("Message: '{message}'")).unwrap();
}
});
ReceiverStream::new(rx)
}
In get_messages
, we use the enumerate
iterator method with the messages
array so that we can get the index of each item we’re sending along with the item itself.
To sleep between messages in the get_messages
function without blocking, we need to use async. However, we can’t make get_messages
itself into an async function, because then we’d return a Future<Output = Stream<Item = String>>
instead of a Stream<Item = String>>
. The caller would have to await get_messages
itself to get access to the stream. But remember: everything in a given future happens linearly; concurrency happens between futures. Awaiting get_messages
would require it to send all the messages, including the sleep delay between each message, before returning the receiver stream. As a result, the timeout would be useless. There would be no delays in the stream itself; they would all happen before the stream was even available.
Instead, we leave get_messages
as a regular function that returns a stream, and we spawn a task to handle the async sleep
calls.
Note: Calling spawn_task
in this way works because we already set up our runtime; had we not, it would cause a panic. Other implementations choose different tradeoffs: they might spawn a new runtime and avoid the panic but end up with a bit of extra overhead, or they may simply not provide a standalone way to spawn tasks without reference to a runtime. Make sure you know what tradeoff your runtime has chosen and write your code accordingly!
Now our code has a much more interesting result. Between every other pair of messages, a Problem: Elapsed(())
error.
Message: 'a'
Problem: Elapsed(())
Message: 'b'
Message: 'c'
Problem: Elapsed(())
Message: 'd'
Message: 'e'
Problem: Elapsed(())
Message: 'f'
Message: 'g'
Problem: Elapsed(())
Message: 'h'
Message: 'i'
Problem: Elapsed(())
Message: 'j'
The timeout doesn’t prevent the messages from arriving in the end. We still get all of the original messages, because our channel is unbounded: it can hold as many messages as we can fit in memory. If the message doesn’t arrive before the timeout, our stream handler will account for that, but when it polls the stream again, the message may now have arrived.
Merging Streams
First, let’s create another stream, which will emit an item every millisecond if we let it run directly. For simplicity, we can use the sleep
function to send a message on a delay and combine it with the same approach we used in get_messages
of creating a stream from a channel. The difference is that this time, we’re going to send back the count of intervals that have elapsed, so the return type will be impl Stream<Item = u32>
, and we can call the function get_intervals
(see the following Listing).
Filename: src/main.rs
fn get_intervals() -> impl Stream<Item = u32> {
let (tx, rx) = trpl::channel();
trpl::spawn_task(async move {
let mut count = 0;
loop {
trpl::sleep(Duration::from_millis(1)).await;
count += 1;
tx.send(count).unwrap();
}
});
ReceiverStream::new(rx)
}
Because this is all wrapped in the task created by spawn_task
, all of it - including the infinite loop - will get cleaned up along with the runtime.
This kind of infinite loop, which ends only when the whole runtime gets torn down, is fairly common in async Rust: many programs need to keep running indefinitely. With async, this doesn’t block anything else, as long as there is at least one await point in each iteration through the loop.
Now, back in our main function’s async block, we can attempt to merge the messages
and intervals
streams, as shown in the following Listing.
Filename: src/main.rs
let messages = get_messages().timeout(Duration::from_millis(200));
let intervals = get_intervals();
let merged = messages.merge(intervals);
At this point, neither messages
nor intervals
needs to be pinned or mutable, because both will be combined into the single merged
stream. However, this call to merge
doesn’t compile! (Neither does the next
call in the while let
loop, but we’ll come back to that.) This is because the two streams have different types. The messages
stream has the type Timeout<impl Stream<Item = String>>
, where Timeout
is the type that implements Stream
for a timeout
call. The intervals
stream has the type impl Stream<Item = u32>
. To merge these two streams, we need to transform one of them to match the other. We’ll rework the intervals stream, because messages is already in the basic format we want and has to handle timeout errors (see the following Listing).
Filename: src/main.rs
let messages = get_messages().timeout(Duration::from_millis(200));
let intervals = get_intervals()
.map(|count| format!("Interval: {count}"))
.timeout(Duration::from_secs(10));
let merged = messages.merge(intervals);
let mut stream = pin!(merged);
Finally, we need to make stream
mutable, so that the while let
loop’s next
calls can iterate through the stream, and pin it so that it’s safe to do so. If you run this, though, there will be two problems. First, it will never stop! You’ll need to stop it with ctrl-c. Second, the messages from the English alphabet will be buried in the midst of all the interval counter messages:
--snip--
Interval: 38
Interval: 39
Interval: 40
Message: 'a'
Interval: 41
Interval: 42
Interval: 43
--snip--
The following Listing shows one way to solve these last two problems.
Filename: src/main.rs
let messages = get_messages().timeout(Duration::from_millis(200));
let intervals = get_intervals()
.map(|count| format!("Interval: {count}"))
.throttle(Duration::from_millis(100))
.timeout(Duration::from_secs(10));
let merged = messages.merge(intervals).take(20);
let mut stream = pin!(merged);
Throttling is a way of limiting the rate at which a function will be called - or, in this case, how often the stream will be polled. Once every 100 milliseconds should do, because that’s roughly how often our messages arrive.
To limit the number of items we will accept from a stream, we apply the take
method to the merged
stream, because we want to limit the final output, not just one stream or the other.
Interval: 1
Message: 'a'
Interval: 2
Interval: 3
Problem: Elapsed(())
Interval: 4
Message: 'b'
Interval: 5
Message: 'c'
Interval: 6
Interval: 7
Problem: Elapsed(())
Interval: 8
Message: 'd'
Interval: 9
Message: 'e'
Interval: 10
Interval: 11
Problem: Elapsed(())
Interval: 12
There’s one last thing we need to handle: errors! With both of these channel-based streams, the send
calls could fail when the other side of the channel closes - and that’s just a matter of how the runtime executes the futures that make up the stream. Up until now, we’ve ignored this possibility by calling unwrap
, but in a well-behaved app, we should explicitly handle the error, at minimum by ending the loop so we don’t try to send any more messages. The following Listing shows a simple error strategy: print the issue and then break
from the loops.
fn get_messages() -> impl Stream<Item = String> {
let (tx, rx) = trpl::channel();
trpl::spawn_task(async move {
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
for (index, message) in messages.into_iter().enumerate() {
let time_to_sleep = if index % 2 == 0 { 100 } else { 300 };
trpl::sleep(Duration::from_millis(time_to_sleep)).await;
if let Err(send_error) = tx.send(format!("Message: '{message}'")) {
eprintln!("Cannot send message '{message}': {send_error}");
break;
}
}
});
ReceiverStream::new(rx)
}
fn get_intervals() -> impl Stream<Item = u32> {
let (tx, rx) = trpl::channel();
trpl::spawn_task(async move {
let mut count = 0;
loop {
trpl::sleep(Duration::from_millis(1)).await;
count += 1;
if let Err(send_error) = tx.send(count) {
eprintln!("Could not send interval {count}: {send_error}");
break;
};
}
});
ReceiverStream::new(rx)
}
A Closer Look at the Traits for Async
The Future Trait
Let’s start by taking a closer look at how the Future
trait works. Here’s how Rust defines it:
use std::pin::Pin;
use std::task::{Context, Poll};
pub trait Future {
type Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
}
First, Future
‘s associated type Output
says what the future resolves to. This is analogous to the Item
associated type for the Iterator
trait. Second, Future
also has the poll
method, which takes a special Pin
reference for its self
parameter and a mutable reference to a Context
type, and returns a Poll<Self::Output>
. We’ll talk more about Pin
and Context
in a moment. For now, let’s focus on what the method returns, the Poll
type:
enum Poll<T> {
Ready(T),
Pending,
}
This Poll
type is similar to an Option
. It has one variant that has a value, Ready(T)
, and one which does not, Pending
. Poll
means something quite different from Option
, though! The Pending
variant indicates that the future still has work to do, so the caller will need to check again later. The Ready
variant indicates that the future has finished its work and the T
value is available.
Note: With most futures, the caller should not call poll
again after the future has returned Ready
. Many futures will panic if polled again after becoming ready. Futures that are safe to poll again will say so explicitly in their documentation. This is similar to how Iterator::next
behaves.
When you see code that uses await
, Rust compiles it under the hood to code that calls poll
. If you look back at the previous Listing, where we printed out the page title for a single URL once it resolved, Rust compiles it into something kind of (although not exactly) like this:
match page_title(url).poll() {
Ready(page_title) => match page_title {
Some(title) => println!("The title for {url} was {title}"),
None => println!("{url} had no title"),
}
Pending => {
}
}
What should we do when the future is still Pending
? We need some way to try again, and again, and again, until the future is finally ready. In other words, we need a loop:
let mut page_title_fut = page_title(url);
loop {
match page_title_fut.poll() {
Ready(value) => match page_title {
Some(title) => println!("The title for {url} was {title}"),
None => println!("{url} had no title"),
}
Pending => {
}
}
}
If Rust compiled it to exactly that code, though, every await
would be blocking - exactly the opposite of what we were going for! Instead, Rust makes sure that the loop can hand off control to something that can pause work on this future to work on other futures and then check this one again later. As we’ve seen, that something is an async runtime, and this scheduling and coordination work is one of its main jobs.
Earlier in the chapter, we described waiting on rx.recv
. The recv
call returns a future, and awaiting the future polls it. We noted that a runtime will pause the future until it’s ready with either Some(message)
or None
when the channel closes. With our deeper understanding of the Future
trait, and specifically Future::poll
, we can see how that works. The runtime knows the future isn’t ready when it returns Poll::Pending
. Conversely, the runtime knows the future is ready and advances it when poll
returns Poll::Ready(Some(message))
or Poll::Ready(None)
.
The basic mechanics of futures: a runtime polls each future it is responsible for, putting the future back to sleep when it is not yet ready.
The Pin and Unpin Traits
When we introduced the idea of pinning in the previous Listing, we ran into a very gnarly error message. Here is the relevant part of it again:
error[E0277]: `{async block@src/main.rs:10:23: 10:33}` cannot be unpinned
--> src/main.rs:48:33
|
48 | trpl::join_all(futures).await;
| ^^^^^ the trait `Unpin` is not implemented for `{async block@src/main.rs:10:23: 10:33}`
|
= note: consider using the `pin!` macro
consider using `Box::pin` if you need to access the pinned value outside of the current scope
= note: required for `Box<{async block@src/main.rs:10:23: 10:33}>` to implement `Future`
note: required by a bound in `futures_util::future::join_all::JoinAll`
--> file:///home/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.30/src/future/join_all.rs:29:8
|
27 | pub struct JoinAll<F>
| ------- required by a bound in this struct
28 | where
29 | F: Future,
| ^^^^^^ required by this bound in `JoinAll`
This error message tells us not only that we need to pin the values but also why pinning is required. The trpl::join_all
function returns a struct called JoinAll
. That struct is generic over a type F
, which is constrained to implement the Future
trait. Directly awaiting a future with await
pins the future implicitly. That’s why we don’t need to use pin!
everywhere we want to await futures.
However, we’re not directly awaiting a future here. Instead, we construct a new future, JoinAll
, by passing a collection of futures to the join_all
function. The signature for join_all
requires that the types of the items in the collection all implement the Future
trait, and Box<T>
implements Future
only if the T
it wraps is a future that implements the Unpin
trait.
That’s a lot to absorb! To really understand it, let’s dive a little further into how the Future
trait actually works, in particular around pinning.
Look again at the definition of the Future
trait:
use std::pin::Pin;
use std::task::{Context, Poll};
pub trait Future {
type Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
}
The cx
parameter and its Context
type are the key to how a runtime actually knows when to check any given future while still being lazy. Again, the details of how that works are beyond the scope of this chapter, and you generally only need to think about this when writing a custom Future
implementation. We’ll focus instead on the type for self
, as this is the first time we’ve seen a method where self
has a type annotation. A type annotation for self
works like type annotations for other function parameters, but with two key differences:
It tells Rust what type self
must be for the method to be called.
It can’t be just any type. It’s restricted to the type on which the method is implemented, a reference or smart pointer to that type, or a Pin
wrapping a reference to that type.
For now, it’s enough to know that if we want to poll a future to check whether it is Pending
or Ready(Output)
, we need a Pin
-wrapped mutable reference to the type.
Pin
is a wrapper for pointer-like types such as &
, &mut
, Box
, and Rc
. (Technically, Pin
works with types that implement the Deref
or DerefMut
traits, but this is effectively equivalent to working only with pointers.) Pin
is not a pointer itself and doesn’t have any behavior of its own like Rc
and Arc
do with reference counting; it’s purely a tool the compiler can use to enforce constraints on pointer usage.
Remember from earlier in this chapter a series of await points in a future get compiled into a state machine, and the compiler makes sure that state machine follows all of Rust’s normal rules around safety, including borrowing and ownership. To make that work, Rust looks at what data is needed between one await point and either the next await point or the end of the async block. It then creates a corresponding variant in the compiled state machine. Each variant gets the access it needs to the data that will be used in that section of the source code, whether by taking ownership of that data or by getting a mutable or immutable reference to it.
So far, so good: if we get anything wrong about the ownership or references in a given async block, the borrow checker will tell us. When we want to move around the future that corresponds to that block - like moving it into a Vec
to pass to join_all
- things get trickier.
When we move a future - whether by pushing it into a data structure to use as an iterator with join_all
or by returning it from a function - that actually means moving the state machine Rust creates for us. And unlike most other types in Rust, the futures Rust creates for async blocks can end up with references to themselves in the fields of any given variant.
By default, though, any object that has a reference to itself is unsafe to move, because references always point to the actual memory address of whatever they refer to. If you move the data structure itself, those internal references will be left pointing to the old location. However, that memory location is now invalid. For one thing, its value will not be updated when you make changes to the data structure. For another - more important - thing, the computer is now free to reuse that memory for other purposes! You could end up reading completely unrelated data later.
Theoretically, the Rust compiler could try to update every reference to an object whenever it gets moved, but that could add a lot of performance overhead, especially if a whole web of references needs updating. If we could instead make sure the data structure in question doesn’t move in memory, we wouldn’t have to update any references. This is exactly what Rust’s borrow checker requires: in safe code, it prevents you from moving any item with an active reference to it.
Pin
builds on that to give us the exact guarantee we need. When we pin a value by wrapping a pointer to that value in Pin
, it can no longer move. Thus, if you have Pin<Box<SomeType>>
, you actually pin the SomeType
value, not the Box
pointer.
In fact, the Box
pointer can still move around freely. Remember: we care about making sure the data ultimately being referenced stays in place. If a pointer moves around, but the data it points to is in the same place, there’s no potential problem. As an independent exercise, look at the docs for the types as well as the std::pin
module and try to work out how you’d do this with a Pin
wrapping a Box
. The key is that the self-referential type itself cannot move, because it is still pinned.
However, most types are perfectly safe to move around, even if they happen to be behind a Pin
wrapper. We only need to think about pinning when items have internal references. Primitive values such as numbers and Booleans are safe because they obviously don’t have any internal references. Neither do most types you normally work with in Rust. You can move around a Vec
, for example, without worrying. Given only what we have seen so far, if you have a Pin<Vec<String>>
, you’d have to do everything via the safe but restrictive APIs provided by Pin
, even though a Vec<String>
is always safe to move if there are no other references to it. We need a way to tell the compiler that it’s fine to move items around in cases like this - and that’s where Unpin
comes into play.
Unpin
is a marker trait, similar to the Send
and Sync
traits, and thus has no functionality of its own. Marker traits exist only to tell the compiler it’s safe to use the type implementing a given trait in a particular context. Unpin
informs the compiler that a given type does not need to uphold any guarantees about whether the value in question can be safely moved.
Just as with Send
and Sync
, the compiler implements Unpin
automatically for all types where it can prove it is safe. A special case, again similar to Send
and Sync
, is where Unpin
is not implemented for a type. The notation for this is impl !Unpin for SomeType
, where SomeType
is the name of a type that does need to uphold those guarantees to be safe whenever a pointer to that type is used in a Pin
.
In other words, there are two things to keep in mind about the relationship between Pin
and Unpin
. First, Unpin
is the “normal” case, and !Unpin
is the special case. Second, whether a type implements Unpin
or !Unpin
only matters when you’re using a pinned pointer to that type like Pin<&mut SomeType>
.
To make that concrete, think about a String
: it has a length and the Unicode characters that make it up. We can wrap a String
in Pin
. However, String
automatically implements Unpin
, as do most other types in Rust.
As a result, we can do things that would be illegal if String
implemented !Unpin
instead, such as replacing one string with another at the exact same location in memory. This doesn’t violate the Pin
contract, because String
has no internal references that make it unsafe to move around! That is precisely why it implements Unpin
rather than !Unpin
.
Now we know enough to understand the errors reported for that join_all
call from back. We originally tried to move the futures produced by async blocks into a Vec<Box<dyn Future<Output = ()>>>
, but as we’ve seen, those futures may have internal references, so they don’t implement Unpin
. They need to be pinned, and then we can pass the Pin
type into the Vec
, confident that the underlying data in the futures will not be moved.
Pin
and Unpin
are mostly important for building lower-level libraries, or when you’re building a runtime itself, rather than for day-to-day Rust code. When you see these traits in error messages, though, now you’ll have a better idea of how to fix your code!
The Stream Trait
As you learned earlier in the chapter, streams are similar to asynchronous iterators.
From Iterator
, we have the idea of a sequence: its next
method provides an Option<Self::Item>
. From Future
, we have the idea of readiness over time: its poll
method provides a Poll<Self::Output>
. To represent a sequence of items that become ready over time, we define a Stream
trait that puts those features together:
use std::pin::Pin;
use std::task::{Context, Poll};
trait Stream {
type Item;
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Option<Self::Item>>;
}
The Stream
trait defines an associated type called Item
for the type of the items produced by the stream. This is similar to Iterator
, where there may be zero to many items, and unlike Future
, where there is always a single Output
, even if it’s the unit type ()
.
Stream
also defines a method to get those items. We call it poll_next
, to make it clear that it polls in the same way Future::poll
does and produces a sequence of items in the same way Iterator::next
does. Its return type combines Poll
with Option
. The outer type is Poll
, because it has to be checked for readiness, just as a future does. The inner type is Option
, because it needs to signal whether there are more messages, just as an iterator does.
In the example we saw in the section on streaming, though, we didn’t use poll_next
or Stream
, but instead used next
and StreamExt
. We could work directly in terms of the poll_next
API by hand-writing our own Stream
state machines, of course, just as we could work with futures directly via their poll
method. Using await
is much nicer, though, and the StreamExt
trait supplies the next
method so we can do just that:
trait StreamExt: Stream {
async fn next(&mut self) -> Option<Self::Item>
where
Self: Unpin;
}
Note: The actual definition we used earlier in the chapter looks slightly different than this, because it supports versions of Rust that did not yet support using async functions in traits. As a result, it looks like this:
fn next(&mut self) -> Next<'_, Self> where Self: Unpin;
That Next
type is a struct
that implements Future
and allows us to name the lifetime of the reference to self
with Next<'_, Self>
, so that await
can work with this method.
The StreamExt
trait is also the home of all the interesting methods available to use with streams. StreamExt
is automatically implemented for every type that implements Stream
, but these traits are defined separately to enable the community to iterate on convenience APIs without affecting the foundational trait.
In the version of StreamExt
used in the trpl
crate, the trait not only defines the next
method but also supplies a default implementation of next
that correctly handles the details of calling Stream::poll_next
. This means that even when you need to write your own streaming data type, you only have to implement Stream
, and then anyone who uses your data type can use StreamExt
and its methods with it automatically.
Putting It All Together: Futures, Tasks, and Threads
Many operating systems have supplied threading-based concurrency models for decades now, and many programming languages support them as a result. However, these models are not without their tradeoffs. On many operating systems, they use a fair bit of memory for each thread, and they come with some overhead for starting up and shutting down. Threads are also only an option when your operating system and hardware support them. Unlike mainstream desktop and mobile computers, some embedded systems don’t have an OS at all, so they also don’t have threads.
The async model provides a different - and ultimately complementary - set of tradeoffs. In the async model, concurrent operations don’t require their own threads. Instead, they can run on tasks, as when we used trpl::spawn_task
to kick off work from a synchronous function in the streams section. A task is similar to a thread, but instead of being managed by the operating system, it’s managed by library-level code: the runtime.
In the previous section, we saw that we could build a stream by using an async channel and spawning an async task we could call from synchronous code. We can do the exact same thing with a thread. In the previous Listing, we used trpl::spawn_task
and trpl::sleep
. In the following Listing, we replace those with the thread::spawn
and thread::sleep
APIs from the standard library in the get_intervals
function.
Filename: src/main.rs
fn get_intervals() -> impl Stream<Item = u32> {
let (tx, rx) = trpl::channel();
thread::spawn(move || {
let mut count = 0;
loop {
thread::sleep(Duration::from_millis(1));
count += 1;
if let Err(send_error) = tx.send(count) {
eprintln!("Could not send interval {count}: {send_error}");
break;
};
}
});
ReceiverStream::new(rx)
}
If you run this code, the output is identical to that of the previous Listing. And notice how little changes here from the perspective of the calling code. What’s more, even though one of our functions spawned an async task on the runtime and the other spawned an OS thread, the resulting streams were unaffected by the differences.
Despite their similarities, these two approaches behave very differently, although we might have a hard time measuring it in this very simple example. We could spawn millions of async tasks on any modern personal computer. If we tried to do that with threads, we would literally run out of memory!
However, there’s a reason these APIs are so similar. Threads act as a boundary for sets of synchronous operations; concurrency is possible between threads. Tasks act as a boundary for sets of asynchronous operations; concurrency is possible both between and within tasks, because a task can switch between futures in its body. Finally, futures are Rust’s most granular unit of concurrency, and each future may represent a tree of other futures. The runtime - specifically, its executor - manages tasks, and tasks manage futures. In that regard, tasks are similar to lightweight, runtime-managed threads with added capabilities that come from being managed by a runtime instead of by the operating system.
This doesn’t mean that async tasks are always better than threads (or vice versa). Concurrency with threads is in some ways a simpler programming model than concurrency with async
. That can be a strength or a weakness. Threads are somewhat “fire and forget”; they have no native equivalent to a future, so they simply run to completion without being interrupted except by the operating system itself. That is, they have no built-in support for intratask concurrency the way futures do. Threads in Rust also have no mechanisms for cancellation - a subject we haven’t covered explicitly in this chapter but was implied by the fact that whenever we ended a future, its state got cleaned up correctly.
These limitations also make threads harder to compose than futures. It’s much more difficult, for example, to use threads to build helpers such as the timeout
and throttle
methods we built earlier in this chapter. The fact that futures are richer data structures means they can be composed together more naturally, as we have seen.
Tasks, then, give us additional control over futures, allowing us to choose where and how to group them. And it turns out that threads and tasks often work very well together, because tasks can (at least in some runtimes) be moved around between threads. In fact, under the hood, the runtime we’ve been using - including the spawn_blocking
and spawn_task
functions - is multithreaded by default! Many runtimes use an approach called work stealing to transparently move tasks around between threads, based on how the threads are currently being utilized, to improve the system’s overall performance. That approach actually requires threads and tasks, and therefore futures.
When thinking about which method to use when, consider these rules of thumb:
- If the work is very parallelizable, such as processing a bunch of data where each part can be processed separately, threads are a better choice.
- If the work is very concurrent, such as handling messages from a bunch of different sources that may come in at different intervals or different rates, async is a better choice.
And if you need both parallelism and concurrency, you don’t have to choose between threads and async. You can use them together freely, letting each one play the part it’s best at. For example, The following Listing shows a fairly common example of this kind of mix in real-world Rust code.
Filename: src/main.rs
use std::{thread, time::Duration};
fn main() {
let (tx, mut rx) = trpl::channel();
thread::spawn(move || {
for i in 1..11 {
tx.send(i).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
trpl::run(async {
while let Some(message) = rx.recv().await {
println!("{message}");
}
});
}
Object-Oriented Programming Features
Object-oriented programming (OOP) is a way of modeling programs. Many competing definitions describe what OOP is, and by some of these definitions Rust is object oriented but by others it is not.
Characteristics of Object-Oriented Languages
Objects Contain Data and Behavior
Object-oriented programs are made up of objects. An object packages both data and the procedures that operate on that data. The procedures are typically called methods or operations.
Encapsulation That Hides Implementation Details
Another aspect commonly associated with OOP is the idea of encapsulation, which means that the implementation details of an object aren’t accessible to code using that object. Therefore, the only way to interact with an object is through its public API; code using the object shouldn’t be able to reach into the object’s internals and change data or behavior directly. This enables the programmer to change and refactor an object’s internals without needing to change the code that uses the object.
We can use the pub
keyword to decide which modules, types, functions, and methods in our code should be public, and by default everything else is private. For example, we can define a struct AveragedCollection
that has a field containing a vector of i32
values. The struct can also have a field that contains the average of the values in the vector, meaning the average doesn’t have to be computed on demand whenever anyone needs it. In other words, AveragedCollection
will cache the calculated average for us. The following Listing has the definition of the AveragedCollection
struct:
Filename: src/lib.rs
pub struct AveragedCollection {
list: Vec<i32>,
average: f64,
}
The struct is marked pub
so that other code can use it, but the fields within the struct remain private. This is important in this case because we want to ensure that whenever a value is added or removed from the list, the average is also updated. We do this by implementing add
, remove
, and average
methods on the struct, as shown in the following Listing:
Filename: src/lib.rs
impl AveragedCollection {
pub fn add(&mut self, value: i32) {
self.list.push(value);
self.update_average();
}
pub fn remove(&mut self) -> Option<i32> {
let result = self.list.pop();
match result {
Some(value) => {
self.update_average();
Some(value)
}
None => None,
}
}
pub fn average(&self) -> f64 {
self.average
}
fn update_average(&mut self) {
let total: i32 = self.list.iter().sum();
self.average = total as f64 / self.list.len() as f64;
}
}
We leave the list
and average
fields private so there is no way for external code to add or remove items to or from the list
field directly; otherwise, the average
field might become out of sync when the list
changes. The average
method returns the value in the average
field, allowing external code to read the average
but not modify it.
Because we’ve encapsulated the implementation details of the struct AveragedCollection
, we can easily change aspects, such as the data structure, in the future. For instance, we could use a HashSet<i32>
instead of a Vec<i32>
for the list
field. As long as the signatures of the add
, remove
, and average
public methods stayed the same, code using AveragedCollection
wouldn’t need to change. If we made list
public instead, this wouldn’t necessarily be the case: HashSet<i32>
and Vec<i32>
have different methods for adding and removing items, so the external code would likely have to change if it were modifying list
directly.
If encapsulation is a required aspect for a language to be considered object oriented, then Rust meets that requirement. The option to use pub
or not for different parts of code enables encapsulation of implementation details.
Inheritance as a Type System and as Code Sharing
Inheritance is a mechanism whereby an object can inherit elements from another object’s definition, thus gaining the parent object’s data and behavior without you having to define them again.
If a language must have inheritance to be object oriented, then Rust is not such a language. There is no way to define a struct that inherits the parent struct’s fields and method implementations without using a macro.
You would choose inheritance for two main reasons. One is for reuse of code: you can implement particular behavior for one type, and inheritance enables you to reuse that implementation for a different type. You can do this in a limited way in Rust code using default trait method implementations, which you saw in the previous Listing when we added a default implementation of the summarize
method on the Summary
trait. Any type implementing the Summary
trait would have the summarize
method available on it without any further code. This is similar to a parent class having an implementation of a method and an inheriting child class also having the implementation of the method. We can also override the default implementation of the summarize
method when we implement the Summary
trait, which is similar to a child class overriding the implementation of a method inherited from a parent class.
The other reason to use inheritance relates to the type system: to enable a child type to be used in the same places as the parent type. This is also called polymorphism, which means that you can substitute multiple objects for each other at runtime if they share certain characteristics.
Polymorphism
To many people, polymorphism is synonymous with inheritance. But it’s actually a more general concept that refers to code that can work with data of multiple types. For inheritance, those types are generally subclasses.
Rust instead uses generics to abstract over different possible types and trait bounds to impose constraints on what those types must provide. This is sometimes called bounded parametric polymorphism.
Inheritance has recently fallen out of favor as a programming design solution in many programming languages because it’s often at risk of sharing more code than necessary. Subclasses shouldn’t always share all characteristics of their parent class but will do so with inheritance. This can make a program’s design less flexible. It also introduces the possibility of calling methods on subclasses that don’t make sense or that cause errors because the methods don’t apply to the subclass. In addition, some languages will only allow single inheritance (meaning a subclass can only inherit from one class), further restricting the flexibility of a program’s design.
For these reasons, Rust takes the different approach of using trait objects instead of inheritance.
Using Trait Objects That Allow for Values of Different Types
However, sometimes we want our library user to be able to extend the set of types that are valid in a particular situation.
To show how we might achieve this, we’ll create an example graphical user interface (GUI) tool that iterates through a list of items, calling a draw
method on each one to draw it to the screen - a common technique for GUI tools. We’ll create a library crate called gui
that contains the structure of a GUI library. This crate might include some types for people to use, such as Button
or TextField
. In addition, gui
users will want to create their own types that can be drawn: for instance, one programmer might add an Image
and another might add a SelectBox
.
At the time of writing the library, we can’t know and define all the types other programmers might want to create. But we do know that gui
needs to keep track of many values of different types, and it needs to call a draw
method on each of these differently typed values. It doesn’t need to know exactly what will happen when we call the draw
method, just that the value will have that method available for us to call.
To do this in a language with inheritance, we might define a class named Component
that has a method named draw
on it. The other classes, such as Button
, Image
, and SelectBox
, would inherit from Component
and thus inherit the draw
method. They could each override the draw
method to define their custom behavior, but the framework could treat all of the types as if they were Component
instances and call draw
on them. But because Rust doesn’t have inheritance, we need another way to structure the gui
library to allow users to extend it with new types.
Defining a Trait for Common Behavior
To implement the behavior we want gui
to have, we’ll define a trait named Draw
that will have one method named draw
. Then we can define a vector that takes a trait object. A trait object points to both an instance of a type implementing our specified trait and a table used to look up trait methods on that type at runtime. We create a trait object by specifying some sort of pointer, such as an &
reference or a Box<T>
smart pointer, then the dyn
keyword, and then specifying the relevant trait. We can use trait objects in place of a generic or concrete type. Wherever we use a trait object, Rust’s type system will ensure at compile time that any value used in that context will implement the trait object’s trait. Consequently, we don’t need to know all the possible types at compile time.
We’ve mentioned that, in Rust, we refrain from calling structs and enums “objects” to distinguish them from other languages’ objects. In a struct or enum, the data in the struct fields and the behavior in impl
blocks are separated, whereas in other languages, the data and behavior combined into one concept is often labeled an object. However, trait objects are more like objects in other languages in the sense that they combine data and behavior. But trait objects differ from traditional objects in that we can’t add data to a trait object. Trait objects aren’t as generally useful as objects in other languages: their specific purpose is to allow abstraction across common behavior.
The following Listing shows how to define a trait named Draw
with one method named draw
.
Filename: src/lib.rs
pub trait Draw {
fn draw(&self);
}
This syntax should look familiar from our discussions on how to define traits in the previous Chapter. Next comes some new syntax: the following Listing defines a struct named Screen
that holds a vector named components
. This vector is of type Box<dyn Draw>
, which is a trait object; it’s a stand-in for any type inside a Box
that implements the Draw
trait.
Filename: src/lib.rs
pub struct Screen {
pub components: Vec<Box<dyn Draw>>,
}
On the Screen
struct, we’ll define a method named run
that will call the draw
method on each of its components
, as shown in the following Listing.
Filename: src/lib.rs
impl Screen {
pub fn run(&self) {
for component in self.components.iter() {
component.draw();
}
}
}
This works differently from defining a struct that uses a generic type parameter with trait bounds. A generic type parameter can be substituted with only one concrete type at a time, whereas trait objects allow for multiple concrete types to fill in for the trait object at runtime. For example, we could have defined the Screen
struct using a generic type and a trait bound as in the following Listing:
Filename: src/lib.rs
pub struct Screen<T: Draw> {
pub components: Vec<T>,
}
impl<T> Screen<T>
where
T: Draw,
{
pub fn run(&self) {
for component in self.components.iter() {
component.draw();
}
}
}
This restricts us to a Screen
instance that has a list of components all of type Button
or all of type TextField
. If you’ll only ever have homogeneous collections, using generics and trait bounds is preferable because the definitions will be monomorphized at compile time to use the concrete types.
On the other hand, with the method using trait objects, one Screen
instance can hold a Vec<T>
that contains a Box<Button>
as well as a Box<TextField>
. Let’s look at how this works, and then we’ll talk about the runtime performance implications.
Implementing the Trait
Now we’ll add some types that implement the Draw
trait. We’ll provide the Button
type. Again, actually implementing a GUI library is beyond the scope of this book, so the draw
method won’t have any useful implementation in its body. To imagine what the implementation might look like, a Button
struct might have fields for width
, height
, and label
, as shown in the following Listing:
Filename: src/lib.rs
pub struct Button {
pub width: u32,
pub height: u32,
pub label: String,
}
impl Draw for Button {
fn draw(&self) {
}
}
The width
, height
, and label
fields on Button
will differ from the fields on other components; for example, a TextField
type might have those same fields plus a placeholder
field. Each of the types we want to draw on the screen will implement the Draw
trait but will use different code in the draw
method to define how to draw that particular type, as Button
has here (without the actual GUI code, as mentioned). The Button
type, for instance, might have an additional impl
block containing methods related to what happens when a user clicks the button. These kinds of methods won’t apply to types like TextField
.
If someone using our library decides to implement a SelectBox
struct that has width
, height
, and options
fields, they would implement the Draw
trait on the SelectBox
type as well, as shown in the following Listing.
Filename: src/main.rs
use gui::Draw;
struct SelectBox {
width: u32,
height: u32,
options: Vec<String>,
}
impl Draw for SelectBox {
fn draw(&self) {
}
}
Our library’s user can now write their main
function to create a Screen
instance. To the Screen
instance, they can add a SelectBox
and a Button
by putting each in a Box<T>
to become a trait object. They can then call the run
method on the Screen
instance, which will call draw
on each of the components. The following Listing shows this implementation:
Filename: src/main.rs
use gui::{Button, Screen};
fn main() {
let screen = Screen {
components: vec![
Box::new(SelectBox {
width: 75,
height: 10,
options: vec![
String::from("Yes"),
String::from("Maybe"),
String::from("No"),
],
}),
Box::new(Button {
width: 50,
height: 10,
label: String::from("OK"),
}),
],
};
screen.run();
}
When we wrote the library, we didn’t know that someone might add the SelectBox
type, but our Screen
implementation was able to operate on the new type and draw it because SelectBox
implements the Draw
trait, which means it implements the draw
method.
This concept - of being concerned only with the messages a value responds to rather than the value’s concrete type - is similar to the concept of duck typing in dynamically typed languages: if it walks like a duck and quacks like a duck, then it must be a duck! In the implementation of run
on Screen
in the previous Listing, run
doesn’t need to know what the concrete type of each component is. It doesn’t check whether a component is an instance of a Button
or a SelectBox
, it just calls the draw
method on the component. By specifying Box<dyn Draw>
as the type of the values in the components
vector, we’ve defined Screen
to need values that we can call the draw
method on.
The advantage of using trait objects and Rust’s type system to write code similar to code using duck typing is that we never have to check whether a value implements a particular method at runtime or worry about getting errors if a value doesn’t implement a method but we call it anyway. Rust won’t compile our code if the values don’t implement the traits that the trait objects need.
For example, the following Listing shows what happens if we try to create a Screen
with a String
as a component.
Filename: src/main.rs
use gui::Screen;
fn main() {
let screen = Screen {
components: vec![Box::new(String::from("Hi"))],
};
screen.run();
}
We’ll get this error because String
doesn’t implement the Draw
trait:
$ cargo run
Compiling gui v0.1.0 (file:///projects/gui)
error[E0277]: the trait bound `String: Draw` is not satisfied
--> src/main.rs:5:26
|
5 | components: vec![Box::new(String::from("Hi"))],
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Draw` is not implemented for `String`
|
= help: the trait `Draw` is implemented for `Button`
= note: required for the cast from `Box<String>` to `Box<dyn Draw>`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `gui` (bin "gui") due to 1 previous error
This error lets us know that either we’re passing something to Screen
that we didn’t mean to pass and so should pass a different type, or we should implement Draw
on String
so that Screen
is able to call draw
on it.
Recall in “Performance of Code Using Generics” in the previous Chapter our discussion on the monomorphization process performed on generics by the compiler: the compiler generates nongeneric implementations of functions and methods for each concrete type that we use in place of a generic type parameter. The code that results from monomorphization is doing static dispatch, which is when the compiler knows what method you’re calling at compile time. This is opposed to dynamic dispatch, which is when the compiler can’t tell at compile time which method you’re calling. In dynamic dispatch cases, the compiler emits code that at runtime will figure out which method to call.
When we use trait objects, Rust must use dynamic dispatch. The compiler doesn’t know all the types that might be used with the code that’s using trait objects, so it doesn’t know which method implemented on which type to call. Instead, at runtime, Rust uses the pointers inside the trait object to know which method to call. This lookup incurs a runtime cost that doesn’t occur with static dispatch. Dynamic dispatch also prevents the compiler from choosing to inline a method’s code, which in turn prevents some optimizations, and Rust has some rules, called dyn compatibility, about where you can and cannot use dynamic dispatch. Those rules are beyond the scope of this discussion, but you can read more about them in the reference. However, we did get extra flexibility in the code that we wrote and were able to support, so it’s a trade-off to consider.
Implementing an Object-Oriented Design Pattern
The state pattern is an object-oriented design pattern. The crux of the pattern is that we define a set of states a value can have internally. The states are represented by a set of state objects, and the value’s behavior changes based on its state. We’re going to work through an example of a blog post struct that has a field to hold its state, which will be a state object from the set “draft”, “review”, or “published”.
The state objects share functionality: in Rust, of course, we use structs and traits rather than objects and inheritance. Each state object is responsible for its own behavior and for governing when it should change into another state. The value that holds a state object knows nothing about the different behavior of the states or when to transition between states.
The advantage of using the state pattern is that, when the business requirements of the program change, we won’t need to change the code of the value holding the state or the code that uses the value. We’ll only need to update the code inside one of the state objects to change its rules or perhaps add more state objects.
First we’re going to implement the state pattern in a more traditional object-oriented way, then we’ll use an approach that’s a bit more natural in Rust. Let’s dig in to incrementally implement a blog post workflow using the state pattern.
The final functionality will look like this:
- A blog post starts as an empty draft.
- When the draft is done, a review of the post is requested.
- When the post is approved, it gets published.
- Only published blog posts return content to print, so unapproved posts can’t accidentally be published.
Any other changes attempted on a post should have no effect. For example, if we try to approve a draft blog post before we’ve requested a review, the post should remain an unpublished draft.
The following Listing shows this workflow in code form: this is an example usage of the API we’ll implement in a library crate named blog
. This won’t compile yet because we haven’t implemented the blog
crate.
Filename: src/main.rs
use blog::Post;
fn main() {
let mut post = Post::new();
post.add_text("I ate a salad for lunch today");
assert_eq!("", post.content());
post.request_review();
assert_eq!("", post.content());
post.approve();
assert_eq!("I ate a salad for lunch today", post.content());
}
If we try to get the post’s content immediately, before approval, we shouldn’t get any text because the post is still a draft. We’ve added assert_eq!
in the code for demonstration purposes. An excellent unit test for this would be to assert that a draft blog post returns an empty string from the content
method, but we’re not going to write tests for this example.
Notice that the only type we’re interacting with from the crate is the Post
type. This type will use the state pattern and will hold a value that will be one of three state objects representing the various states a post can be in - draft, review, or published. Changing from one state to another will be managed internally within the Post
type. The states change in response to the methods called by our library’s users on the Post
instance, but they don’t have to manage the state changes directly. Also, users can’t make a mistake with the states, such as publishing a post before it’s reviewed.
Defining Post and Creating a New Instance in the Draft State
Let’s get started on the implementation of the library! We know we need a public Post
struct that holds some content, so we’ll start with the definition of the struct and an associated public new
function to create an instance of Post
, as shown in the following Listing. We’ll also make a private State
trait that will define the behavior that all state objects for a Post
must have.
Then Post
will hold a trait object of Box<dyn State>
inside an Option<T>
in a private field named state
to hold the state object. You’ll see why the Option<T>
is necessary in a bit.
Filename: src/lib.rs
pub struct Post {
state: Option<Box<dyn State>>,
content: String,
}
impl Post {
pub fn new() -> Post {
Post {
state: Some(Box::new(Draft {})),
content: String::new(),
}
}
}
trait State {}
struct Draft {}
impl State for Draft {}
The State
trait defines the behavior shared by different post states. The state objects are Draft
, PendingReview
, and Published
, and they will all implement the State
trait. For now, the trait doesn’t have any methods, and we’ll start by defining just the Draft
state because that is the state we want a post to start in.
Storing the Text of the Post Content
The add_text
method is pretty straightforward, so let’s add the implementation in the following Listing to the impl Post
block.
Filename: src/lib.rs
impl Post {
pub fn add_text(&mut self, text: &str) {
self.content.push_str(text);
}
}
The add_text
method takes a mutable reference to self
because we’re changing the Post
instance that we’re calling add_text
on. This behavior doesn’t depend on the state the post is in, so it’s not part of the state pattern. The add_text
method doesn’t interact with the state
field at all, but it is part of the behavior we want to support.
Ensuring the Content of a Draft Post Is Empty
Even after we’ve called add_text
and added some content to our post, we still want the content
method to return an empty string slice because the post is still in the draft state. For now, let’s implement the content
method with the simplest thing that will fulfill this requirement: always returning an empty string slice. We’ll change this later once we implement the ability to change a post’s state so it can be published. So far, posts can only be in the draft state, so the post content should always be empty. The following Listing shows this placeholder implementation.
Filename: src/lib.rs
impl Post {
pub fn content(&self) -> &str {
""
}
}
Requesting a Review Changes the Post’s State
Next, we need to add functionality to request a review of a post, which should change its state from Draft
to PendingReview
. The following Listing shows this code.
Filename: src/lib.rs
impl Post {
pub fn request_review(&mut self) {
if let Some(s) = self.state.take() {
self.state = Some(s.request_review())
}
}
}
trait State {
fn request_review(self: Box<Self>) -> Box<dyn State>;
}
struct Draft {}
impl State for Draft {
fn request_review(self: Box<Self>) -> Box<dyn State> {
Box::new(PendingReview {})
}
}
struct PendingReview {}
impl State for PendingReview {
fn request_review(self: Box<Self>) -> Box<dyn State> {
self
}
}
We add the request_review
method to the State
trait; all types that implement the trait will now need to implement the request_review
method. Note that rather than having self
, &self
, or &mut self
as the first parameter of the method, we have self: Box<Self>
. This syntax means the method is only valid when called on a Box
holding the type. This syntax takes ownership of Box<Self>
, invalidating the old state so the state value of the Post
can transform into a new state.
To consume the old state, the request_review
method needs to take ownership of the state value. This is where the Option
in the state
field of Post
comes in: we call the take
method to take the Some
value out of the state
field and leave a None
in its place because Rust doesn’t let us have unpopulated fields in structs. This lets us move the state
value out of Post
rather than borrowing it. Then we’ll set the post’s state
value to the result of this operation.
We need to set state
to None
temporarily rather than setting it directly with code like self.state = self.state.request_review();
to get ownership of the state
value. This ensures Post
can’t use the old state
value after we’ve transformed it into a new state.
The request_review
method on Draft
returns a new, boxed instance of a new PendingReview
struct, which represents the state when a post is waiting for a review. The PendingReview
struct also implements the request_review
method but doesn’t do any transformations. Rather, it returns itself because when we request a review on a post already in the PendingReview
state, it should stay in the PendingReview
state.
Now we can start seeing the advantages of the state pattern: the request_review
method on Post
is the same no matter its state value. Each state is responsible for its own rules.
Adding approve to Change the Behavior of content
The approve
method will be similar to the request_review
method: it will set state
to the value that the current state says it should have when that state is approved, as shown in the following Listing:
Filename: src/lib.rs
impl Post {
pub fn approve(&mut self) {
if let Some(s) = self.state.take() {
self.state = Some(s.approve())
}
}
}
trait State {
fn request_review(self: Box<Self>) -> Box<dyn State>;
fn approve(self: Box<Self>) -> Box<dyn State>;
}
struct Draft {}
impl State for Draft {
fn approve(self: Box<Self>) -> Box<dyn State> {
self
}
}
struct PendingReview {}
impl State for PendingReview {
fn approve(self: Box<Self>) -> Box<dyn State> {
Box::new(Published {})
}
}
struct Published {}
impl State for Published {
fn request_review(self: Box<Self>) -> Box<dyn State> {
self
}
fn approve(self: Box<Self>) -> Box<dyn State> {
self
}
}
Similar to the way request_review
on PendingReview
works, if we call the approve
method on a Draft
, it will have no effect because approve
will return self
. When we call approve
on PendingReview
, it returns a new, boxed instance of the Published
struct. The Published
struct implements the State
trait, and for both the request_review
method and the approve
method, it returns itself, because the post should stay in the Published
state in those cases.
Now we need to update the content
method on Post
. We want the value returned from content
to depend on the current state of the Post
, so we’re going to have the Post
delegate to a content
method defined on its state
, as shown in the following Listing:
Filename: src/lib.rs
impl Post {
pub fn content(&self) -> &str {
self.state.as_ref().unwrap().content(self)
}
}
We call the as_ref
method on the Option
because we want a reference to the value inside the Option
rather than ownership of the value. Because state
is an Option<Box<dyn State>>
, when we call as_ref
, an Option<&Box<dyn State>>
is returned. If we didn’t call as_ref
, we would get an error because we can’t move state
out of the borrowed &self
of the function parameter.
We then call the unwrap
method, which we know will never panic, because we know the methods on Post
ensure that state
will always contain a Some
value when those methods are done. This is one of the cases when we know that a None
value is never possible, even though the compiler isn’t able to understand that.
At this point, when we call content
on the &Box<dyn State>
, deref coercion will take effect on the &
and the Box
so the content
method will ultimately be called on the type that implements the State
trait. That means we need to add content
to the State
trait definition, and that is where we’ll put the logic for what content to return depending on which state we have, as shown in the following Listing:
Filename: src/lib.rs
trait State {
fn content<'a>(&self, post: &'a Post) -> &'a str {
""
}
}
struct Published {}
impl State for Published {
fn content<'a>(&self, post: &'a Post) -> &'a str {
&post.content
}
}
We add a default implementation for the content
method that returns an empty string slice. That means we don’t need to implement content
on the Draft
and PendingReview
structs. The Published
struct will override the content
method and return the value in post.content
.
Note that we need lifetime annotations on this method. We’re taking a reference to a post
as an argument and returning a reference to part of that post
, so the lifetime of the returned reference is related to the lifetime of the post
argument.
Why Not An Enum?
You may have been wondering why we didn’t use an enum
with the different possible post states as variants. That’s certainly a possible solution; try it and compare the end results to see which you prefer! One disadvantage of using an enum is that every place that checks the value of the enum will need a match
expression or similar to handle every possible variant. This could get more repetitive than this trait object solution.
Trade-offs of the State Pattern
We’ve shown that Rust is capable of implementing the object-oriented state pattern to encapsulate the different kinds of behavior a post should have in each state. The methods on Post
know nothing about the various behaviors. The way we organized the code, we have to look in only one place to know the different ways a published post can behave: the implementation of the State
trait on the Published
struct.
If we were to create an alternative implementation that didn’t use the state pattern, we might instead use match expressions in the methods on Post
or even in the main
code that checks the state of the post and changes behavior in those places. That would mean we would have to look in several places to understand all the implications of a post being in the published state! This would only increase the more states we added: each of those match
expressions would need another arm.
With the state pattern, the Post
methods and the places we use Post
don’t need match
expressions, and to add a new state, we would only need to add a new struct and implement the trait methods on that one struct.
The implementation using the state pattern is easy to extend to add more functionality. To see the simplicity of maintaining code that uses the state pattern, try a few of these suggestions:
- Add a
reject
method that changes the post’s state from PendingReview
back to Draft
.
- Require two calls to
approve
before the state can be changed to Published
.
- Allow users to add text content only when a post is in the Draft state. Hint: have the state object responsible for what might change about the content but not responsible for modifying the
Post
.
One downside of the state pattern is that, because the states implement the transitions between states, some of the states are coupled to each other. If we add another state between PendingReview
and Published
, such as Scheduled
, we would have to change the code in PendingReview
to transition to Scheduled
instead. It would be less work if PendingReview
didn’t need to change with the addition of a new state, but that would mean switching to another design pattern.
Another downside is that we’ve duplicated some logic. To eliminate some of the duplication, we might try to make default implementations for the request_review
and approve
methods on the State
trait that return self
; however, this wouldn’t work: when using State
as a trait object, the trait doesn’t know what the concrete self
will be exactly, so the return type isn’t known at compile time. (This is one of the dyn compatibility rules mentioned earlier.)
Other duplication includes the similar implementations of the request_review
and approve
methods on Post
. Both methods use Option::take
with the state
field of Post
, and if state
is Some
, they delegate to the wrapped value’s implementation of the same method and set the new value of the state
field to the result. If we had a lot of methods on Post
that followed this pattern, we might consider defining a macro to eliminate the repetition.
By implementing the state pattern exactly as it’s defined for object-oriented languages, we’re not taking as full advantage of Rust’s strengths as we could. Let’s look at some changes we can make to the blog
crate that can make invalid states and transitions into compile-time errors.
Encoding States and Behavior as Types
We’ll show you how to rethink the state pattern to get a different set of trade-offs. Rather than encapsulating the states and transitions completely so outside code has no knowledge of them, we’ll encode the states into different types. Consequently, Rust’s type checking system will prevent attempts to use draft posts where only published posts are allowed by issuing a compiler error.
Let’s consider the first part of main in Listing:
Filename: src/main.rs
fn main() {
let mut post = Post::new();
post.add_text("I ate a salad for lunch today");
assert_eq!("", post.content());
}
We still enable the creation of new posts in the draft state using Post::new
and the ability to add text to the post’s content. But instead of having a content
method on a draft post that returns an empty string, we’ll make it so draft posts don’t have the content
method at all. That way, if we try to get a draft post’s content, we’ll get a compiler error telling us the method doesn’t exist. As a result, it will be impossible for us to accidentally display draft post content in production because that code won’t even compile. The following Listing shows the definition of a Post
struct and a DraftPost
struct, as well as methods on each.
Filename: src/lib.rs
pub struct Post {
content: String,
}
pub struct DraftPost {
content: String,
}
impl Post {
pub fn new() -> DraftPost {
DraftPost {
content: String::new(),
}
}
pub fn content(&self) -> &str {
&self.content
}
}
impl DraftPost {
pub fn add_text(&mut self, text: &str) {
self.content.push_str(text);
}
}
Both the Post
and DraftPost
structs have a private content
field that stores the blog post text. The structs no longer have the state
field because we’re moving the encoding of the state to the types of the structs. The Post
struct will represent a published post, and it has a content
method that returns the content
.
We still have a Post::new
function, but instead of returning an instance of Post
, it returns an instance of DraftPost
. Because content
is private and there aren’t any functions that return Post
, it’s not possible to create an instance of Post
right now.
The DraftPost
struct has an add_text
method, so we can add text to content
as before, but note that DraftPost
does not have a content
method defined! So now the program ensures all posts start as draft posts, and draft posts don’t have their content available for display. Any attempt to get around these constraints will result in a compiler error.
So how do we get a published post? We want to enforce the rule that a draft post has to be reviewed and approved before it can be published. A post in the pending review state should still not display any content. Let’s implement these constraints by adding another struct, PendingReviewPost
, defining the request_review
method on DraftPost
to return a PendingReviewPost
and defining an approve
method on PendingReviewPost
to return a Post
, as shown in the following Listing.
Filename: src/lib.rs
impl DraftPost {
pub fn request_review(self) -> PendingReviewPost {
PendingReviewPost {
content: self.content,
}
}
}
pub struct PendingReviewPost {
content: String,
}
impl PendingReviewPost {
pub fn approve(self) -> Post {
Post {
content: self.content,
}
}
}
The request_review
and approve
methods take ownership of self
, thus consuming the DraftPost
and PendingReviewPost
instances and transforming them into a PendingReviewPost
and a published Post
, respectively. This way, we won’t have any lingering DraftPost
instances after we’ve called request_review
on them, and so forth. The PendingReviewPost
struct doesn’t have a content
method defined on it, so attempting to read its content results in a compiler error, as with DraftPost
. Because the only way to get a published Post
instance that does have a content
method defined is to call the approve
method on a PendingReviewPost
, and the only way to get a PendingReviewPost
is to call the request_review
method on a DraftPost
, we’ve now encoded the blog post workflow into the type system.
But we also have to make some small changes to main
. The request_review
and approve
methods return new instances rather than modifying the struct they’re called on, so we need to add more let post =
shadowing assignments to save the returned instances. We also can’t have the assertions about the draft and pending review posts’ contents be empty strings, nor do we need them: we can’t compile code that tries to use the content of posts in those states any longer. The updated code in main
is shown in the following Listing.
Filename: src/main.rs
use blog::Post;
fn main() {
let mut post = Post::new();
post.add_text("I ate a salad for lunch today");
let post = post.request_review();
let post = post.approve();
assert_eq!("I ate a salad for lunch today", post.content());
}
The changes we needed to make to main
to reassign post
mean that this implementation doesn’t quite follow the object-oriented state pattern anymore: the transformations between the states are no longer encapsulated entirely within the Post
implementation. However, our gain is that invalid states are now impossible because of the type system and the type checking that happens at compile time! This ensures that certain bugs, such as display of the content of an unpublished post, will be discovered before they make it to production.
We’ve seen that even though Rust is capable of implementing object-oriented design patterns, other patterns, such as encoding state into the type system, are also available in Rust. These patterns have different trade-offs. Although you might be very familiar with object-oriented patterns, rethinking the problem to take advantage of Rust’s features can provide benefits, such as preventing some bugs at compile time. Object-oriented patterns won’t always be the best solution in Rust due to certain features, like ownership, that object-oriented languages don’t have.
Patterns and Matching
Patterns are a special syntax in Rust for matching against the structure of types, both complex and simple. Using patterns in conjunction with match
expressions and other constructs gives you more control over a program’s control flow. A pattern consists of some combination of the following:
- Literals
- Destructured arrays, enums, structs, or tuples
- Variables
- Wildcards
- Placeholders
All the Places Patterns Can Be Used
match Arms
Formally, match
expressions are defined as the keyword match
, a value to match on, and one or more match arms that consist of a pattern and an expression to run if the value matches that arm’s pattern, like this:
match VALUE {
PATTERN => EXPRESSION,
PATTERN => EXPRESSION,
PATTERN => EXPRESSION,
}
For example, here’s the match
expression from the following Listing that matches on an Option<i32>
value in the variable x
:
match x {
None => None,
Some(i) => Some(i + 1),
}
One requirement for match
expressions is that they need to be exhaustive in the sense that all possibilities for the value in the match
expression must be accounted for. One way to ensure you’ve covered every possibility is to have a catchall pattern for the last arm: for example, a variable name matching any value can never fail and thus covers every remaining case.
The particular pattern _
will match anything, but it never binds to a variable, so it’s often used in the last match arm. The _
pattern can be useful when you want to ignore any value not specified, for example.
Conditional if let Expressions
In the previous Chapter, we discussed how to use if let
expressions mainly as a shorter way to write the equivalent of a match
that only matches one case. Optionally, if let
can have a corresponding else
containing code to run if the pattern in the if let
doesn’t match.
It’s also possible to mix and match if let
, else if
, and else if let
expressions. Doing so gives us more flexibility than a match
expression in which we can express only one value to compare with the patterns. Also, Rust doesn’t require that the conditions in a series of if let
, else if
, else if let
arms relate to each other.
The code in the following Listing determines what color to make your background based on a series of checks for several conditions. For this example, we’ve created variables with hardcoded values that a real program might receive from user input.
Filename: src/main.rs
fn main() {
let favorite_color: Option<&str> = None;
let is_tuesday = false;
let age: Result<u8, _> = "34".parse();
if let Some(color) = favorite_color {
println!("Using your favorite color, {color}, as the background");
} else if is_tuesday {
println!("Tuesday is green day!");
} else if let Ok(age) = age {
if age > 30 {
println!("Using purple as the background color");
} else {
println!("Using orange as the background color");
}
} else {
println!("Using blue as the background color");
}
}
If the user specifies a favorite color, that color is used as the background. If no favorite color is specified and today is Tuesday, the background color is green. Otherwise, if the user specifies their age as a string and we can parse it as a number successfully, the color is either purple or orange depending on the value of the number. If none of these conditions apply, the background color is blue.
This conditional structure lets us support complex requirements. With the hardcoded values we have here, this example will print Using purple as the background color
.
You can see that if let
can also introduce new variables that shadow existing variables in the same way that match
arms can: the line if let Ok(age) = age
introduces a new age
variable that contains the value inside the Ok
variant, shadowing the existing age
variable. This means we need to place the if age > 30
condition within that block: we can’t combine these two conditions into if let Ok(age) = age && age > 30
. The new age we want to compare to 30 isn’t valid until the new scope starts with the curly bracket.
The downside of using if let
expressions is that the compiler doesn’t check for exhaustiveness, whereas with match expressions it does. If we omitted the last else block and therefore missed handling some cases, the compiler would not alert us to the possible logic bug.
while let Conditional Loops
Similar in construction to if let
, the while let
conditional loop allows a while
loop to run for as long as a pattern continues to match. In the following Listing we show a while let
loop that waits on messages sent between threads, but in this case checking a Result
instead of an Option
.
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
for val in [1, 2, 3] {
tx.send(val).unwrap();
}
});
while let Ok(value) = rx.recv() {
println!("{value}");
}
This example prints 1
, 2
, and then 3
. The recv
method takes the first message out of the receiver side of the channel and returns an Ok(value)
. When we first saw recv
back in the previous Chapter, we unwrapped the error directly, or interacted with it as an iterator using a for
loop. As this Listing shows, though, we can also use while let
, because the recv
method returns Ok
each time a message arrives, as long as the sender exists, and then produces an Err
once the sender side disconnects.
for Loops
In a for
loop, the value that directly follows the keyword for
is a pattern. For example, in for x in y
, the x
is the pattern. The following Listing demonstrates how to use a pattern in a for
loop to destructure, or break apart, a tuple as part of the for
loop.
let v = vec!['a', 'b', 'c'];
for (index, value) in v.iter().enumerate() {
println!("{value} is at index {index}");
}
The code in this Listing will print the following:
$ cargo run
Compiling patterns v0.1.0 (file:///projects/patterns)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.52s
Running `target/debug/patterns`
a is at index 0
b is at index 1
c is at index 2
We adapt an iterator using the enumerate
method so it produces a value and the index for that value, placed into a tuple. The first value produced is the tuple (0, 'a')
. When this value is matched to the pattern (index, value)
, index
will be 0
and value
will be 'a'
, printing the first line of the output.
let Statements
Prior to this chapter, we had only explicitly discussed using patterns with match
and if let
, but in fact, we’ve used patterns in other places as well, including in let
statements. For example, consider this straightforward variable assignment with let
:
let x = 5;
Every time you’ve used a let
statement like this you’ve been using patterns, although you might not have realized it! More formally, a let
statement looks like this:
let PATTERN = EXPRESSION;
In statements like let x = 5
; with a variable name in the PATTERN
slot, the variable name is just a particularly simple form of a pattern. Rust compares the expression against the pattern and assigns any names it finds. So, in the let x = 5
; example, x
is a pattern that means “bind what matches here to the variable x.” Because the name x
is the whole pattern, this pattern effectively means “bind everything to the variable x
, whatever the value is.”
To see the pattern-matching aspect of let
more clearly, consider the following Listing, which uses a pattern with let
to destructure a tuple.
let (x, y, z) = (1, 2, 3);
Here, we match a tuple against a pattern. Rust compares the value (1, 2, 3)
to the pattern (x, y, z)
and sees that the value matches the pattern, in that the number of elements is the same in both, so Rust binds 1
to x
, 2
to y
, and 3
to z
. You can think of this tuple pattern as nesting three individual variable patterns inside it.
If the number of elements in the pattern doesn’t match the number of elements in the tuple, the overall type won’t match and we’ll get a compiler error. For example, the following Listing shows an attempt to destructure a tuple with three elements into two variables, which won’t work.
let (x, y) = (1, 2, 3);
Attempting to compile this code results in this type error:
$ cargo run
Compiling patterns v0.1.0 (file:/
error[E0308]: mismatched types
--> src/main.rs:2:9
|
2 | let (x, y) = (1, 2, 3);
| ^^^^^^ --------- this expression has type `({integer}, {integer}, {integer})`
| |
| expected a tuple with 3 elements, found one with 2 elements
|
= note: expected tuple `({integer}, {integer}, {integer})`
found tuple `(_, _)`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `patterns` (bin "patterns") due to 1 previous error
To fix the error, we could ignore one or more of the values in the tuple using _
or ..
. If the problem is that we have too many variables in the pattern, the solution is to make the types match by removing variables so the number of variables equals the number of elements in the tuple.
Function Parameters
Function parameters can also be patterns. The code in the following Listing, which declares a function named foo
that takes one parameter named x
of type i32
, should by now look familiar.
fn foo(x: i32) {
}
The x
part is a pattern! As we did with let
, we could match a tuple in a function’s arguments to the pattern. The following Listing splits the values in a tuple as we pass it to a function.
Filename: src/main.rs
fn print_coordinates(&(x, y): &(i32, i32)) {
println!("Current location: ({x}, {y})");
}
fn main() {
let point = (3, 5);
print_coordinates(&point);
}
This code prints Current location: (3, 5)
. The values &(3, 5)
match the pattern &(x, y)
, so x
is the value 3
and y
is the value 5
.
We can also use patterns in closure parameter lists in the same way as in function parameter lists because closures are similar to functions.
At this point, you’ve seen several ways to use patterns, but patterns don’t work the same in every place we can use them. In some places, the patterns must be irrefutable; in other circumstances, they can be refutable.
Refutability: Whether a Pattern Might Fail to Match
Patterns come in two forms: refutable and irrefutable. Patterns that will match for any possible value passed are irrefutable. An example would be x
in the statement let x = 5;
because x
matches anything and therefore cannot fail to match. Patterns that can fail to match for some possible value are refutable. An example would be Some(x)
in the expression if let Some(x) = a_value
because if the value in the a_value
variable is None
rather than Some
, the Some(x)
pattern will not match.
Function parameters, let
statements, and for
loops can only accept irrefutable patterns because the program cannot do anything meaningful when values don’t match. The if let
and while let
expressions and the let...else
statement accept refutable and irrefutable patterns, but the compiler warns against irrefutable patterns because, by definition, they’re intended to handle possible failure: the functionality of a conditional is in its ability to perform differently depending on success or failure.
Let’s look at an example of what happens when we try to use a refutable pattern where Rust requires an irrefutable pattern and vice versa. The following Listing shows a let
statement, but for the pattern, we’ve specified Some(x)
, a refutable pattern. As you might expect, this code will not compile.
let Some(x) = some_option_value;
If some_option_value
were a None
value, it would fail to match the pattern Some(x)
, meaning the pattern is refutable. However, the let
statement can only accept an irrefutable pattern because there is nothing valid the code can do with a None
value. At compile time, Rust will complain that we’ve tried to use a refutable pattern where an irrefutable pattern is required:
$ cargo run
Compiling patterns v0.1.0 (file:///projects/patterns)
error[E0005]: refutable pattern in local binding
--> src/main.rs:3:9
|
3 | let Some(x) = some_option_value;
| ^^^^^^^ pattern `None` not covered
|
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
= note: for more information, visit https://doc.rust-lang.org/book/ch19-02-refutability.html
= note: the matched value is of type `Option<i32>`
help: you might want to use `let else` to handle the variant that isn't matched
|
3 | let Some(x) = some_option_value else { todo!() };
| ++++++++++++++++
For more information about this error, try `rustc --explain E0005`.
error: could not compile `patterns` (bin "patterns") due to 1 previous error
Because we didn’t cover (and couldn’t cover!) every valid value with the pattern Some(x)
, Rust rightfully produces a compiler error.
If we have a refutable pattern where an irrefutable pattern is needed, we can fix it by changing the code that uses the pattern: instead of using let
, we can use if let
. Then if the pattern doesn’t match, the code will just skip the code in the curly brackets, giving it a way to continue validly. The following Listing shows how to fix the code.
let Some(x) = some_option_value else {
return;
};
However, if we give if let
an irrefutable pattern (a pattern that will always match), such as x
, as shown in Listing, the compiler will give a warning.
let x = 5 else {
return;
};
Rust complains that it doesn’t make sense to use if let with an irrefutable pattern:
$ cargo run
Compiling patterns v0.1.0 (file:/
warning: irrefutable `let...else` pattern
--> src/main.rs:2:5
|
2 | let x = 5 else {
| ^^^^^^^^^
|
= note: this pattern will always match, so the `else` clause is useless
= help: consider removing the `else` clause
= note: `#[warn(irrefutable_let_patterns)]` on by default
warning: `patterns` (bin "patterns") generated 1 warning
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.39s
Running `target/debug/patterns`
For this reason, match arms must use refutable patterns, except for the last arm, which should match any remaining values with an irrefutable pattern. Rust allows us to use an irrefutable pattern in a match
with only one arm, but this syntax isn’t particularly useful and could be replaced with a simpler let
statement.
Pattern Syntax
Matching Literals
You can match patterns against literals directly. The following code gives some examples:
let x = 1;
match x {
1 => println!("one"),
2 => println!("two"),
3 => println!("three"),
_ => println!("anything"),
}
This code prints one
because the value in x
is 1. This syntax is useful when you want your code to take an action if it gets a particular concrete value.
Matching Named Variables
Named variables are irrefutable patterns that match any value, and we’ve used them many times in this book. However, there is a complication when you use named variables in match
, if let
, or while let
expressions. Because each of these kinds of expression starts a new scope, variables declared as part of a pattern inside the expression will shadow those with the same name outside, as is the case with all variables. In the following Listing, we declare a variable named x
with the value Some(5)
and a variable y
with the value 10
. We then create a match
expression on the value x
. Look at the patterns in the match arms and println!
at the end, and try to figure out what the code will print before running this code or reading further.
Filename: src/main.rs
let x = Some(5);
let y = 10;
match x {
Some(50) => println!("Got 50"),
Some(y) => println!("Matched, y = {y}"),
_ => println!("Default case, x = {x:?}"),
}
println!("at the end: x = {x:?}, y = {y}");
Let’s walk through what happens when the match
expression runs. The pattern in the first match arm doesn’t match the defined value of x
, so the code continues.
The pattern in the second match arm introduces a new variable named y
that will match any value inside a Some
value. Because we’re in a new scope inside the match
expression, this is a new y
variable, not the y
we declared at the beginning with the value 10
. This new y
binding will match any value inside a Some
, which is what we have in x
. Therefore, this new y
binds to the inner value of the Some
in x
. That value is 5
, so the expression for that arm executes and prints Matched, y = 5
.
If x
had been a None
value instead of Some(5)
, the patterns in the first two arms wouldn’t have matched, so the value would have matched to the underscore. We didn’t introduce the x
variable in the pattern of the underscore arm, so the x
in the expression is still the outer x
that hasn’t been shadowed. In this hypothetical case, the match
would print Default case, x = None
.
When the match
expression is done, its scope ends, and so does the scope of the inner y
. The last println!
produces at the end: x = Some(5), y = 10
.
Multiple Patterns
You can match multiple patterns using the |
syntax, which is the pattern or operator. For example, in the following code we match the value of x
against the match arms, the first of which has an or option, meaning if the value of x
matches either of the values in that arm, that arm’s code will run:
let x = 1;
match x {
1 | 2 => println!("one or two"),
3 => println!("three"),
_ => println!("anything"),
}
This code prints one or two
.
Matching Ranges of Values with ..=
The ..=
syntax allows us to match to an inclusive range of values. In the following code, when a pattern matches any of the values within the given range, that arm will execute:
let x = 5;
match x {
1..=5 => println!("one through five"),
_ => println!("something else"),
}
If x
is 1
, 2
, 3
, 4
, or 5
, the first arm will match. This syntax is more convenient for multiple match values than using the |
operator to express the same idea; if we were to use |
we would have to specify 1 | 2 | 3 | 4 | 5
. Specifying a range is much shorter, especially if we want to match, say, any number between 1 and 1,000!
The compiler checks that the range isn’t empty at compile time, and because the only types for which Rust can tell if a range is empty or not are char
and numeric values, ranges are only allowed with numeric or char
values.
Here is an example using ranges of char
values:
let x = 'c';
match x {
'a'..='j' => println!("early ASCII letter"),
'k'..='z' => println!("late ASCII letter"),
_ => println!("something else"),
}
Rust can tell that 'c'
is within the first pattern’s range and prints early ASCII letter
.
Destructuring to Break Apart Values
Destructuring Structs
The following Listing shows a Point
struct with two fields, x
and y
, that we can break apart using a pattern with a let
statement.
Filename: src/main.rs
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 0, y: 7 };
let Point { x: a, y: b } = p;
assert_eq!(0, a);
assert_eq!(7, b);
}
This code creates the variables a
and b
that match the values of the x
and y
fields of the p
struct. This example shows that the names of the variables in the pattern don’t have to match the field names of the struct. However, it’s common to match the variable names to the field names to make it easier to remember which variables came from which fields. Because of this common usage, and because writing let Point { x: x, y: y } = p
; contains a lot of duplication, Rust has a shorthand for patterns that match struct fields: you only need to list the name of the struct field, and the variables created from the pattern will have the same names. The following Listing behaves in the same way as the code in the previous Listing, but the variables created in the let
pattern are x
and y
instead of a
and b
.
Filename: src/main.rs
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 0, y: 7 };
let Point { x, y } = p;
assert_eq!(0, x);
assert_eq!(7, y);
}
This code creates the variables x
and y
that match the x
and y
fields of the p
variable. The outcome is that the variables x
and y
contain the values from the p
struct.
We can also destructure with literal values as part of the struct pattern rather than creating variables for all the fields. Doing so allows us to test some of the fields for particular values while creating variables to destructure the other fields.
In the following Listing, we have a match
expression that separates Point
values into three cases: points that lie directly on the x
axis (which is true when y = 0
), on the y
axis (x = 0
), or on neither axis.
Filename: src/main.rs
fn main() {
let p = Point { x: 0, y: 7 };
match p {
Point { x, y: 0 } => println!("On the x axis at {x}"),
Point { x: 0, y } => println!("On the y axis at {y}"),
Point { x, y } => {
println!("On neither axis: ({x}, {y})");
}
}
}
The first arm will match any point that lies on the x
axis by specifying that the y
field matches if its value matches the literal 0
. The pattern still creates an x
variable that we can use in the code for this arm.
Similarly, the second arm matches any point on the y
axis by specifying that the x
field matches if its value is 0
and creates a variable y
for the value of the y
field. The third arm doesn’t specify any literals, so it matches any other Point
and creates variables for both the x
and y
fields.
In this example, the value p
matches the second arm by virtue of x
containing a 0
, so this code will print On the y axis at 7
.
Remember that a match
expression stops checking arms once it has found the first matching pattern, so even though Point { x: 0, y: 0 }
is on the x
axis and the y
axis, this code would only print On the x axis at 0
.
Destructuring Enums
We’ve destructured enums in this book, but we haven’t yet explicitly discussed that the pattern to destructure an enum corresponds to the way the data stored within the enum is defined. As an example, in the following Listing we use the Message
enum and write a match
with patterns that will destructure each inner value.
Filename: src/main.rs
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
fn main() {
let msg = Message::ChangeColor(0, 160, 255);
match msg {
Message::Quit => {
println!("The Quit variant has no data to destructure.");
}
Message::Move { x, y } => {
println!("Move in the x direction {x} and in the y direction {y}");
}
Message::Write(text) => {
println!("Text message: {text}");
}
Message::ChangeColor(r, g, b) => {
println!("Change color to red {r}, green {g}, and blue {b}");
}
}
}
This code will print Change color to red 0, green 160, and blue 255
.
For enum variants without any data, like Message::Quit
, we can’t destructure the value any further. We can only match on the literal Message::Quit
value, and no variables are in that pattern.
For struct-like enum variants, such as Message::Move, we can use a pattern similar to the pattern we specify to match structs. After the variant name, we place curly brackets and then list the fields with variables so we break apart the pieces to use in the code for this arm. Here we use the shorthand form as we did in the previous Listing.
For tuple-like enum variants, like Message::Write
that holds a tuple with one element and Message::ChangeColor
that holds a tuple with three elements, the pattern is similar to the pattern we specify to match tuples. The number of variables in the pattern must match the number of elements in the variant we’re matching.
Destructuring Nested Structs and Enums
So far, our examples have all been matching structs or enums one level deep, but matching can work on nested items too! For example, we can refactor the code in the previous Listing to support RGB and HSV colors in the ChangeColor
message, as shown in the following Listing.
enum Color {
Rgb(i32, i32, i32),
Hsv(i32, i32, i32),
}
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(Color),
}
fn main() {
let msg = Message::ChangeColor(Color::Hsv(0, 160, 255));
match msg {
Message::ChangeColor(Color::Rgb(r, g, b)) => {
println!("Change color to red {r}, green {g}, and blue {b}");
}
Message::ChangeColor(Color::Hsv(h, s, v)) => {
println!("Change color to hue {h}, saturation {s}, value {v}");
}
_ => (),
}
}
The pattern of the first arm in the match
expression matches a Message::ChangeColor
enum variant that contains a Color::Rgb
variant; then the pattern binds to the three inner i32
values. The pattern of the second arm also matches a Message::ChangeColor
enum variant, but the inner enum matches Color::Hsv
instead. We can specify these complex conditions in one match
expression, even though two enums are involved.
Destructuring Structs and Tuples
We can mix, match, and nest destructuring patterns in even more complex ways. The following example shows a complicated destructure where we nest structs and tuples inside a tuple and destructure all the primitive values out:
let ((feet, inches), Point { x, y }) = ((3, 10), Point { x: 3, y: -10 });
This code lets us break complex types into their component parts so we can use the values we’re interested in separately.
Destructuring with patterns is a convenient way to use pieces of values, such as the value from each field in a struct, separately from each other.
Ignoring Values in a Pattern
You’ve seen that it’s sometimes useful to ignore values in a pattern, such as in the last arm of a match
, to get a catchall that doesn’t actually do anything but does account for all remaining possible values. There are a few ways to ignore entire values or parts of values in a pattern: using the _
pattern (which you’ve seen), using the _
pattern within another pattern, using a name that starts with an underscore, or using ..
to ignore remaining parts of a value. Let’s explore how and why to use each of these patterns.
An Entire Value with _
We’ve used the underscore as a wildcard pattern that will match any value but not bind to the value. This is especially useful as the last arm in a match
expression, but we can also use it in any pattern, including function parameters, as shown in the following Listing.
Filename: src/main.rs
fn foo(_: i32, y: i32) {
println!("This code only uses the y parameter: {y}");
}
fn main() {
foo(3, 4);
}
This code will completely ignore the value 3
passed as the first argument, and will print This code only uses the y parameter: 4
.
In most cases when you no longer need a particular function parameter, you would change the signature so it doesn’t include the unused parameter. Ignoring a function parameter can be especially useful in cases when, for example, you’re implementing a trait when you need a certain type signature but the function body in your implementation doesn’t need one of the parameters. You then avoid getting a compiler warning about unused function parameters, as you would if you used a name instead.
Parts of a Value with a Nested _
We can also use _
inside another pattern to ignore just part of a value, for example, when we want to test for only part of a value but have no use for the other parts in the corresponding code we want to run. The following Listing shows code responsible for managing a setting’s value. The business requirements are that the user should not be allowed to overwrite an existing customization of a setting but can unset the setting and give it a value if it is currently unset.
let mut setting_value = Some(5);
let new_setting_value = Some(10);
match (setting_value, new_setting_value) {
(Some(_), Some(_)) => {
println!("Can't overwrite an existing customized value");
}
_ => {
setting_value = new_setting_value;
}
}
println!("setting is {setting_value:?}");
This code will print Can't overwrite an existing customized value
and then setting is Some(5)
. In the first match arm, we don’t need to match on or use the values inside either Some
variant, but we do need to test for the case when setting_value
and new_setting_value
are the Some
variant. In that case, we print the reason for not changing setting_value
, and it doesn’t get changed.
In all other cases (if either setting_value
or new_setting_value
is None
) expressed by the _
pattern in the second arm, we want to allow new_setting_value
to become setting_value
.
We can also use underscores in multiple places within one pattern to ignore particular values. The following Listing shows an example of ignoring the second and fourth values in a tuple of five items.
let numbers = (2, 4, 8, 16, 32);
match numbers {
(first, _, third, _, fifth) => {
println!("Some numbers: {first}, {third}, {fifth}");
}
}
This code will print Some numbers: 2, 8, 32
, and the values 4
and 16
will be ignored.
An Unused Variable by Starting Its Name with _
If you create a variable but don’t use it anywhere, Rust will usually issue a warning because an unused variable could be a bug. However, sometimes it’s useful to be able to create a variable you won’t use yet, such as when you’re prototyping or just starting a project. In this situation, you can tell Rust not to warn you about the unused variable by starting the name of the variable with an underscore. In the following Listing, we create two unused variables, but when we compile this code, we should only get a warning about one of them.
Filename: src/main.rs
fn main() {
let _x = 5;
let y = 10;
}
Here, we get a warning about not using the variable y
, but we don’t get a warning about not using _x
.
Note that there is a subtle difference between using only _
and using a name that starts with an underscore. The syntax _x
still binds the value to the variable, whereas _
doesn’t bind at all. To show a case where this distinction matters, the following Listing will provide us with an error.
let s = Some(String::from("Hello!"));
if let Some(_s) = s {
println!("found a string");
}
println!("{s:?}");
We’ll receive an error because the s
value will still be moved into _s
, which prevents us from using s
again. However, using the underscore by itself doesn’t ever bind to the value. The following Listing will compile without any errors because s
doesn’t get moved into _
.
let s = Some(String::from("Hello!"));
if let Some(_) = s {
println!("found a string");
}
println!("{s:?}");
This code works just fine because we never bind s
to anything; it isn’t moved.
Remaining Parts of a Value with ..
With values that have many parts, we can use the ..
syntax to use specific parts and ignore the rest, avoiding the need to list underscores for each ignored value. The ..
pattern ignores any parts of a value that we haven’t explicitly matched in the rest of the pattern. In the following Listing, we have a Point
struct that holds a coordinate in three-dimensional space. In the match
expression, we want to operate only on the x
coordinate and ignore the values in the y
and z
fields.
struct Point {
x: i32,
y: i32,
z: i32,
}
let origin = Point { x: 0, y: 0, z: 0 };
match origin {
Point { x, .. } => println!("x is {x}"),
}
We list the x
value and then just include the ..
pattern. This is quicker than having to list y: _
and z: _
, particularly when we’re working with structs that have lots of fields in situations where only one or two fields are relevant.
The syntax ..
will expand to as many values as it needs to be. The following Listing shows how to use ..
with a tuple.
Filename: src/main.rs
fn main() {
let numbers = (2, 4, 8, 16, 32);
match numbers {
(first, .., last) => {
println!("Some numbers: {first}, {last}");
}
}
}
In this code, the first and last value are matched with first
and last
. The ..
will match and ignore everything in the middle.
However, using ..
must be unambiguous. If it is unclear which values are intended for matching and which should be ignored, Rust will give us an error. The following Listing shows an example of using ..
ambiguously, so it will not compile.
Filename: src/main.rs
fn main() {
let numbers = (2, 4, 8, 16, 32);
match numbers {
(.., second, ..) => {
println!("Some numbers: {second}")
},
}
}
When we compile this example, we get this error:
$ cargo run
Compiling patterns v0.1.0 (file:/
error: `..` can only be used once per tuple pattern
--> src/main.rs:5:22
|
5 | (.., second, ..) => {
| -- ^^ can only be used once per tuple pattern
| |
| previously used here
error: could not compile `patterns` (bin "patterns") due to 1 previous error
It’s impossible for Rust to determine how many values in the tuple to ignore before matching a value with second
and then how many further values to ignore thereafter. This code could mean that we want to ignore 2
, bind second
to 4
, and then ignore 8
, 16
, and 32
; or that we want to ignore 2
and 4
, bind second
to 8
, and then ignore 16
and 32
; and so forth. The variable name second
doesn’t mean anything special to Rust, so we get a compiler error because using ..
in two places like this is ambiguous.
A match guard is an additional if
condition, specified after the pattern in a match
arm, that must also match for that arm to be chosen. Match guards are useful for expressing more complex ideas than a pattern alone allows. Note, however, that they are only available in match
expressions, not in if let
or while let
expressions.
The condition can use variables created in the pattern. The following Listing shows a match
where the first arm has the pattern Some(x)
and also has a match guard of if x % 2 == 0
(which will be true
if the number is even).
let num = Some(4);
match num {
Some(x) if x % 2 == 0 => println!("The number {x} is even"),
Some(x) => println!("The number {x} is odd"),
None => (),
}
This example will print The number 4 is even
. When num
is compared to the pattern in the first arm, it matches because Some(4)
matches Some(x)
. Then the match guard checks whether the remainder of dividing x
by 2 is equal to 0, and because it is, the first arm is selected.
If num
had been Some(5)
instead, the match guard in the first arm would have been false
because the remainder of 5 divided by 2 is 1, which is not equal to 0. Rust would then go to the second arm, which would match because the second arm doesn’t have a match guard and therefore matches any Some
variant.
There is no way to express the if x % 2 == 0
condition within a pattern, so the match guard gives us the ability to express this logic. The downside of this additional expressiveness is that the compiler doesn’t try to check for exhaustiveness when match guard expressions are involved.
In the previous Listing, we mentioned that we could use match guards to solve our pattern-shadowing problem. Recall that we created a new variable inside the pattern in the match
expression instead of using the variable outside the match
. That new variable meant we couldn’t test against the value of the outer variable. The following Listing shows how we can use a match guard to fix this problem.
Filename: src/main.rs
fn main() {
let x = Some(5);
let y = 10;
match x {
Some(50) => println!("Got 50"),
Some(n) if n == y => println!("Matched, n = {n}"),
_ => println!("Default case, x = {x:?}"),
}
println!("at the end: x = {x:?}, y = {y}");
}
This code will now print Default case, x = Some(5)
. The pattern in the second match arm doesn’t introduce a new variable y
that would shadow the outer y
, meaning we can use the outer y
in the match guard. Instead of specifying the pattern as Some(y)
, which would have shadowed the outer y
, we specify Some(n)
. This creates a new variable n
that doesn’t shadow anything because there is no n
variable outside the match
.
The match guard if n == y
is not a pattern and therefore doesn’t introduce new variables. This y
is the outer y
rather than a new y
shadowing it, and we can look for a value that has the same value as the outer y
by comparing n
to y
.
You can also use the or operator |
in a match guard to specify multiple patterns; the match guard condition will apply to all the patterns. The following Listing shows the precedence when combining a pattern that uses |
with a match guard. The important part of this example is that the if y
match guard applies to 4
, 5
, and 6
, even though it might look like if y
only applies to 6
.
let x = 4;
let y = false;
match x {
4 | 5 | 6 if y => println!("yes"),
_ => println!("no"),
}
The match condition states that the arm only matches if the value of x
is equal to 4
, 5
, or 6
and if y
is true
. When this code runs, the pattern of the first arm matches because x
is 4
, but the match guard if y
is false
, so the first arm is not chosen. The code moves on to the second arm, which does match, and this program prints no
. The reason is that the if
condition applies to the whole pattern 4 | 5 | 6
, not just to the last value 6
. In other words, the precedence of a match guard in relation to a pattern behaves like this:
(4 | 5 | 6) if y => ...
rather than this:
4 | 5 | (6 if y) => ...
After running the code, the precedence behavior is evident: if the match guard were applied only to the final value in the list of values specified using the |
operator, the arm would have matched and the program would have printed yes
.
@ Bindings
The at operator @
lets us create a variable that holds a value at the same time we’re testing that value for a pattern match. In the following Listing, we want to test that a Message::Hello id
field is within the range 3..=7
. We also want to bind the value to the variable id_variable
so we can use it in the code associated with the arm. We could name this variable id
, the same as the field, but for this example we’ll use a different name.
enum Message {
Hello { id: i32 },
}
let msg = Message::Hello { id: 5 };
match msg {
Message::Hello {
id: id_variable @ 3..=7,
} => println!("Found an id in range: {id_variable}"),
Message::Hello { id: 10..=12 } => {
println!("Found an id in another range")
}
Message::Hello { id } => println!("Found some other id: {id}"),
}
This example will print Found an id in range: 5
. By specifying id_variable @
before the range 3..=7
, we’re capturing whatever value matched the range while also testing that the value matched the range pattern.
In the second arm, where we only have a range specified in the pattern, the code associated with the arm doesn’t have a variable that contains the actual value of the id
field. The id
field’s value could have been 10, 11, or 12, but the code that goes with that pattern doesn’t know which it is. The pattern code isn’t able to use the value from the id
field, because we haven’t saved the id
value in a variable.
In the last arm, where we’ve specified a variable without a range, we do have the value available to use in the arm’s code in a variable named id
. The reason is that we’ve used the struct field shorthand syntax. But we haven’t applied any test to the value in the id
field in this arm, as we did with the first two arms: any value would match this pattern.
Using @
lets us test a value and save it in a variable within one pattern.
Advanced Features
Unsafe Rust
Unsafe Rust exists because, by nature, static analysis is conservative. When the compiler tries to determine whether or not code upholds the guarantees, it’s better for it to reject some valid programs than to accept some invalid programs. Although the code might be okay, if the Rust compiler doesn’t have enough information to be confident, it will reject the code. In these cases, you can use unsafe code to tell the compiler, “Trust me, I know what I’m doing.” Be warned, however, that you use unsafe Rust at your own risk: if you use unsafe code incorrectly, problems can occur due to memory unsafety, such as null pointer dereferencing.
Another reason Rust has an unsafe alter ego is that the underlying computer hardware is inherently unsafe. If Rust didn’t let you do unsafe operations, you couldn’t do certain tasks. Rust needs to allow you to do low-level systems programming, such as directly interacting with the operating system or even writing your own operating system. Working with low-level systems programming is one of the goals of the language.
Unsafe Superpowers
To switch to unsafe Rust, use the unsafe
keyword and then start a new block that holds the unsafe code. You can take five actions in unsafe Rust that you can’t in safe Rust, which we call unsafe superpowers. Those superpowers include the ability to:
- Dereference a raw pointer
- Call an unsafe function or method
- Access or modify a mutable static variable
- Implement an unsafe trait
- Access fields of a
union
It’s important to understand that unsafe
doesn’t turn off the borrow checker or disable any of Rust’s other safety checks: if you use a reference in unsafe code, it will still be checked. The unsafe
keyword only gives you access to these five features that are then not checked by the compiler for memory safety. You’ll still get some degree of safety inside of an unsafe block.
In addition, unsafe
does not mean the code inside the block is necessarily dangerous or that it will definitely have memory safety problems: the intent is that as the programmer, you’ll ensure the code inside an unsafe block will access memory in a valid way.
People are fallible and mistakes will happen, but by requiring these five unsafe operations to be inside blocks annotated with unsafe
, you’ll know that any errors related to memory safety must be within an unsafe
block. Keep unsafe
blocks small; you’ll be thankful later when you investigate memory bugs.
To isolate unsafe code as much as possible, it’s best to enclose such code within a safe abstraction and provide a safe API, which we’ll discuss later in the chapter when we examine unsafe functions and methods. Parts of the standard library are implemented as safe abstractions over unsafe code that has been audited. Wrapping unsafe code in a safe abstraction prevents uses of unsafe from leaking out into all the places that you or your users might want to use the functionality implemented with unsafe code, because using a safe abstraction is safe.
Dereferencing a Raw Pointer
The compiler ensures references are always valid. Unsafe Rust has two new types called raw pointers that are similar to references. As with references, raw pointers can be immutable or mutable and are written as *const T
and *mut T
, respectively. The asterisk isn’t the dereference operator; it’s part of the type name. In the context of raw pointers, immutable means that the pointer can’t be directly assigned to after being dereferenced.
Different from references and smart pointers, raw pointers:
- Are allowed to ignore the borrowing rules by having both immutable and mutable pointers or multiple mutable pointers to the same location
- Aren’t guaranteed to point to valid memory
- Are allowed to be null
- Don’t implement any automatic cleanup
By opting out of having Rust enforce these guarantees, you can give up guaranteed safety in exchange for greater performance or the ability to interface with another language or hardware where Rust’s guarantees don’t apply.
The following Listing shows how to create an immutable and a mutable raw pointer.
let mut num = 5;
let r1 = &raw const num;
let r2 = &raw mut num;
Notice that we don’t include the unsafe
keyword in this code. We can create raw pointers in safe code; we just can’t dereference raw pointers outside an unsafe block, as you’ll see in a bit.
We’ve created raw pointers by using the raw borrow operators: &raw const num
creates a *const i32
immutable raw pointer, and &raw mut num
creates a *mut i32
mutable raw pointer. Because we created them directly from a local variable, we know these particular raw pointers are valid, but we can’t make that assumption about just any raw pointer.
To demonstrate this, next we’ll create a raw pointer whose validity we can’t be so certain of, using as
to cast a value instead of using the raw borrow operators. The following Listing shows how to create a raw pointer to an arbitrary location in memory. Trying to use arbitrary memory is undefined: there might be data at that address or there might not, the compiler might optimize the code so there is no memory access, or the program might terminate with a segmentation fault. Usually, there is no good reason to write code like this, especially in cases where you can use a raw borrow operator instead, but it is possible.
let address = 0x012345usize;
let r = address as *const i32;
Recall that we can create raw pointers in safe code, but we can’t dereference raw pointers and read the data being pointed to. In the following Listing, we use the dereference operator *
on a raw pointer that requires an unsafe
block.
let mut num = 5;
let r1 = &raw const num;
let r2 = &raw mut num;
unsafe {
println!("r1 is: {}", *r1);
println!("r2 is: {}", *r2);
}
Creating a pointer does no harm; it’s only when we try to access the value that it points at that we might end up dealing with an invalid value.
Note also that we created *const i32
and *mut i32
raw pointers that both pointed to the same memory location, where num
is stored. If we instead tried to create an immutable and a mutable reference to num
, the code would not have compiled because Rust’s ownership rules don’t allow a mutable reference at the same time as any immutable references. With raw pointers, we can create a mutable pointer and an immutable pointer to the same location and change data through the mutable pointer, potentially creating a data race. Be careful!
With all of these dangers, why would you ever use raw pointers? One major use case is when interfacing with C code. Another case is when building up safe abstractions that the borrow checker doesn’t understand. We’ll introduce unsafe functions and then look at an example of a safe abstraction that uses unsafe code.
Calling an Unsafe Function or Method
The second type of operation you can perform in an unsafe block is calling unsafe functions. Unsafe functions and methods look exactly like regular functions and methods, but they have an extra unsafe
before the rest of the definition. The unsafe
keyword in this context indicates the function has requirements we need to uphold when we call this function, because Rust can’t guarantee we’ve met these requirements. By calling an unsafe function within an unsafe
block, we’re saying that we’ve read this function’s documentation and we take responsibility for upholding the function’s contracts.
Here is an unsafe function named dangerous that doesn’t do anything in its body:
unsafe fn dangerous() {}
unsafe {
dangerous();
}
We must call the dangerous
function within a separate unsafe
block. If we try to call dangerous
without the unsafe
block, we’ll get an error:
$ cargo run
Compiling unsafe-example v0.1.0 (file:/
error[E0133]: call to unsafe function `dangerous` is unsafe and requires unsafe block
--> src/main.rs:4:5
|
4 | dangerous();
| ^^^^^^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
For more information about this error, try `rustc --explain E0133`.
error: could not compile `unsafe-example` (bin "unsafe-example") due to 1 previous error
With the unsafe
block, we’re asserting to Rust that we’ve read the function’s documentation, we understand how to use it properly, and we’ve verified that we’re fulfilling the contract of the function.
To perform unsafe operations in the body of an unsafe function, you still need to use an unsafe
block, just as within a regular function, and the compiler will warn you if you forget. This helps to keep unsafe
blocks as small as possible, as unsafe operations may not be needed across the whole function body.
Creating a Safe Abstraction over Unsafe Code
Just because a function contains unsafe code doesn’t mean we need to mark the entire function as unsafe. In fact, wrapping unsafe code in a safe function is a common abstraction. As an example, let’s study the split_at_mut
function from the standard library, which requires some unsafe code. We’ll explore how we might implement it. This safe method is defined on mutable slices: it takes one slice and makes it two by splitting the slice at the index given as an argument. The following Listing shows how to use split_at_mut
.
let mut v = vec![1, 2, 3, 4, 5, 6];
let r = &mut v[..];
let (a, b) = r.split_at_mut(3);
assert_eq!(a, &mut [1, 2, 3]);
assert_eq!(b, &mut [4, 5, 6]);
We can’t implement this function using only safe Rust. An attempt might look something like Listing, which won’t compile. For simplicity, we’ll implement split_at_mut
as a function rather than a method and only for slices of i32
values rather than for a generic type T
.
fn split_at_mut(values: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
let len = values.len();
assert!(mid <= len);
(&mut values[..mid], &mut values[mid..])
}
When we try to compile the code in the Listing, we’ll get an error.
$ cargo run
Compiling unsafe-example v0.1.0 (file:///projects/unsafe-example)
error[E0499]: cannot borrow `*values` as mutable more than once at a time
--> src/main.rs:6:31
|
1 | fn split_at_mut(values: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
| - let's call the lifetime of this reference `'1`
...
6 | (&mut values[..mid], &mut values[mid..])
| --------------------------^^^^^^--------
| | | |
| | | second mutable borrow occurs here
| | first mutable borrow occurs here
| returning this value requires that `*values` is borrowed for `'1`
|
= help: use `.split_at_mut(position)` to obtain two mutable non-overlapping sub-slices
For more information about this error, try `rustc --explain E0499`.
error: could not compile `unsafe-example` (bin "unsafe-example") due to 1 previous error
Rust’s borrow checker can’t understand that we’re borrowing different parts of the slice; it only knows that we’re borrowing from the same slice twice. Borrowing different parts of a slice is fundamentally okay because the two slices aren’t overlapping, but Rust isn’t smart enough to know this. When we know code is okay, but Rust doesn’t, it’s time to reach for unsafe code.
The following Listing shows how to use an unsafe
block, a raw pointer, and some calls to unsafe functions to make the implementation of split_at_mut
work.
use std::slice;
fn split_at_mut(values: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
let len = values.len();
let ptr = values.as_mut_ptr();
assert!(mid <= len);
unsafe {
(
slice::from_raw_parts_mut(ptr, mid),
slice::from_raw_parts_mut(ptr.add(mid), len - mid),
)
}
}
In this case, because we have a mutable slice to i32
values, as_mut_ptr
returns a raw pointer with the type *mut i32
, which we’ve stored in the variable ptr
.
We keep the assertion that the mid
index is within the slice. Then we get to the unsafe code: the slice::from_raw_parts_mut
function takes a raw pointer and a length, and it creates a slice. We use it to create a slice that starts from ptr
and is mid
items long. Then we call the add
method on ptr
with mid
as an argument to get a raw pointer that starts at mid
, and we create a slice using that pointer and the remaining number of items after mid
as the length.
The function slice::from_raw_parts_mut
is unsafe because it takes a raw pointer and must trust that this pointer is valid. The add
method on raw pointers is also unsafe because it must trust that the offset location is also a valid pointer. Therefore, we had to put an unsafe
block around our calls to slice::from_raw_parts_mut
and add
so we could call them. By looking at the code and by adding the assertion that mid
must be less than or equal to len
, we can tell that all the raw pointers used within the unsafe
block will be valid pointers to data within the slice. This is an acceptable and appropriate use of unsafe
.
Note that we don’t need to mark the resultant split_at_mut
function as unsafe
, and we can call this function from safe Rust. We’ve created a safe abstraction to the unsafe code with an implementation of the function that uses unsafe
code in a safe way, because it creates only valid pointers from the data this function has access to.
In contrast, the use of slice::from_raw_parts_mut
in the following Listing would likely crash when the slice is used. This code takes an arbitrary memory location and creates a slice 10,000 items long.
use std::slice;
let address = 0x01234usize;
let r = address as *mut i32;
let values: &[i32] = unsafe { slice::from_raw_parts_mut(r, 10000) };
We don’t own the memory at this arbitrary location, and there is no guarantee that the slice this code creates contains valid i32
values. Attempting to use values
as though it’s a valid slice results in undefined behavior.
Using extern Functions to Call External Code
Sometimes, your Rust code might need to interact with code written in another language. For this, Rust has the keyword extern
that facilitates the creation and use of a Foreign Function Interface (FFI). An FFI is a way for a programming language to define functions and enable a different (foreign) programming language to call those functions.
The following Listing demonstrates how to set up an integration with the abs
function from the C standard library. Functions declared within extern
blocks are generally unsafe to call from Rust code, so extern
blocks must also be marked unsafe
. The reason is that other languages don’t enforce Rust’s rules and guarantees, and Rust can’t check them, so responsibility falls on the programmer to ensure safety.
Filename: src/main.rs
unsafe extern "C" {
fn abs(input: i32) -> i32;
}
fn main() {
unsafe {
println!("Absolute value of -3 according to C: {}", abs(-3));
}
}
Within the unsafe extern "C"
block, we list the names and signatures of external functions from another language we want to call. The "C"
part defines which application binary interface (ABI) the external function uses: the ABI defines how to call the function at the assembly level. The "C"
ABI is the most common and follows the C programming language’s ABI. Information about all the ABIs Rust supports is available in the Rust Reference.
Every item declared within an unsafe extern
block is implicitly unsafe
. However, some FFI functions are safe to call. For example, the abs
function from C’s standard library does not have any memory safety considerations and we know it can be called with any i32
. In cases like this, we can use the safe
keyword to say that this specific function is safe to call even though it is in an unsafe extern
block. Once we make that change, calling it no longer requires an unsafe
block, as shown in the following Listing.
Filename: src/main.rs
unsafe extern "C" {
safe fn abs(input: i32) -> i32;
}
fn main() {
println!("Absolute value of -3 according to C: {}", abs(-3));
}
Marking a function as safe
does not inherently make it safe! Instead, it is like a promise you are making to Rust that it is safe. It is still your responsibility to make sure that promise is kept!
Calling Rust Functions from Other Languages
We can also use extern
to create an interface that allows other languages to call Rust functions. Instead of creating a whole extern
block, we add the extern
keyword and specify the ABI to use just before the fn
keyword for the relevant function. We also need to add an #[unsafe(no_mangle)]
annotation to tell the Rust compiler not to mangle the name of this function. Mangling is when a compiler changes the name we’ve given a function to a different name that contains more information for other parts of the compilation process to consume but is less human readable. Every programming language compiler mangles names slightly differently, so for a Rust function to be nameable by other languages, we must disable the Rust compiler’s name mangling. This is unsafe because there might be name collisions across libraries without the built-in mangling, so it is our responsibility to make sure the name we choose is safe to export without mangling.
In the following example, we make the call_from_c
function accessible from C code, after it’s compiled to a shared library and linked from C:
#[unsafe(no_mangle)]
pub extern "C" fn call_from_c() {
println!("Just called a Rust function from C!");
}
This usage of extern
requires unsafe
only in the attribute, not on the extern
block.
Accessing or Modifying a Mutable Static Variable
In this book, we’ve not yet talked about global variables, which Rust does support but can be problematic with Rust’s ownership rules. If two threads are accessing the same mutable global variable, it can cause a data race.
In Rust, global variables are called static variables. The following Listing shows an example declaration and use of a static variable with a string slice as a value.
Filename: src/main.rs
static HELLO_WORLD: &str = "Hello, world!";
fn main() {
println!("name is: {HELLO_WORLD}");
}
Static variables are similar to constants. The names of static variables are in SCREAMING_SNAKE_CASE
by convention. Static variables can only store references with the 'static
lifetime, which means the Rust compiler can figure out the lifetime and we aren’t required to annotate it explicitly. Accessing an immutable static variable is safe.
A subtle difference between constants and immutable static variables is that values in a static variable have a fixed address in memory. Using the value will always access the same data. Constants, on the other hand, are allowed to duplicate their data whenever they’re used. Another difference is that static variables can be mutable. Accessing and modifying mutable static variables is unsafe. The following Listing shows how to declare, access, and modify a mutable static variable named COUNTER
.
Filename: src/main.rs
static mut COUNTER: u32 = 0;
unsafe fn add_to_count(inc: u32) {
unsafe {
COUNTER += inc;
}
}
fn main() {
unsafe {
add_to_count(3);
println!("COUNTER: {}", *(&raw const COUNTER));
}
}
As with regular variables, we specify mutability using the mut
keyword. Any code that reads or writes from COUNTER
must be within an unsafe
block. This code compiles and prints COUNTER: 3
as we would expect because it’s single threaded. Having multiple threads access COUNTER
would likely result in data races, so it is undefined behavior. Therefore, we need to mark the entire function as unsafe
, and document the safety limitation, so anyone calling the function knows what they are and are not allowed to do safely.
Whenever we write an unsafe function, it is idiomatic to write a comment starting with SAFETY
and explaining what the caller needs to do to call the function safely. Likewise, whenever we perform an unsafe operation, it is idiomatic to write a comment starting with SAFETY
to explain how the safety rules are upheld.
Additionally, the compiler will not allow you to create references to a mutable static variable. You can only access it via a raw pointer, created with one of the raw borrow operators. That includes in cases where the reference is created invisibly, as when it is used in the println!
in this code listing. The requirement that references to static mutable variables can only be created via raw pointers helps make the safety requirements for using them more obvious.
With mutable data that is globally accessible, it’s difficult to ensure there are no data races, which is why Rust considers mutable static variables to be unsafe. Where possible, it’s preferable to use the concurrency techniques and thread-safe smart pointers so the compiler checks that data access from different threads is done safely.
Implementing an Unsafe Trait
We can use unsafe
to implement an unsafe trait. A trait is unsafe when at least one of its methods has some invariant that the compiler can’t verify. We declare that a trait is unsafe
by adding the unsafe
keyword before trait
and marking the implementation of the trait as unsafe
too, as shown in the following Listing.
unsafe trait Foo {
}
unsafe impl Foo for i32 {
}
By using unsafe impl
, we’re promising that we’ll uphold the invariants that the compiler can’t verify.
As an example, recall the Sync
and Send
marker traits: the compiler implements these traits automatically if our types are composed entirely of other types that implement Send
and Sync
. If we implement a type that contains a type that does not implement Send
or Sync
, such as raw pointers, and we want to mark that type as Send
or Sync
, we must use unsafe
. Rust can’t verify that our type upholds the guarantees that it can be safely sent across threads or accessed from multiple threads; therefore, we need to do those checks manually and indicate as such with unsafe
.
Accessing Fields of a Union
The final action that works only with unsafe
is accessing fields of a union. A union
is similar to a struct
, but only one declared field is used in a particular instance at one time. Unions are primarily used to interface with unions in C code. Accessing union fields is unsafe because Rust can’t guarantee the type of the data currently being stored in the union instance. You can learn more about unions in the Rust Reference.
Using Miri to Check Unsafe Code
When writing unsafe code, you might want to check that what you have written actually is safe and correct. One of the best ways to do that is to use Miri, an official Rust tool for detecting undefined behavior. Whereas the borrow checker is a static tool that works at compile time, Miri is a dynamic tool that works at runtime. It checks your code by running your program, or its test suite, and detecting when you violate the rules it understands about how Rust should work.
Using Miri requires a nightly build of Rust (which we talk about more in Appendix G: How Rust is Made and “Nightly Rust”). You can install both a nightly version of Rust and the Miri tool by typing rustup +nightly component add miri
. This does not change what version of Rust your project uses; it only adds the tool to your system so you can use it when you want to. You can run Miri on a project by typing cargo +nightly miri run
or cargo +nightly miri test
.
For an example of how helpful this can be, consider what happens when we run it against the Listing before the previous one.
$ cargo +nightly miri run
Compiling unsafe-example v0.1.0 (file:///projects/unsafe-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
Running `file:///home/.rustup/toolchains/nightly/bin/cargo-miri runner target/miri/debug/unsafe-example`
COUNTER: 3
Miri correctly warns us that we have shared references to mutable data. Here, Miri issues only a warning because this is not guaranteed to be undefined behavior in this case, and it does not tell us how to fix the problem. but at least we know there is a risk of undefined behavior and can think about how to make the code safe. In some cases, Miri can also detect outright errors - code patterns that are sure to be wrong - and make recommendations about how to fix those errors.
Miri doesn’t catch everything you might get wrong when writing unsafe code. Miri is a dynamic analysis tool, so it only catches problems with code that actually gets run. That means you will need to use it in conjunction with good testing techniques to increase your confidence about the unsafe code you have written. Miri also does not cover every possible way your code can be unsound.
Put another way: If Miri does catch a problem, you know there’s a bug, but just because Miri doesn’t catch a bug doesn’t mean there isn’t a problem. It can catch a lot, though. Try running it on the other examples of unsafe code in this chapter and see what it says!
You can learn more about Miri at its GitHub repository.
When to Use Unsafe Code
Using unsafe
to use one of the five superpowers just discussed isn’t wrong or even frowned upon, but it is trickier to get unsafe
code correct because the compiler can’t help uphold memory safety. When you have a reason to use unsafe
code, you can do so, and having the explicit unsafe
annotation makes it easier to track down the source of problems when they occur. Whenever you write unsafe code, you can use Miri to help you be more confident that the code you have written upholds Rust’s rules.
For a much deeper exploration of how to work effectively with unsafe Rust, read Rust’s official guide to the subject, the Rustonomicon.
Advanced Traits
Associated Types
Associated types connect a type placeholder with a trait such that the trait method definitions can use these placeholder types in their signatures. The implementor of a trait will specify the concrete type to be used instead of the placeholder type for the particular implementation. That way, we can define a trait that uses some types without needing to know exactly what those types are until the trait is implemented.
One example of a trait with an associated type is the Iterator
trait that the standard library provides. The associated type is named Item
and stands in for the type of the values the type implementing the Iterator
trait is iterating over. The definition of the Iterator
trait is as shown in the following Listing.
pub trait Iterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
}
The type Item
is a placeholder, and the next
method’s definition shows that it will return values of type Option<Self::Item>
. Implementors of the Iterator
trait will specify the concrete type for Item
, and the next
method will return an Option
containing a value of that concrete type.
Associated types might seem like a similar concept to generics, in that the latter allow us to define a function without specifying what types it can handle. To examine the difference between the two concepts, we’ll look at an implementation of the Iterator
trait on a type named Counter
that specifies the Item
type is u32
:
Filename: src/lib.rs
impl Iterator for Counter {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
This syntax seems comparable to that of generics. So why not just define the Iterator
trait with generics, as shown in the following Listing?
pub trait Iterator<T> {
fn next(&mut self) -> Option<T>;
}
The difference is that when using generics, we must annotate the types in each implementation; because we can also implement Iterator<String> for Counter
or any other type, we could have multiple implementations of Iterator
for Counter
. In other words, when a trait has a generic parameter, it can be implemented for a type multiple times, changing the concrete types of the generic type parameters each time. When we use the next
method on Counter
, we would have to provide type annotations to indicate which implementation of Iterator
we want to use.
With associated types, we don’t need to annotate types because we can’t implement a trait on a type multiple times. In the previous Listing with the definition that uses associated types, we can choose what the type of Item
will be only once, because there can be only one impl Iterator for Counter
. We don’t have to specify that we want an iterator of u32
values everywhere that we call next
on Counter
.
Associated types also become part of the trait’s contract: implementors of the trait must provide a type to stand in for the associated type placeholder. Associated types often have a name that describes how the type will be used, and documenting the associated type in the API documentation is a good practice.
Default Generic Type Parameters and Operator Overloading
When we use generic type parameters, we can specify a default concrete type for the generic type. This eliminates the need for implementors of the trait to specify a concrete type if the default type works. You specify a default type when declaring a generic type with the <PlaceholderType=ConcreteType>
syntax.
A great example of a situation where this technique is useful is with operator overloading
, in which you customize the behavior of an operator (such as +
) in particular situations.
Rust doesn’t allow you to create your own operators or overload arbitrary operators. But you can overload the operations and corresponding traits listed in std::ops
by implementing the traits associated with the operator. For example, in the following Listing we overload the +
operator to add two Point
instances together. We do this by implementing the Add
trait on a Point
struct.
Filename: src/main.rs
use std::ops::Add;
#[derive(Debug, Copy, Clone, PartialEq)]
struct Point {
x: i32,
y: i32,
}
impl Add for Point {
type Output = Point;
fn add(self, other: Point) -> Point {
Point {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
fn main() {
assert_eq!(
Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
Point { x: 3, y: 3 }
);
}
The add
method adds the x
values of two Point
instances and the y
values of two Point
instances to create a new Point
. The Add
trait has an associated type named Output
that determines the type returned from the add
method.
The default generic type in this code is within the Add
trait. Here is its definition:
trait Add<Rhs=Self> {
type Output;
fn add(self, rhs: Rhs) -> Self::Output;
}
This code should look generally familiar: a trait with one method and an associated type. The new part is Rhs=Self
: this syntax is called default type parameters. The Rhs
generic type parameter (short for “right-hand side”) defines the type of the rhs
parameter in the add
method. If we don’t specify a concrete type for Rhs
when we implement the Add
trait, the type of Rhs
will default to Self
, which will be the type we’re implementing Add
on.
When we implemented Add
for Point
, we used the default for Rhs
because we wanted to add two Point
instances. Let’s look at an example of implementing the Add
trait where we want to customize the Rhs
type rather than using the default.
We have two structs, Millimeters
and Meters
, holding values in different units. This thin wrapping of an existing type in another struct is known as the newtype pattern, which we describe in more detail in the “Using the Newtype Pattern to Implement External Traits on External Types” section. We want to add values in millimeters to values in meters and have the implementation of Add
do the conversion correctly. We can implement Add
for Millimeters
with Meters
as the Rhs
, as shown in the following Listing.
Filename: src/lib.rs
use std::ops::Add;
struct Millimeters(u32);
struct Meters(u32);
impl Add<Meters> for Millimeters {
type Output = Millimeters;
fn add(self, other: Meters) -> Millimeters {
Millimeters(self.0 + (other.0 * 1000))
}
}
To add Millimeters
and Meters
, we specify impl Add<Meters>
to set the value of the Rhs
type parameter instead of using the default of Self
.
You’ll use default type parameters in two main ways:
- To extend a type without breaking existing code
- To allow customization in specific cases most users won’t need
The standard library’s Add
trait is an example of the second purpose: usually, you’ll add two like types, but the Add
trait provides the ability to customize beyond that. Using a default type parameter in the Add
trait definition means you don’t have to specify the extra parameter most of the time. In other words, a bit of implementation boilerplate isn’t needed, making it easier to use the trait.
The first purpose is similar to the second but in reverse: if you want to add a type parameter to an existing trait, you can give it a default to allow extension of the functionality of the trait without breaking the existing implementation code.
Disambiguating Between Methods with the Same Name
Nothing in Rust prevents a trait from having a method with the same name as another trait’s method, nor does Rust prevent you from implementing both traits on one type. It’s also possible to implement a method directly on the type with the same name as methods from traits.
When calling methods with the same name, you’ll need to tell Rust which one you want to use. Consider the code in the following Listing where we’ve defined two traits, Pilot
and Wizard
, that both have a method called fly
. We then implement both traits on a type Human
that already has a method named fly
implemented on it. Each fly
method does something different.
Filename: src/main.rs
trait Pilot {
fn fly(&self);
}
trait Wizard {
fn fly(&self);
}
struct Human;
impl Pilot for Human {
fn fly(&self) {
println!("This is your captain speaking.");
}
}
impl Wizard for Human {
fn fly(&self) {
println!("Up!");
}
}
impl Human {
fn fly(&self) {
println!("*waving arms furiously*");
}
}
When we call fly
on an instance of Human
, the compiler defaults to calling the method that is directly implemented on the type, as shown in the following Listing.
Filename: src/main.rs
fn main() {
let person = Human;
person.fly();
}
Running this code will print *waving arms furiously*
, showing that Rust called the fly
method implemented on Human directly.
To call the fly
methods from either the Pilot
trait or the Wizard
trait, we need to use more explicit syntax to specify which fly
method we mean. The following Listing demonstrates this syntax.
Filename: src/main.rs
fn main() {
let person = Human;
Pilot::fly(&person);
Wizard::fly(&person);
person.fly();
}
Specifying the trait name before the method name clarifies to Rust which implementation of fly
we want to call. We could also write Human::fly(&person)
, which is equivalent to the person.fly()
that we used in the Listing, but this is a bit longer to write if we don’t need to disambiguate.
Running this code prints the following:
$ cargo run
Compiling traits-example v0.1.0 (file:///projects/traits-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.46s
Running `target/debug/traits-example`
This is your captain speaking.
Up!
*waving arms furiously*
Because the fly
method takes a self
parameter, if we had two types that both implement one trait, Rust could figure out which implementation of a trait to use based on the type of self
.
However, associated functions that are not methods don’t have a self
parameter. When there are multiple types or traits that define non-method functions with the same function name, Rust doesn’t always know which type you mean unless you use fully qualified syntax. For example, in the following Listing we create a trait for an animal shelter that wants to name all baby dogs Spot. We make an Animal
trait with an associated non-method function baby_name
. The Animal
trait is implemented for the struct Dog
, on which we also provide an associated non-method function baby_name
directly.
Filename: src/main.rs
trait Animal {
fn baby_name() -> String;
}
struct Dog;
impl Dog {
fn baby_name() -> String {
String::from("Spot")
}
}
impl Animal for Dog {
fn baby_name() -> String {
String::from("puppy")
}
}
fn main() {
println!("A baby dog is called a {}", Dog::baby_name());
}
We implement the code for naming all puppies Spot in the baby_name
associated function that is defined on Dog
. The Dog
type also implements the trait Animal
, which describes characteristics that all animals have. Baby dogs are called puppies, and that is expressed in the implementation of the Animal
trait on Dog
in the baby_name
function associated with the Animal
trait.
In main
, we call the Dog::baby_name
function, which calls the associated function defined on Dog
directly. This code prints the following:
$ cargo run
Compiling traits-example v0.1.0 (file:///projects/traits-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.54s
Running `target/debug/traits-example`
A baby dog is called a Spot
This output isn’t what we wanted. We want to call the baby_name
function that is part of the Animal
trait that we implemented on Dog
so the code prints A baby dog is called a puppy
. The technique of specifying the trait name that we used in the previous Listing doesn’t help here; if we change main to the code in the following Listing, we’ll get a compilation error.
Filename: src/main.rs
fn main() {
println!("A baby dog is called a {}", Animal::baby_name());
}
Because Animal::baby_name
doesn’t have a self
parameter, and there could be other types that implement the Animal
trait, Rust can’t figure out which implementation of Animal::baby_name
we want. We’ll get this compiler error:
$ cargo run
Compiling traits-example v0.1.0 (file:///projects/traits-example)
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
--> src/main.rs:20:43
|
2 | fn baby_name() -> String;
| ------------------------- `Animal::baby_name` defined here
...
20 | println!("A baby dog is called a {}", Animal::baby_name());
| ^^^^^^^^^^^^^^^^^^^ cannot call associated function of trait
|
help: use the fully-qualified path to the only available implementation
|
20 | println!("A baby dog is called a {}", <Dog as Animal>::baby_name());
| +++++++ +
For more information about this error, try `rustc --explain E0790`.
error: could not compile `traits-example` (bin "traits-example") due to 1 previous error
To disambiguate and tell Rust that we want to use the implementation of Animal
for Dog
as opposed to the implementation of Animal
for some other type, we need to use fully qualified syntax. The following Listing demonstrates how to use fully qualified syntax.
Filename: src/main.rs
fn main() {
println!("A baby dog is called a {}", <Dog as Animal>::baby_name());
}
We’re providing Rust with a type annotation within the angle brackets, which indicates we want to call the baby_name
method from the Animal
trait as implemented on Dog
by saying that we want to treat the Dog
type as an Animal
for this function call. This code will now print what we want:
$ cargo run
Compiling traits-example v0.1.0 (file:///projects/traits-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.48s
Running `target/debug/traits-example`
A baby dog is called a puppy
In general, fully qualified syntax is defined as follows:
<Type as Trait>::function(receiver_if_method, next_arg, ...);
For associated functions that aren’t methods, there would not be a receiver
: there would only be the list of other arguments. You could use fully qualified syntax everywhere that you call functions or methods. However, you’re allowed to omit any part of this syntax that Rust can figure out from other information in the program. You only need to use this more verbose syntax in cases where there are multiple implementations that use the same name and Rust needs help to identify which implementation you want to call.
Using Supertraits
Sometimes you might write a trait definition that depends on another trait: for a type to implement the first trait, you want to require that type to also implement the second trait. You would do this so that your trait definition can make use of the associated items of the second trait. The trait your trait definition is relying on is called a supertrait of your trait.
For example, let’s say we want to make an OutlinePrint
trait with an outline_print
method that will print a given value formatted so that it’s framed in asterisks. That is, given a Point
struct that implements the standard library trait Display
to result in (x, y)
, when we call outline_print
on a Point
instance that has 1
for x
and 3
for y
, it should print the following:
**********
* *
* (1, 3) *
* *
**********
In the implementation of the outline_print
method, we want to use the Display
trait’s functionality. Therefore, we need to specify that the OutlinePrint
trait will work only for types that also implement Display
and provide the functionality that OutlinePrint
needs. We can do that in the trait definition by specifying OutlinePrint: Display
. This technique is similar to adding a trait bound to the trait. The following Listing shows an implementation of the OutlinePrint
trait.
Filename: src/main.rs
use std::fmt;
trait OutlinePrint: fmt::Display {
fn outline_print(&self) {
let output = self.to_string();
let len = output.len();
println!("{}", "*".repeat(len + 4));
println!("*{}*", " ".repeat(len + 2));
println!("* {output} *");
println!("*{}*", " ".repeat(len + 2));
println!("{}", "*".repeat(len + 4));
}
}
Because we’ve specified that OutlinePrint
requires the Display
trait, we can use the to_string
function that is automatically implemented for any type that implements Display
. If we tried to use to_string
without adding a colon and specifying the Display
trait after the trait name, we’d get an error saying that no method named to_string
was found for the type &Self
in the current scope.
Let’s see what happens when we try to implement OutlinePrint
on a type that doesn’t implement Display
, such as the Point
struct:
Filename: src/main.rs
struct Point {
x: i32,
y: i32,
}
impl OutlinePrint for Point {}
We get an error saying that Display
is required but not implemented:
$ cargo run
Compiling traits-example v0.1.0 (file:///projects/traits-example)
error[E0277]: `Point` doesn't implement `std::fmt::Display`
--> src/main.rs:20:23
|
20 | impl OutlinePrint for Point {}
| ^^^^^ `Point` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Point`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
note: required by a bound in `OutlinePrint`
--> src/main.rs:3:21
|
3 | trait OutlinePrint: fmt::Display {
| ^^^^^^^^^^^^ required by this bound in `OutlinePrint`
error[E0277]: `Point` doesn't implement `std::fmt::Display`
--> src/main.rs:24:7
|
24 | p.outline_print();
| ^^^^^^^^^^^^^ `Point` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Point`
= note: in format strings you may be able to use `{:?}` (or {:
note: required by a bound in `OutlinePrint::outline_print`
--> src/main.rs:3:21
|
3 | trait OutlinePrint: fmt::Display {
| ^^^^^^^^^^^^ required by this bound in `OutlinePrint::outline_print`
4 | fn outline_print(&self) {
| ------------- required by a bound in this associated function
For more information about this error, try `rustc --explain E0277`.
error: could not compile `traits-example` (bin "traits-example") due to 2 previous errors
To fix this, we implement Display
on Point
and satisfy the constraint that OutlinePrint
requires, like so:
Filename: src/main.rs
use std::fmt;
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}
Then, implementing the OutlinePrint
trait on Point
will compile successfully, and we can call outline_print
on a Point
instance to display it within an outline of asterisks.
Using the Newtype Pattern to Implement External Traits on External Types
In the previous Chapter, we mentioned the orphan rule that states we’re only allowed to implement a trait on a type if either the trait or the type, or both, are local to our crate. It’s possible to get around this restriction using the newtype pattern, which involves creating a new type in a tuple struct. The tuple struct will have one field and be a thin wrapper around the type for which we want to implement a trait. Then the wrapper type is local to our crate, and we can implement the trait on the wrapper. Newtype is a term that originates from the Haskell programming language. There is no runtime performance penalty for using this pattern, and the wrapper type is elided at compile time.
As an example, let’s say we want to implement Display
on Vec<T>
, which the orphan rule prevents us from doing directly because the Display
trait and the Vec<T>
type are defined outside our crate. We can make a Wrapper
struct that holds an instance of Vec<T>
; then we can implement Display
on Wrapper
and use the Vec<T>
value, as shown in the following Listing.
Filename: src/main.rs
use std::fmt;
struct Wrapper(Vec<String>);
impl fmt::Display for Wrapper {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}]", self.0.join(", "))
}
}
fn main() {
let w = Wrapper(vec![String::from("hello"), String::from("world")]);
println!("w = {w}");
}
The downside of using this technique is that Wrapper
is a new type, so it doesn’t have the methods of the value it’s holding. We would have to implement all the methods of Vec<T>
directly on Wrapper
such that the methods delegate to self.0
, which would allow us to treat Wrapper
exactly like a Vec<T>
. If we wanted the new type to have every method the inner type has, implementing the Deref
trait on the Wrapper
to return the inner type would be a solution. If we didn’t want the Wrapper
type to have all the methods of the inner type - for example, to restrict the Wrapper
type’s behavior - we would have to implement just the methods we do want manually.
This newtype pattern is also useful even when traits are not involved. Let’s switch focus and look at some advanced ways to interact with Rust’s type system.
Advanced Types
Using the Newtype Pattern for Type Safety and Abstraction
The newtype pattern is also useful for tasks beyond those we’ve discussed so far, including statically enforcing that values are never confused and indicating the units of a value.
We can also use the newtype pattern to abstract away some implementation details of a type: the new type can expose a public API that is different from the API of the private inner type.
The newtype pattern is a lightweight way to achieve encapsulation to hide implementation details.
Creating Type Synonyms with Type Aliases
Rust provides the ability to declare a type alias to give an existing type another name. For this we use the type
keyword. For example, we can create the alias Kilometers
to i32
like so:
type Kilometers = i32;
Now, the alias Kilometers
is a synonym for i32
; unlike the Millimeters
and Meters
types we created in the previous Listing, Kilometers
is not a separate, new type. Values that have the type Kilometers
will be treated the same as values of type i32
:
type Kilometers = i32;
let x: i32 = 5;
let y: Kilometers = 5;
println!("x + y = {}", x + y);
Because Kilometers
and i32
are the same type, we can add values of both types and we can pass Kilometers
values to functions that take i32
parameters. However, using this method, we don’t get the type-checking benefits that we get from the newtype pattern discussed earlier. In other words, if we mix up Kilometers
and i32
values somewhere, the compiler will not give us an error.
The main use case for type synonyms is to reduce repetition. For example, we might have a lengthy type like this:
Box<dyn Fn() + Send + 'static>
Writing this lengthy type in function signatures and as type annotations all over the code can be tiresome and error prone. Imagine having a project full of code like that in the following Listing.
let f: Box<dyn Fn() + Send + 'static> = Box::new(|| println!("hi"));
fn takes_long_type(f: Box<dyn Fn() + Send + 'static>) {
}
fn returns_long_type() -> Box<dyn Fn() + Send + 'static> {
}
A type alias makes this code more manageable by reducing the repetition. In the following Listing, we’ve introduced an alias named Thunk
for the verbose type and can replace all uses of the type with the shorter alias Thunk
.
type Thunk = Box<dyn Fn() + Send + 'static>;
let f: Thunk = Box::new(|| println!("hi"));
fn takes_long_type(f: Thunk) {
}
fn returns_long_type() -> Thunk {
}
This code is much easier to read and write! Choosing a meaningful name for a type alias can help communicate your intent as well (thunk is a word for code to be evaluated at a later time, so it’s an appropriate name for a closure that gets stored).
Type aliases are also commonly used with the Result<T, E>
type for reducing repetition. Consider the std::io
module in the standard library. I/O operations often return a Result<T, E>
to handle situations when operations fail to work. This library has a std::io::Error
struct that represents all possible I/O errors. Many of the functions in std::io
will be returning Result<T, E>
where the E
is std::io::Error
, such as these functions in the Write
trait:
use std::fmt;
use std::io::Error;
pub trait Write {
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>;
fn flush(&mut self) -> Result<(), Error>;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>;
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Error>;
}
The Result<..., Error>
is repeated a lot. As such, std::io
has this type alias declaration:
type Result<T> = std::result::Result<T, std::io::Error>;
Because this declaration is in the std::io
module, we can use the fully qualified alias std::io::Result<T>
; that is, a Result<T, E>
with the E
filled in as std::io::Error
. The Write
trait function signatures end up looking like this:
pub trait Write {
fn write(&mut self, buf: &[u8]) -> Result<usize>;
fn flush(&mut self) -> Result<()>;
fn write_all(&mut self, buf: &[u8]) -> Result<()>;
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()>;
}
The type alias helps in two ways: it makes code easier to write and it gives us a consistent interface across all of std::io
. Because it’s an alias, it’s just another Result<T, E>
, which means we can use any methods that work on Result<T, E>
with it, as well as special syntax like the ?
operator.
The Never Type That Never Returns
Rust has a special type named !
that’s known in type theory lingo as the empty type because it has no values. We prefer to call it the never type because it stands in the place of the return type when a function will never return. Here is an example:
fn bar() -> ! {
}
This code is read as “the function bar
returns never.” Functions that return never are called diverging functions. We can’t create values of the type !
so bar
can never possibly return.
But what use is a type you can never create values for? Recall the code from the previous Listing, part of the number-guessing game; we’ve reproduced a bit of it here in the following Listing.
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
At the time, we skipped over some details in this code. We discussed that match
arms must all return the same type. So, for example, the following code doesn’t work:
let guess = match guess.trim().parse() {
Ok(_) => 5,
Err(_) => "hello",
};
The type of guess
in this code would have to be an integer and a string, and Rust requires that guess
have only one type. So what does continue
return? How were we allowed to return a u32
from one arm and have another arm that ends with continue
in the previous Listing?
As you might have guessed, continue
has a !
value. That is, when Rust computes the type of guess
, it looks at both match arms, the former with a value of u32
and the latter with a !
value. Because !
can never have a value, Rust decides that the type of guess
is u32
.
The formal way of describing this behavior is that expressions of type !
can be coerced into any other type. We’re allowed to end this match
arm with continue
because continue
doesn’t return a value; instead, it moves control back to the top of the loop, so in the Err
case, we never assign a value to guess
.
The never type is useful with the panic!
macro as well. Recall the unwrap
function that we call on Option<T>
values to produce a value or panic with this definition:
impl<T> Option<T> {
pub fn unwrap(self) -> T {
match self {
Some(val) => val,
None => panic!("called `Option::unwrap()` on a `None` value"),
}
}
}
In this code, the same thing happens as in the match
in the previous Listing: Rust sees that val
has the type T
and panic!
has the type !
, so the result of the overall match
expression is T
. This code works because panic!
doesn’t produce a value; it ends the program. In the None
case, we won’t be returning a value from unwrap
, so this code is valid.
One final expression that has the type !
is a loop
:
print!("forever ");
loop {
print!("and ever ");
}
Here, the loop never ends, so !
is the value of the expression. However, this wouldn’t be true if we included a break
, because the loop would terminate when it got to the break
.
Dynamically Sized Types and the Sized Trait
Rust needs to know certain details about its types, such as how much space to allocate for a value of a particular type. This leaves one corner of its type system a little confusing at first: the concept of dynamically sized types. Sometimes referred to as DSTs or unsized types, these types let us write code using values whose size we can know only at runtime.
Let’s dig into the details of a dynamically sized type called str
, which we’ve been using throughout the book. That’s right, not &str
, but str
on its own, is a DST. We can’t know how long the string is until runtime, meaning we can’t create a variable of type str, nor can we take an argument of type str
. Consider the following code, which does not work:
let s1: str = "Hello there!";
let s2: str = "How's it going?";
Rust needs to know how much memory to allocate for any value of a particular type, and all values of a type must use the same amount of memory. If Rust allowed us to write this code, these two str
values would need to take up the same amount of space. But they have different lengths: s1
needs 12 bytes of storage and s2
needs 15. This is why it’s not possible to create a variable holding a dynamically sized type.
So what do we do? In this case, you already know the answer: we make the types of s1
and s2
a &str
rather than a str
. Recall from “String Slices” in the previous Chapter that the slice data structure just stores the starting position and the length of the slice. So although a &T
is a single value that stores the memory address of where the T
is located, a &str
is two values: the address of the str
and its length. As such, we can know the size of a &str
value at compile time: it’s twice the length of a usize
. That is, we always know the size of a &str
, no matter how long the string it refers to is. In general, this is the way in which dynamically sized types are used in Rust: they have an extra bit of metadata that stores the size of the dynamic information. The golden rule of dynamically sized types is that we must always put values of dynamically sized types behind a pointer of some kind.
We can combine str
with all kinds of pointers: for example, Box<str>
or Rc<str>
. In fact, you’ve seen this before but with a different dynamically sized type: traits. Every trait is a dynamically sized type we can refer to by using the name of the trait. We mentioned that to use traits as trait objects, we must put them behind a pointer, such as &dyn Trait
or Box<dyn Trait>
(Rc<dyn Trait>
would work too).
To work with DSTs, Rust provides the Sized
trait to determine whether or not a type’s size is known at compile time. This trait is automatically implemented for everything whose size is known at compile time. In addition, Rust implicitly adds a bound on Sized
to every generic function. That is, a generic function definition like this:
fn generic<T>(t: T) {
}
is actually treated as though we had written this:
fn generic<T: Sized>(t: T) {
}
By default, generic functions will work only on types that have a known size at compile time. However, you can use the following special syntax to relax this restriction:
fn generic<T: ?Sized>(t: &T) {
}
A trait bound on ?Sized
means “T
may or may not be Sized
“ and this notation overrides the default that generic types must have a known size at compile time. The ?Trait
syntax with this meaning is only available for Sized
, not any other traits.
Also note that we switched the type of the t
parameter from T
to &T
. Because the type might not be Sized
, we need to use it behind some kind of pointer. In this case, we’ve chosen a reference.
Advanced Functions and Closures
Function Pointers
We’ve talked about how to pass closures to functions; you can also pass regular functions to functions! This technique is useful when you want to pass a function you’ve already defined rather than defining a new closure. Functions coerce to the type fn
(with a lowercase f), not to be confused with the Fn
closure trait. The fn
type is called a function pointer. Passing functions with function pointers will allow you to use functions as arguments to other functions.
The syntax for specifying that a parameter is a function pointer is similar to that of closures, as shown in the following Listing, where we’ve defined a function add_one
that adds 1 to its parameter. The function do_twice
takes two parameters: a function pointer to any function that takes an i32
parameter and returns an i32
, and one i32
value. The do_twice
function calls the function f
twice, passing it the arg
value, then adds the two function call results together. The main
function calls do_twice
with the arguments add_one
and 5
.
Filename: src/main.rs
fn add_one(x: i32) -> i32 {
x + 1
}
fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 {
f(arg) + f(arg)
}
fn main() {
let answer = do_twice(add_one, 5);
println!("The answer is: {answer}");
}
This code prints The answer is: 12
.
Unlike closures, fn
is a type rather than a trait, so we specify fn
as the parameter type directly rather than declaring a generic type parameter with one of the Fn
traits as a trait bound.
Function pointers implement all three of the closure traits (Fn
, FnMut
, and FnOnce
), meaning you can always pass a function pointer as an argument for a function that expects a closure. It’s best to write functions using a generic type and one of the closure traits so your functions can accept either functions or closures.
That said, one example of where you would want to only accept fn
and not closures is when interfacing with external code that doesn’t have closures: C functions can accept functions as arguments, but C doesn’t have closures.
As an example of where you could use either a closure defined inline or a named function, let’s look at a use of the map
method provided by the Iterator
trait in the standard library. To use the map
method to turn a vector of numbers into a vector of strings, we could use a closure, as in the following Listing.
let list_of_numbers = vec![1, 2, 3];
let list_of_strings: Vec<String> =
list_of_numbers.iter().map(|i| i.to_string()).collect();
Or we could name a function as the argument to map instead of the closure. The following Listing shows what this would look like.
let list_of_numbers = vec![1, 2, 3];
let list_of_strings: Vec<String> =
list_of_numbers.iter().map(ToString::to_string).collect();
Note that we must use the fully qualified syntax that we talked about because there are multiple functions available named to_string
.
Here, we’re using the to_string
function defined in the ToString
trait, which the standard library has implemented for any type that implements Display
.
The name of each enum variant that we define also becomes an initializer function. We can use these initializer functions as function pointers that implement the closure traits, which means we can specify the initializer functions as arguments for methods that take closures, as seen in the following Listing.
enum Status {
Value(u32),
Stop,
}
let list_of_statuses: Vec<Status> = (0u32..20).map(Status::Value).collect();
Here we create Status::Value
instances using each u32
value in the range that map
is called on by using the initializer function of Status::Value
. Some people prefer this style and some people prefer to use closures. They compile to the same code, so use whichever style is clearer to you.
Returning Closures
Closures are represented by traits, which means you can’t return closures directly. In most cases where you might want to return a trait, you can instead use the concrete type that implements the trait as the return value of the function. However, you can’t usually do that with closures because they don’t have a concrete type that is returnable. You’re not allowed to use the function pointer fn
as a return type if the closure captures any values from its scope, for example.
Instead, you will normally use the impl Trait
syntax we learned about. You can return any function type, using Fn
, FnOnce
and FnMut
. For example, the code in the following Listing will work just fine.
fn returns_closure() -> impl Fn(i32) -> i32 {
|x| x + 1
}
However, each closure is also its own distinct type. If you need to work with multiple functions that have the same signature but different implementations, you will need to use a trait object for them. Consider what happens if you write code like that shown in the following Listing.
Filename: src/main.rs
fn main() {
let handlers = vec![returns_closure(), returns_initialized_closure(123)];
for handler in handlers {
let output = handler(5);
println!("{output}");
}
}
fn returns_closure() -> impl Fn(i32) -> i32 {
|x| x + 1
}
fn returns_initialized_closure(init: i32) -> impl Fn(i32) -> i32 {
move |x| x + init
}
Here we have two functions, returns_closure
and returns_initialized_closure
, which both return impl Fn(i32) -> i32
. Notice that the closures that they return are different, even though they implement the same type. If we try to compile this, Rust lets us know that it won’t work:
$ cargo build
Compiling functions-example v0.1.0 (file:///projects/functions-example)
error[E0308]: mismatched types
--> src/main.rs:2:44
|
2 | let handlers = vec![returns_closure(), returns_initialized_closure(123)];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found a different opaque type
...
9 | fn returns_closure() -> impl Fn(i32) -> i32 {
| ------------------- the expected opaque type
...
13 | fn returns_initialized_closure(init: i32) -> impl Fn(i32) -> i32 {
| ------------------- the found opaque type
|
= note: expected opaque type `impl Fn(i32) -> i32` (opaque type at <src/main.rs:9:25>)
found opaque type `impl Fn(i32) -> i32` (opaque type at <src/main.rs:13:46>)
= note: distinct uses of `impl Trait` result in different opaque types
For more information about this error, try `rustc --explain E0308`.
error: could not compile `functions-example` (bin "functions-example") due to 1 previous error
The error message tells us that whenever we return an impl Trait
Rust creates a unique opaque type, a type where we cannot see into the details of what Rust constructs for us. So even though these functions both return closures that implements the same trait, Fn(i32) -> i32
, the opaque types Rust generates for each are distinct. (This is similar to how Rust produces different concrete types for distinct async blocks even when they have the same output type.) We have seen a solution to this problem a few times now: we can use a trait object, as in the following Listing.
fn returns_closure() -> Box<dyn Fn(i32) -> i32> {
Box::new(|x| x + 1)
}
fn returns_initialized_closure(init: i32) -> Box<dyn Fn(i32) -> i32> {
Box::new(move |x| x + init)
}
This code will compile just fine.
Macros
The term macro refers to a family of features in Rust: declarative macros with macro_rules!
and three kinds of procedural macros:
- Custom
#[derive]
macros that specify code added with the derive
attribute used on structs and enums
- Attribute-like macros that define custom attributes usable on any item
- Function-like macros that look like function calls but operate on the tokens specified as their argument
The Difference Between Macros and Functions
Fundamentally, macros are a way of writing code that writes other code, which is known as metaprogramming. In Appendix C, we discuss the derive
attribute, which generates an implementation of various traits for you. We’ve also used the println!
and vec!
macros throughout the book. All of these macros expand to produce more code than the code you’ve written manually.
Metaprogramming is useful for reducing the amount of code you have to write and maintain, which is also one of the roles of functions. However, macros have some additional powers that functions don’t have.
A function signature must declare the number and type of parameters the function has. Macros, on the other hand, can take a variable number of parameters: we can call println!("hello")
with one argument or println!("hello {}", name)
with two arguments. Also, macros are expanded before the compiler interprets the meaning of the code, so a macro can, for example, implement a trait on a given type. A function can’t, because it gets called at runtime and a trait needs to be implemented at compile time.
The downside to implementing a macro instead of a function is that macro definitions are more complex than function definitions because you’re writing Rust code that writes Rust code. Due to this indirection, macro definitions are generally more difficult to read, understand, and maintain than function definitions.
Another important difference between macros and functions is that you must define macros or bring them into scope before you call them in a file, as opposed to functions you can define anywhere and call anywhere.
The most widely used form of macros in Rust is the declarative macro. These are also sometimes referred to as “macros by example,” “macro_rules!
macros,” or just plain “macros.” At their core, declarative macros allow you to write something similar to a Rust match
expression. match
expressions are control structures that take an expression, compare the resultant value of the expression to patterns, and then run the code associated with the matching pattern. Macros also compare a value to patterns that are associated with particular code: in this situation, the value is the literal Rust source code passed to the macro; the patterns are compared with the structure of that source code; and the code associated with each pattern, when matched, replaces the code passed to the macro. This all happens during compilation.
To define a macro, you use the macro_rules!
construct. Let’s explore how to use macro_rules!
by looking at how the vec!
macro is defined. The previous chapter covered how we can use the vec!
macro to create a new vector with particular values. For example, the following macro creates a new vector containing three integers:
let v: Vec<u32> = vec![1, 2, 3];
We could also use the vec!
macro to make a vector of two integers or a vector of five string slices. We wouldn’t be able to use a function to do the same because we wouldn’t know the number or type of values up front.
The following Listing shows a slightly simplified definition of the vec!
macro.
Filename: src/lib.rs
#[macro_export]
macro_rules! vec {
( $( $x:expr ),* ) => {
{
let mut temp_vec = Vec::new();
$(
temp_vec.push($x);
)*
temp_vec
}
};
}
Note: The actual definition of the vec!
macro in the standard library includes code to pre-allocate the correct amount of memory up front. That code is an optimization that we don’t include here, to make the example simpler.
The #[macro_export]
annotation indicates that this macro should be made available whenever the crate in which the macro is defined is brought into scope. Without this annotation, the macro can’t be brought into scope.
We then start the macro definition with macro_rules!
and the name of the macro we’re defining without the exclamation mark. The name, in this case vec, is followed by curly brackets denoting the body of the macro definition.
The structure in the vec!
body is similar to the structure of a match
expression. Here we have one arm with the pattern ( $( $x:expr ),* )
, followed by =>
and the block of code associated with this pattern. If the pattern matches, the associated block of code will be emitted. Given that this is the only pattern in this macro, there is only one valid way to match; any other pattern will result in an error. More complex macros will have more than one arm.
Valid pattern syntax in macro definitions is different from the pattern syntax covered in the previous Chapter because macro patterns are matched against Rust code structure rather than values. Let’s walk through what the pattern pieces in the Listing mean; for the full macro pattern syntax, see the Rust Reference.
First we use a set of parentheses to encompass the whole pattern. We use a dollar sign ($
) to declare a variable in the macro system that will contain the Rust code matching the pattern. The dollar sign makes it clear this is a macro variable as opposed to a regular Rust variable. Next comes a set of parentheses that captures values that match the pattern within the parentheses for use in the replacement code. Within $()
is $x:expr
, which matches any Rust expression and gives the expression the name $x
.
The comma following $()
indicates that a literal comma separator character must appear between each instance of the code that matches the code within $()
. The *
specifies that the pattern matches zero or more of whatever precedes the *
.
When we call this macro with vec![1, 2, 3];
, the $x
pattern matches three times with the three expressions 1
, 2
, and 3
.
Now let’s look at the pattern in the body of the code associated with this arm: temp_vec.push()
within $()*
is generated for each part that matches $()
in the pattern zero or more times depending on how many times the pattern matches. The $x
is replaced with each expression matched. When we call this macro with vec![1, 2, 3];
, the code generated that replaces this macro call will be the following:
{
let mut temp_vec = Vec::new();
temp_vec.push(1);
temp_vec.push(2);
temp_vec.push(3);
temp_vec
}
We’ve defined a macro that can take any number of arguments of any type and can generate code to create a vector containing the specified elements.
Procedural Macros for Generating Code from Attributes
The second form of macros is the procedural macro, which acts more like a function (and is a type of procedure). Procedural macros accept some code as an input, operate on that code, and produce some code as an output rather than matching against patterns and replacing the code with other code as declarative macros do. The three kinds of procedural macros are custom derive
, attribute-like, and function-like, and all work in a similar fashion.
When creating procedural macros, the definitions must reside in their own crate with a special crate type. This is for complex technical reasons that we hope to eliminate in the future. In the following Listing, we show how to define a procedural macro, where some_attribute
is a placeholder for using a specific macro variety.
Filename: src/lib.rs
use proc_macro;
#[some_attribute]
pub fn some_name(input: TokenStream) -> TokenStream {
}
The function that defines a procedural macro takes a TokenStream
as an input and produces a TokenStream
as an output. The TokenStream
type is defined by the proc_macro
crate that is included with Rust and represents a sequence of tokens. This is the core of the macro: the source code that the macro is operating on makes up the input TokenStream
, and the code the macro produces is the output TokenStream
. The function also has an attribute attached to it that specifies which kind of procedural macro we’re creating. We can have multiple kinds of procedural macros in the same crate.
Let’s look at the different kinds of procedural macros. We’ll start with a custom derive
macro and then explain the small dissimilarities that make the other forms different.
How to Write a Custom derive Macro
Let’s create a crate named hello_macro
that defines a trait named HelloMacro
with one associated function named hello_macro
. Rather than making our users implement the HelloMacro
trait for each of their types, we’ll provide a procedural macro so users can annotate their type with #[derive(HelloMacro)]
to get a default implementation of the hello_macro
function. The default implementation will print Hello, Macro! My name is TypeName!
where TypeName
is the name of the type on which this trait has been defined. In other words, we’ll write a crate that enables another programmer to write code like the following Listing using our crate.
Filename: src/main.rs
use hello_macro::HelloMacro;
use hello_macro_derive::HelloMacro;
#[derive(HelloMacro)]
struct Pancakes;
fn main() {
Pancakes::hello_macro();
}
This code will print Hello, Macro! My name is Pancakes!
when we’re done. The first step is to make a new library crate, like this:
cargo new hello_macro --lib
Next, we’ll define the HelloMacro
trait and its associated function:
Filename: src/lib.rs
pub trait HelloMacro {
fn hello_macro();
}
We have a trait and its function. At this point, our crate user could implement the trait to achieve the desired functionality, as in the following Listing.
Filename: src/main.rs
use hello_macro::HelloMacro;
struct Pancakes;
impl HelloMacro for Pancakes {
fn hello_macro() {
println!("Hello, Macro! My name is Pancakes!");
}
}
fn main() {
Pancakes::hello_macro();
}
However, they would need to write the implementation block for each type they wanted to use with hello_macro
; we want to spare them from having to do this work.
Additionally, we can’t yet provide the hello_macro
function with default implementation that will print the name of the type the trait is implemented on: Rust doesn’t have reflection capabilities, so it can’t look up the type’s name at runtime. We need a macro to generate code at compile time.
The next step is to define the procedural macro. At the time of this writing, procedural macros need to be in their own crate. Eventually, this restriction might be lifted. The convention for structuring crates and macro crates is as follows: for a crate named foo
, a custom derive
procedural macro crate is called foo_derive
. Let’s start a new crate called hello_macro_derive
inside our hello_macro
project:
cargo new hello_macro_derive --lib
Our two crates are tightly related, so we create the procedural macro crate within the directory of our hello_macro
crate. If we change the trait definition in hello_macro
, we’ll have to change the implementation of the procedural macro in hello_macro_derive
as well. The two crates will need to be published separately, and programmers using these crates will need to add both as dependencies and bring them both into scope. We could instead have the hello_macro
crate use hello_macro_derive
as a dependency and re-export the procedural macro code. However, the way we’ve structured the project makes it possible for programmers to use hello_macro
even if they don’t want the derive
functionality.
We need to declare the hello_macro_derive
crate as a procedural macro crate. We’ll also need functionality from the syn
and quote
crates, as you’ll see in a moment, so we need to add them as dependencies. Add the following to the Cargo.toml file for hello_macro_derive
:
Filename: hello_macro_derive/Cargo.toml
[lib]
proc-macro = true
[dependencies]
syn = "2.0"
quote = "1.0"
To start defining the procedural macro, place the code in the following Listing into your src/lib.rs file for the hello_macro_derive
crate. Note that this code won’t compile until we add a definition for the impl_hello_macro
function.
Filename: hello_macro_derive/src/lib.rs
use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_derive(HelloMacro)]
pub fn hello_macro_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
impl_hello_macro(&ast)
}
Notice that we’ve split the code into the hello_macro_derive
function, which is responsible for parsing the TokenStream
, and the impl_hello_macro
function, which is responsible for transforming the syntax tree: this makes writing a procedural macro more convenient. The code in the outer function (hello_macro_derive
in this case) will be the same for almost every procedural macro crate you see or create. The code you specify in the body of the inner function (impl_hello_macro in this case) will be different depending on your procedural macro’s purpose.
We’ve introduced three new crates: proc_macro
, syn
, and quote
. The proc_macro
crate comes with Rust, so we didn’t need to add that to the dependencies in Cargo.toml. The proc_macro
crate is the compiler’s API that allows us to read and manipulate Rust code from our code.
The syn
crate parses Rust code from a string into a data structure that we can perform operations on. The quote
crate turns syn
data structures back into Rust code. These crates make it much simpler to parse any sort of Rust code we might want to handle: writing a full parser for Rust code is no simple task.
The hello_macro_derive
function will be called when a user of our library specifies #[derive(HelloMacro)]
on a type. This is possible because we’ve annotated the hello_macro_derive
function here with proc_macro_derive
and specified the name HelloMacro
, which matches our trait name; this is the convention most procedural macros follow.
The hello_macro_derive
function first converts the input
from a TokenStream
to a data structure that we can then interpret and perform operations on. This is where syn
comes into play. The parse
function in syn
takes a TokenStream
and returns a DeriveInput
struct representing the parsed Rust code. The following Listing shows the relevant parts of the DeriveInput
struct we get from parsing the struct Pancakes;
string.
DeriveInput {
ident: Ident {
ident: "Pancakes",
span: #0 bytes(95..103)
},
data: Struct(
DataStruct {
struct_token: Struct,
fields: Unit,
semi_token: Some(
Semi
)
}
)
}
The fields of this struct show that the Rust code we’ve parsed is a unit struct with the ident
(identifier, meaning the name) of Pancakes
. There are more fields on this struct for describing all sorts of Rust code; check the syn documentation for DeriveInput
for more information.
Soon we’ll define the impl_hello_macro
function, which is where we’ll build the new Rust code we want to include. But before we do, note that the output for our derive
macro is also a TokenStream
. The returned TokenStream
is added to the code that our crate users write, so when they compile their crate, they’ll get the extra functionality that we provide in the modified TokenStream
.
You might have noticed that we’re calling unwrap
to cause the hello_macro_derive
function to panic if the call to the syn::parse
function fails here. It’s necessary for our procedural macro to panic on errors because proc_macro_derive
functions must return TokenStream
rather than Result
to conform to the procedural macro API. We’ve simplified this example by using unwrap
; in production code, you should provide more specific error messages about what went wrong by using panic!
or expect
.
Now that we have the code to turn the annotated Rust code from a TokenStream
into a DeriveInput
instance, let’s generate the code that implements the HelloMacro
trait on the annotated type, as shown in the following Listing.
Filename: hello_macro_derive/src/lib.rs
fn impl_hello_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let generated = quote! {
impl HelloMacro for #name {
fn hello_macro() {
println!("Hello, Macro! My name is {}!", stringify!(#name));
}
}
};
generated.into()
}
We get an Ident
struct instance containing the name (identifier) of the annotated type using ast.ident
. The struct shows that when we run the impl_hello_macro
function on the code, the ident
we get will have the ident
field with a value of "Pancakes"
. Thus, the name
variable will contain an Ident
struct instance that, when printed, will be the string "Pancakes"
, the name of the struct.
The quote!
macro lets us define the Rust code that we want to return. The compiler expects something different to the direct result of the quote!
macro’s execution, so we need to convert it to a TokenStream
. We do this by calling the into method, which consumes this intermediate representation and returns a value of the required TokenStream
type.
The quote!
macro also provides some very cool templating mechanics: we can enter #name
, and quote!
will replace it with the value in the variable name
. You can even do some repetition similar to the way regular macros work. Check out the quote crate’s docs for a thorough introduction.
The stringify!
macro used here is built into Rust. It takes a Rust expression, such as 1 + 2
, and at compile time turns the expression into a string literal, such as "1 + 2"
. This is different from format!
or println!
, macros which evaluate the expression and then turn the result into a String. There is a possibility that the #name
input might be an expression to print literally, so we use stringify!
. Using stringify!
also saves an allocation by converting #name
to a string literal at compile time.
At this point, cargo build
should complete successfully in both hello_macro
and hello_macro_derive
. Let’s hook up these crates to the code to see the procedural macro in action! Create a new binary project in your projects directory using cargo new pancakes
. We need to add hello_macro
and hello_macro_derive
as dependencies in the pancakes crate’s Cargo.toml. If you’re publishing your versions of hello_macro
and hello_macro_derive
to crates.io, they would be regular dependencies; if not, you can specify them as path dependencies as follows:
hello_macro = { path = "../hello_macro" }
hello_macro_derive = { path = "../hello_macro/hello_macro_derive" }
Put the code in the previous Listing into src/main.rs, and run cargo run
: it should print Hello, Macro! My name is Pancakes!
The implementation of the HelloMacro
trait from the procedural macro was included without the pancakes
crate needing to implement it; the #[derive(HelloMacro)]
added the trait implementation.
Attribute-Like macros
Attribute-like macros are similar to custom derive
macros, but instead of generating code for the derive
attribute, they allow you to create new attributes. They’re also more flexible: derive
only works for structs and enums; attributes can be applied to other items as well, such as functions. Here’s an example of using an attribute-like macro. Say you have an attribute named route
that annotates functions when using a web application framework:
#[route(GET, "/")]
fn index() {
This #[route]
attribute would be defined by the framework as a procedural macro. The signature of the macro definition function would look like this:
#[proc_macro_attribute]
pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream {
Here, we have two parameters of type TokenStream
. The first is for the contents of the attribute: the GET, "/"
part. The second is the body of the item the attribute is attached to: in this case, fn index() {}
and the rest of the function’s body.
Other than that, attribute-like macros work the same way as custom derive
macros: you create a crate with the proc-macro
crate type and implement a function that generates the code you want!
Function-Like macros
Function-like macros define macros that look like function calls. Similarly to macro_rules!
macros, they’re more flexible than functions; for example, they can take an unknown number of arguments. However, macro_rules!
macros can only be defined using the match-like syntax we discussed earlier. Function-like macros take a TokenStream
parameter and their definition manipulates that TokenStream
using Rust code as the other two types of procedural macros do. An example of a function-like macro is an sql!
macro that might be called like so:
let sql = sql!(SELECT * FROM posts WHERE id=1);
This macro would parse the SQL statement inside it and check that it’s syntactically correct, which is much more complex processing than a macro_rules!
macro can do. The sql!
macro would be defined like this:
#[proc_macro]
pub fn sql(input: TokenStream) -> TokenStream {
This definition is similar to the custom derive
macro’s signature: we receive the tokens that are inside the parentheses and return the code we wanted to generate.
Final Project: Building a Multithreaded Web Server
Here is our plan for building the web server:
- Learn a bit about TCP and HTTP.
- Listen for TCP connections on a socket.
- Parse a small number of HTTP requests.
- Create a proper HTTP response.
- Improve the throughput of our server with a thread pool.
Building a Single-Threaded Web Server
The two main protocols involved in web servers are Hypertext Transfer Protocol (HTTP) and Transmission Control Protocol (TCP). Both protocols are request-response protocols, meaning a client initiates requests and a server listens to the requests and provides a response to the client. The contents of those requests and responses are defined by the protocols.
TCP is the lower-level protocol that describes the details of how information gets from one server to another but doesn’t specify what that information is. HTTP builds on top of TCP by defining the contents of the requests and responses. It’s technically possible to use HTTP with other protocols, but in the vast majority of cases, HTTP sends its data over TCP. We’ll work with the raw bytes of TCP and HTTP requests and responses.
Listening to the TCP Connection
Our web server needs to listen to a TCP connection, so that’s the first part we’ll work on. The standard library offers a std::net
module that lets us do this. Let’s make a new project in the usual fashion:
$ cargo new hello
Created binary (application) `hello` project
$ cd hello
Now enter the code in the following Listing in src/main.rs to start. This code will listen at the local address 127.0.0.1:7878
for incoming TCP streams. When it gets an incoming stream, it will print Connection established!
.
Filename: src/main.rs
use std::net::TcpListener;
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
println!("Connection established!");
}
}
Using TcpListener
, we can listen for TCP connections at the address 127.0.0.1:7878
. In the address, the section before the colon is an IP address representing your computer (this is the same on every computer and doesn’t represent the authors’ computer specifically), and 7878
is the port. We’ve chosen this port for two reasons: HTTP isn’t normally accepted on this port so our server is unlikely to conflict with any other web server you might have running on your machine, and 7878 is rust typed on a telephone.
The bind
function in this scenario works like the new
function in that it will return a new TcpListener
instance. The function is called bind
because, in networking, connecting to a port to listen to is known as “binding to a port.”
The bind
function returns a Result<T, E>
, which indicates that it’s possible for binding to fail. For example, connecting to port 80 requires administrator privileges (non-administrators can listen only on ports higher than 1023), so if we tried to connect to port 80 without being an administrator, binding wouldn’t work. Binding also wouldn’t work, for example, if we ran two instances of our program and so had two programs listening to the same port. Because we’re writing a basic server just for learning purposes, we won’t worry about handling these kinds of errors; instead, we use unwrap
to stop the program if errors happen.
The incoming
method on TcpListener
returns an iterator that gives us a sequence of streams (more specifically, streams of type TcpStream
). A single stream represents an open connection between the client and the server. A connection is the name for the full request and response process in which a client connects to the server, the server generates a response, and the server closes the connection. As such, we will read from the TcpStream
to see what the client sent and then write our response to the stream to send data back to the client. Overall, this for
loop will process each connection in turn and produce a series of streams for us to handle.
For now, our handling of the stream consists of calling unwrap
to terminate our program if the stream has any errors; if there aren’t any errors, the program prints a message. We’ll add more functionality for the success case in the next listing. The reason we might receive errors from the incoming
method when a client connects to the server is that we’re not actually iterating over connections. Instead, we’re iterating over connection attempts. The connection might not be successful for a number of reasons, many of them operating system specific. For example, many operating systems have a limit to the number of simultaneous open connections they can support; new connection attempts beyond that number will produce an error until some of the open connections are closed.
Let’s try running this code! Invoke cargo run
in the terminal and then load 127.0.0.1:7878 in a web browser. The browser should show an error message like “Connection reset” because the server isn’t currently sending back any data. But when you look at your terminal, you should see several messages that were printed when the browser connected to the server!
Running `target/debug/hello`
Connection established!
Connection established!
Connection established!
Sometimes you’ll see multiple messages printed for one browser request; the reason might be that the browser is making a request for the page as well as a request for other resources, like the favicon.ico icon that appears in the browser tab.
It could also be that the browser is trying to connect to the server multiple times because the server isn’t responding with any data. When stream
goes out of scope and is dropped at the end of the loop, the connection is closed as part of the drop
implementation. Browsers sometimes deal with closed connections by retrying, because the problem might be temporary.
Browsers also sometimes open multiple connections to the server without sending any requests, so that if they do later send requests, they can happen faster. When this happens, our server will see each connection, regardless of whether there are any requests over that connection. Many versions of Chrome-based browsers do this, for example; you can disable that optimization by using = private browsing mode or use a different browser.
The important factor is that we’ve successfully gotten a handle to a TCP connection!
Remember to stop the program by pressing ctrl-c
when you’re done running a particular version of the code. Then restart the program by invoking the cargo run
command after you’ve made each set of code changes to make sure you’re running the newest code.
Reading the Request
Let’s implement the functionality to read the request from the browser! To separate the concerns of first getting a connection and then taking some action with the connection, we’ll start a new function for processing connections. In this new handle_connection
function, we’ll read data from the TCP stream and print it so we can see the data being sent from the browser. Change the code to look like the following Listing.
Filename: src/main.rs
use std::{
io::{BufReader, prelude::*},
net::{TcpListener, TcpStream},
};
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
}
}
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&stream);
let http_request: Vec<_> = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
println!("Request: {http_request:#?}");
}
We bring std::io::prelude
and std::io::BufReader
into scope to get access to traits and types that let us read from and write to the stream. In the for
loop in the main
function, instead of printing a message that says we made a connection, we now call the new handle_connection
function and pass the stream
to it.
In the handle_connection
function, we create a new BufReader
instance that wraps a reference to the stream
. The BufReader
adds buffering by managing calls to the std::io::Read
trait methods for us.
We create a variable named http_request
to collect the lines of the request the browser sends to our server. We indicate that we want to collect these lines in a vector by adding the Vec<_>
type annotation.
BufReader
implements the std::io::BufRead
trait, which provides the lines
method. The lines
method returns an iterator of Result<String, std::io::Error>
by splitting the stream of data whenever it sees a newline byte. To get each String
, we map and unwrap
each Result
. The Result
might be an error if the data isn’t valid UTF-8 or if there was a problem reading from the stream. Again, a production program should handle these errors more gracefully, but we’re choosing to stop the program in the error case for simplicity.
The browser signals the end of an HTTP request by sending two newline characters in a row, so to get one request from the stream, we take lines until we get a line that is the empty string. Once we’ve collected the lines into the vector, we’re printing them out using pretty debug formatting so we can take a look at the instructions the web browser is sending to our server.
Let’s try this code! Start the program and make a request in a web browser again. Note that we’ll still get an error page in the browser, but our program’s output in the terminal will now look similar to this:
$ cargo run
Compiling hello v0.1.0 (file:///projects/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.42s
Running `target/debug/hello`
Request: [
"GET / HTTP/1.1",
"Host: 127.0.0.1:7878",
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language: en-US,en;q=0.5",
"Accept-Encoding: gzip, deflate, br",
"DNT: 1",
"Connection: keep-alive",
"Upgrade-Insecure-Requests: 1",
"Sec-Fetch-Dest: document",
"Sec-Fetch-Mode: navigate",
"Sec-Fetch-Site: none",
"Sec-Fetch-User: ?1",
"Cache-Control: max-age=0",
]
Depending on your browser, you might get slightly different output. Now that we’re printing the request data, we can see why we get multiple connections from one browser request by looking at the path after GET
in the first line of the request. If the repeated connections are all requesting /, we know the browser is trying to fetch / repeatedly because it’s not getting a response from our program.
A Closer Look at an HTTP Request
HTTP is a text-based protocol, and a request takes this format:
Method Request-URI HTTP-Version CRLF
headers CRLF
message-body
The first line is the request line that holds information about what the client is requesting. The first part of the request line indicates the method being used, such as GET
or POST
, which describes how the client is making this request. Our client used a GET
request, which means it is asking for information.
The next part of the request line is /, which indicates the uniform resource identifier (URI) the client is requesting: a URI is almost, but not quite, the same as a uniform resource locator (URL). The difference between URIs and URLs isn’t important for our purposes in this chapter, but the HTTP spec uses the term URI, so we can just mentally substitute URL for URI here.
The last part is the HTTP version the client uses, and then the request line ends in a CRLF sequence. (CRLF stands for carriage return and line feed, which are terms from the typewriter days!) The CRLF sequence can also be written as \r\n
, where \r
is a carriage return and \n
is a line feed. The CRLF sequence separates the request line from the rest of the request data. Note that when the CRLF is printed, we see a new line start rather than \r\n
.
Looking at the request line data we received from running our program so far, we see that GET
is the method, / is the request URI, and HTTP/1.1
is the version.
After the request line, the remaining lines starting from Host
: onward are headers. GET
requests have no body.
Try making a request from a different browser or asking for a different address, such as 127.0.0.1:7878/test, to see how the request data changes.
Writing a Response
We’re going to implement sending data in response to a client request. Responses have the following format:
HTTP-Version Status-Code Reason-Phrase CRLF
headers CRLF
message-body
The first line is a status line that contains the HTTP version used in the response, a numeric status code that summarizes the result of the request, and a reason phrase that provides a text description of the status code. After the CRLF sequence are any headers, another CRLF sequence, and the body of the response.
Here is an example response that uses HTTP version 1.1, and has a status code of 200, an OK reason phrase, no headers, and no body:
HTTP/1.1 200 OK\r\n\r\n
The status code 200 is the standard success response. The text is a tiny successful HTTP response. Let’s write this to the stream as our response to a successful request! From the handle_connection
function, remove the println!
that was printing the request data and replace it with the code in the following Listing.
Filename: src/main.rs
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&stream);
let http_request: Vec<_> = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
let response = "HTTP/1.1 200 OK\r\n\r\n";
stream.write_all(response.as_bytes()).unwrap();
}
The first new line defines the response
variable that holds the success message’s data. Then we call as_bytes
on our response
to convert the string data to bytes. The write_all
method on stream
takes a &[u8]
and sends those bytes directly down the connection. Because the write_all
operation could fail, we use unwrap
on any error result as before. Again, in a real application you would add error handling here.
With these changes, let’s run our code and make a request. We’re no longer printing any data to the terminal, so we won’t see any output other than the output from Cargo. When you load 127.0.0.1:7878 in a web browser, you should get a blank page instead of an error. You’ve just handcoded receiving an HTTP request and sending a response!
Returning Real HTML
Let’s implement the functionality for returning more than a blank page. Create the new file hello.html in the root of your project directory, not in the src directory. You can input any HTML you want; The following Listing shows one possibility.
Filename: hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Hello!</title>
</head>
<body>
<h1>Hello!</h1>
<p>Hi from Rust</p>
</body>
</html>
This is a minimal HTML5 document with a heading and some text. To return this from the server when a request is received, we’ll modify handle_connection
as shown in the following Listing to read the HTML file, add it to the response as a body, and send it.
Filename: src/main.rs
use std::{
fs,
io::{BufReader, prelude::*},
net::{TcpListener, TcpStream},
};
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&stream);
let http_request: Vec<_> = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
let status_line = "HTTP/1.1 200 OK";
let contents = fs::read_to_string("hello.html").unwrap();
let length = contents.len();
let response =
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
stream.write_all(response.as_bytes()).unwrap();
}
We’ve added fs
to the use
statement to bring the standard library’s filesystem module into scope. The code for reading the contents of a file to a string should look familiar; we used it when we read the contents of a file for our I/O project.
Next, we use format!
to add the file’s contents as the body of the success response. To ensure a valid HTTP response, we add the Content-Length
header which is set to the size of our response body, in this case the size of hello.html
.
Run this code with cargo run
and load 127.0.0.1:7878 in your browser; you should see your HTML rendered!
Currently, we’re ignoring the request data in http_request
and just sending back the contents of the HTML file unconditionally. That means if you try requesting 127.0.0.1:7878/something-else in your browser, you’ll still get back this same HTML response. At the moment, our server is very limited and does not do what most web servers do. We want to customize our responses depending on the request and only send back the HTML file for a well-formed request to /.
Validating the Request and Selectively Responding
Right now, our web server will return the HTML in the file no matter what the client requested. Let’s add functionality to check that the browser is requesting / before returning the HTML file and return an error if the browser requests anything else. For this we need to modify handle_connection
, as shown in the following Listing. This new code checks the content of the request received against what we know a request for / looks like and adds if
and else
blocks to treat requests differently.
Filename: src/main.rs
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&stream);
let request_line = buf_reader.lines().next().unwrap().unwrap();
if request_line == "GET / HTTP/1.1" {
let status_line = "HTTP/1.1 200 OK";
let contents = fs::read_to_string("hello.html").unwrap();
let length = contents.len();
let response = format!(
"{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"
);
stream.write_all(response.as_bytes()).unwrap();
} else {
}
}
We’re only going to be looking at the first line of the HTTP request, so rather than reading the entire request into a vector, we’re calling next
to get the first item from the iterator. The first unwrap
takes care of the Option
and stops the program if the iterator has no items. The second unwrap
handles the Result
and has the same effect as the unwrap
that was in the map
added in the previous Listing.
Next, we check the request_line
to see if it equals the request line of a GET request to the / path. If it does, the if
block returns the contents of our HTML file.
If the request_line
does not equal the GET request to the / path, it means we’ve received some other request. We’ll add code to the else
block in a moment to respond to all other requests.
Run this code now and request 127.0.0.1:7878; you should get the HTML in hello.html. If you make any other request, such as 127.0.0.1:7878/something-else, you’ll get a connection error like those you saw when running the code in the first two Listings.
Now let’s add the code in the following Listing to the else
block to return a response with the status code 404, which signals that the content for the request was not found. We’ll also return some HTML for a page to render in the browser indicating the response to the end user.
Filename: src/main.rs
} else {
let status_line = "HTTP/1.1 404 NOT FOUND";
let contents = fs::read_to_string("404.html").unwrap();
let length = contents.len();
let response = format!(
"{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"
);
stream.write_all(response.as_bytes()).unwrap();
}
Here, our response has a status line with status code 404 and the reason phrase NOT FOUND
. The body of the response will be the HTML in the file 404.html. You’ll need to create a 404.html file next to hello.html for the error page; again feel free to use any HTML you want or use the example HTML in the following Listing.
Filename: 404.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Hello!</title>
</head>
<body>
<h1>Oops!</h1>
<p>Sorry, I don't know what you're asking for.</p>
</body>
</html>
With these changes, run your server again. Requesting 127.0.0.1:7878 should return the contents of hello.html, and any other request, like 127.0.0.1:7878/foo, should return the error HTML from 404.html.
A Touch of Refactoring
At the moment, the if
and else
blocks have a lot of repetition: they’re both reading files and writing the contents of the files to the stream. The only differences are the status line and the filename. Let’s make the code more concise by pulling out those differences into separate if
and else
lines that will assign the values of the status line and the filename to variables; we can then use those variables unconditionally in the code to read the file and write the response. The following Listing shows the resultant code after replacing the large if
and else
blocks.
Filename: src/main.rs
fn handle_connection(mut stream: TcpStream) {
let (status_line, filename) = if request_line == "GET / HTTP/1.1" {
("HTTP/1.1 200 OK", "hello.html")
} else {
("HTTP/1.1 404 NOT FOUND", "404.html")
};
let contents = fs::read_to_string(filename).unwrap();
let length = contents.len();
let response =
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
stream.write_all(response.as_bytes()).unwrap();
}
Now the if
and else
blocks only return the appropriate values for the status line and filename in a tuple; we then use destructuring to assign these two values to status_line
and filename
using a pattern in the let
statement, as discussed in the previous Chapter.
The previously duplicated code is now outside the if
and else
blocks and uses the status_line
and filename
variables. This makes it easier to see the difference between the two cases, and it means we have only one place to update the code if we want to change how the file reading and response writing work. The behavior of the code in the last Listing will be the same as that in the previous Listing.
Turning Our Single-Threaded Server into a Multithreaded Server
Simulating a Slow Request in the Current Server Implementation
We’ll look at how a slow-processing request can affect other requests made to our current server implementation. The following Listing implements handling a request to /sleep with a simulated slow response that will cause the server to sleep for five seconds before responding.
Filename: src/main.rs
use std::{
fs,
io::{BufReader, prelude::*},
net::{TcpListener, TcpStream},
thread,
time::Duration,
};
fn handle_connection(mut stream: TcpStream) {
let (status_line, filename) = match &request_line[..] {
"GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"),
"GET /sleep HTTP/1.1" => {
thread::sleep(Duration::from_secs(5));
("HTTP/1.1 200 OK", "hello.html")
}
_ => ("HTTP/1.1 404 NOT FOUND", "404.html"),
};
}
We switched from if
to match
now that we have three cases. We need to explicitly match on a slice of request_line
to pattern match against the string literal values; match
doesn’t do automatic referencing and dereferencing, like the equality method does.
Start the server using cargo run
. Then open two browser windows: one for http://127.0.0.1:7878/ and the other for http://127.0.0.1:7878/sleep/. If you enter the /URI a few times, as before, you’ll see it respond quickly. But if you enter /sleep and then load /, you’ll see that / waits until sleep
has slept for its full five seconds before loading.
Improving Throughput with a Thread Pool
A thread pool is a group of spawned threads that are waiting and ready to handle a task. When the program receives a new task, it assigns one of the threads in the pool to the task, and that thread will process the task. The remaining threads in the pool are available to handle any other tasks that come in while the first thread is processing. When the first thread is done processing its task, it’s returned to the pool of idle threads, ready to handle a new task. A thread pool allows you to process connections concurrently, increasing the throughput of your server.
We’ll limit the number of threads in the pool to a small number to protect us from DoS attacks; if we had our program create a new thread for each request as it came in, someone making 10 million requests to our server could create havoc by using up all our server’s resources and grinding the processing of requests to a halt.
Rather than spawning unlimited threads, then, we’ll have a fixed number of threads waiting in the pool. Requests that come in are sent to the pool for processing. The pool will maintain a queue of incoming requests. Each of the threads in the pool will pop off a request from this queue, handle the request, and then ask the queue for another request. With this design, we can process up to N
requests concurrently, where N
is the number of threads. If each thread is responding to a long-running request, subsequent requests can still back up in the queue, but we’ve increased the number of long-running requests we can handle before reaching that point.
This technique is just one of many ways to improve the throughput of a web server. Other options you might explore are the fork/join model, the single-threaded async I/O model, and the multithreaded async I/O model. If you’re interested in this topic, you can read more about other solutions and try to implement them; with a low-level language like Rust, all of these options are possible.
Before we begin implementing a thread pool, let’s talk about what using the pool should look like. When you’re trying to design code, writing the client interface first can help guide your design. Write the API of the code so it’s structured in the way you want to call it; then implement the functionality within that structure rather than implementing the functionality and then designing the public API.
Similar to how we used test-driven development in the project in the previous Chapter, we’ll use compiler-driven development here. We’ll write the code that calls the functions we want, and then we’ll look at errors from the compiler to determine what we should change next to get the code to work. Before we do that, however, we’ll explore the technique we’re not going to use as a starting point.
Spawning a Thread for Each Request
First, let’s explore how our code might look if it did create a new thread for every connection. As mentioned earlier, this isn’t our final plan due to the problems with potentially spawning an unlimited number of threads, but it is a starting point to get a working multithreaded server first. Then we’ll add the thread pool as an improvement, and contrasting the two solutions will be easier. The following Listing shows the changes to make to main
to spawn a new thread to handle each stream within the for
loop.
Filename: src/main.rs
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
thread::spawn(|| {
handle_connection(stream);
});
}
}
As you learned in the previous Chapter, thread::spawn
will create a new thread and then run the code in the closure in the new thread. If you run this code and load /sleep in your browser, then / in two more browser tabs, you’ll indeed see that the requests to / don’t have to wait for /sleep to finish. However, as we mentioned, this will eventually overwhelm the system because you’d be making new threads without any limit.
You may also recall from the previous Chapter that this is exactly the kind of situation where async and await really shine! Keep that in mind as we build the thread pool and think about how things would look different or the same with async.
Creating a Finite Number of Threads
We want our thread pool to work in a similar, familiar way so that switching from threads to a thread pool doesn’t require large changes to the code that uses our API. The following Listing shows the hypothetical interface for a ThreadPool
struct we want to use instead of thread::spawn
.
Filename: src/main.rs
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
let pool = ThreadPool::new(4);
for stream in listener.incoming() {
let stream = stream.unwrap();
pool.execute(|| {
handle_connection(stream);
});
}
}
We use ThreadPool::new
to create a new thread pool with a configurable number of threads, in this case four. Then, in the for
loop, pool.execute
has a similar interface as thread::spawn
in that it takes a closure the pool should run for each stream. We need to implement pool.execute
so it takes the closure and gives it to a thread in the pool to run. This code won’t yet compile, but we’ll try so the compiler can guide us in how to fix it.
Building ThreadPool Using Compiler Driven Development
Make the changes in the Listing to src/main.rs, and then let’s use the compiler errors from cargo check
to drive our development. Here is the first error we get:
$ cargo check
Checking hello v0.1.0 (file:///projects/hello)
error[E0433]: failed to resolve: use of undeclared type `ThreadPool`
--> src/main.rs:11:16
|
11 | let pool = ThreadPool::new(4);
| ^^^^^^^^^^ use of undeclared type `ThreadPool`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `hello` (bin "hello") due to 1 previous error
Great! This error tells us we need a ThreadPool
type or module, so we’ll build one now. Our ThreadPool
implementation will be independent of the kind of work our web server is doing. So let’s switch the hello
crate from a binary crate to a library crate to hold our ThreadPool
implementation. After we change to a library crate, we could also use the separate thread pool library for any work we want to do using a thread pool, not just for serving web requests.
Create a src/lib.rs file that contains the following, which is the simplest definition of a ThreadPool
struct that we can have for now:
Filename: src/lib.rs
pub struct ThreadPool;
Then edit main.rs file to bring ThreadPool
into scope from the library crate by adding the following code to the top of src/main.rs:
Filename: src/main.rs
use hello::ThreadPool;
This code still won’t work, but let’s check it again to get the next error that we need to address:
$ cargo check
Checking hello v0.1.0 (file:///projects/hello)
error[E0599]: no function or associated item named `new` found for struct `ThreadPool` in the current scope
--> src/main.rs:12:28
|
12 | let pool = ThreadPool::new(4);
| ^^^ function or associated item not found in `ThreadPool`
For more information about this error, try `rustc --explain E0599`.
error: could not compile `hello` (bin "hello") due to 1 previous error
This error indicates that next we need to create an associated function named new
for ThreadPool
. We also know that new
needs to have one parameter that can accept 4
as an argument and should return a ThreadPool
instance. Let’s implement the simplest new
function that will have those characteristics:
Filename: src/lib.rs
pub struct ThreadPool;
impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
ThreadPool
}
}
We chose usize
as the type of the size
parameter because we know that a negative number of threads doesn’t make any sense. We also know we’ll use this 4
as the number of elements in a collection of threads, which is what the usize
type is for.
Let’s check the code again:
$ cargo check
Checking hello v0.1.0 (file:///projects/hello)
error[E0599]: no method named `execute` found for struct `ThreadPool` in the current scope
--> src/main.rs:17:14
|
17 | pool.execute(|| {
| -----^^^^^^^ method not found in `ThreadPool`
For more information about this error, try `rustc --explain E0599`.
error: could not compile `hello` (bin "hello") due to 1 previous error
Now the error occurs because we don’t have an execute
method on ThreadPool
. In addition, we’ll implement the execute
function so it takes the closure it’s given and gives it to an idle thread in the pool to run.
We’ll define the execute
method on ThreadPool
to take a closure as a parameter. We can take closures as parameters with three different traits: Fn
, FnMut
, and FnOnce
. We need to decide which kind of closure to use here. We know we’ll end up doing something similar to the standard library thread::spawn
implementation, so we can look at what bounds the signature of thread::spawn
has on its parameter. The documentation shows us the following:
pub fn spawn<F, T>(f: F) -> JoinHandle<T>
where
F: FnOnce() -> T,
F: Send + 'static,
T: Send + 'static,
The F
type parameter is the one we’re concerned with here; the T
type parameter is related to the return value, and we’re not concerned with that. We can see that spawn
uses FnOnce
as the trait bound on F
. This is probably what we want as well, because we’ll eventually pass the argument we get in execute
to spawn
. We can be further confident that FnOnce
is the trait we want to use because the thread for running a request will only execute that request’s closure one time, which matches the Once
in FnOnce
.
The F
type parameter also has the trait bound Send
and the lifetime bound 'static
, which are useful in our situation: we need Send
to transfer the closure from one thread to another and 'static
because we don’t know how long the thread will take to execute. Let’s create an execute
method on ThreadPool
that will take a generic parameter of type F
with these bounds:
Filename: src/lib.rs
impl ThreadPool {
pub fn execute<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
{
}
}
We still use the ()
after FnOnce
because this FnOnce
represents a closure that takes no parameters and returns the unit type ()
. Just like function definitions, the return type can be omitted from the signature, but even if we have no parameters, we still need the parentheses.
Again, this is the simplest implementation of the execute
method: it does nothing, but we’re only trying to make our code compile. Let’s check it again:
$ cargo check
Checking hello v0.1.0 (file:///projects/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.24s
It compiles! But note that if you try cargo run
and make a request in the browser, you’ll see the errors in the browser that we saw at the beginning of the chapter. Our library isn’t actually calling the closure passed to execute
yet!
Note: A saying you might hear about languages with strict compilers, such as Haskell and Rust, is “if the code compiles, it works.” But this saying is not universally true. Our project compiles, but it does absolutely nothing! If we were building a real, complete project, this would be a good time to start writing unit tests to check that the code compiles and has the behavior we want.
Consider: what would be different here if we were going to execute a future instead of a closure?
Validating the Number of Threads in new
We aren’t doing anything with the parameters to new
and execute
. Let’s implement the bodies of these functions with the behavior we want. To start, let’s think about new
. Earlier we chose an unsigned type for the size
parameter because a pool with a negative number of threads makes no sense. However, a pool with zero threads also makes no sense, yet zero is a perfectly valid usize
. We’ll add code to check that size
is greater than zero before we return a ThreadPool
instance and have the program panic if it receives a zero by using the assert!
macro, as shown in the following Listing.
Filename: src/lib.rs
impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0);
ThreadPool
}
}
We’ve also added some documentation for our ThreadPool
with doc comments. Note that we followed good documentation practices by adding a section that calls out the situations in which our function can panic. Try running cargo doc --open
and clicking the ThreadPool
struct to see what the generated docs for new
look like!
Instead of adding the assert!
macro as we’ve done here, we could change new
into build
and return a Result
like we did with Config::build
in the I/O project. But we’ve decided in this case that trying to create a thread pool without any threads should be an unrecoverable error. If you’re feeling ambitious, try to write a function named build
with the following signature to compare with the new
function:
pub fn build(size: usize) -> Result<ThreadPool, PoolCreationError> {
Creating Space to Store the Threads
Now that we have a way to know we have a valid number of threads to store in the pool, we can create those threads and store them in the ThreadPool
struct before returning the struct. But how do we “store” a thread? Let’s take another look at the thread::spawn
signature:
pub fn spawn<F, T>(f: F) -> JoinHandle<T>
where
F: FnOnce() -> T,
F: Send + 'static,
T: Send + 'static,
The spawn
function returns a JoinHandle<T>
, where T
is the type that the closure returns. Let’s try using JoinHandle
too and see what happens. In our case, the closures we’re passing to the thread pool will handle the connection and not return anything, so T
will be the unit type ()
.
The code in the following Listing will compile but doesn’t create any threads yet. We’ve changed the definition of ThreadPool
to hold a vector of thread::JoinHandle<()>
instances, initialized the vector with a capacity of size
, set up a for
loop that will run some code to create the threads, and returned a ThreadPool
instance containing them.
Filename: src/lib.rs
use std::thread;
pub struct ThreadPool {
threads: Vec<thread::JoinHandle<()>>,
}
impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0);
let mut threads = Vec::with_capacity(size);
for _ in 0..size {
}
ThreadPool { threads }
}
}
We’ve brought std::thread
into scope in the library crate because we’re using thread::JoinHandle
as the type of the items in the vector in ThreadPool
.
Once a valid size is received, our ThreadPool
creates a new vector that can hold size
items. The with_capacity
function performs the same task as Vec::new
but with an important difference: it pre-allocates space in the vector. Because we know we need to store size
elements in the vector, doing this allocation up front is slightly more efficient than using Vec::new
, which resizes itself as elements are inserted.
When you run cargo check
again, it should succeed.
A Worker Struct Responsible for Sending Code from the ThreadPool to a Thread
We left a comment in the for
loop in the previous Listing regarding the creation of threads. Here, we’ll look at how we actually create threads. The standard library provides thread::spawn
as a way to create threads, and thread::spawn
expects to get some code the thread should run as soon as the thread is created. However, in our case, we want to create the threads and have them wait for code that we’ll send later. The standard library’s implementation of threads doesn’t include any way to do that; we have to implement it manually.
We’ll implement this behavior by introducing a new data structure between the ThreadPool
and the threads that will manage this new behavior. We’ll call this data structure Worker, which is a common term in pooling implementations. The Worker
picks up code that needs to be run and runs the code in the Worker’s thread.
Instead of storing a vector of JoinHandle<()>
instances in the thread pool, we’ll store instances of the Worker
struct. Each Worker
will store a single JoinHandle<()>
instance. Then we’ll implement a method on Worker
that will take a closure of code to run and send it to the already running thread for execution. We’ll also give each Worker
an id
so we can distinguish between the different instances of Worker
in the pool when logging or debugging.
Here is the new process that will happen when we create a ThreadPool
. We’ll implement the code that sends the closure to the thread after we have Worker
set up in this way:
- Define a
Worker
struct that holds an id
and a JoinHandle<()>
.
- Change
ThreadPool
to hold a vector of Worker
instances.
- Define a
Worker::new
function that takes an id
number and returns a Worker
instance that holds the id
and a thread spawned with an empty closure.
- In
ThreadPool::new
, use the for
loop counter to generate an id
, create a new Worker
with that id
, and store the worker in the vector.
Ready? Here is the following Listing with one way to make the preceding modifications.
Filename: src/lib.rs
use std::thread;
pub struct ThreadPool {
workers: Vec<Worker>,
}
impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0);
let mut workers = Vec::with_capacity(size);
for id in 0..size {
workers.push(Worker::new(id));
}
ThreadPool { workers }
}
}
struct Worker {
id: usize,
thread: thread::JoinHandle<()>,
}
impl Worker {
fn new(id: usize) -> Worker {
let thread = thread::spawn(|| {});
Worker { id, thread }
}
}
We’ve changed the name of the field on ThreadPool
from threads
to workers
because it’s now holding Worker
instances instead of JoinHandle<()>
instances. We use the counter in the for
loop as an argument to Worker::new
, and we store each new Worker
in the vector named workers
.
External code (like our server in src/main.rs) doesn’t need to know the implementation details regarding using a Worker
struct within ThreadPool
, so we make the Worker
struct and its new
function private. The Worker::new
function uses the id
we give it and stores a JoinHandle<()>
instance that is created by spawning a new thread using an empty closure.
Note: If the operating system can’t create a thread because there aren’t enough system resources, thread::spawn
will panic. That will cause our whole server to panic, even though the creation of some threads might succeed. For simplicity’s sake, this behavior is fine, but in a production thread pool implementation, you’d likely want to use std::thread::Builder
and its spawn
method that returns Result
instead.
This code will compile and will store the number of Worker
instances we specified as an argument to ThreadPool::new
. But we’re still not processing the closure that we get in execute
. Let’s look at how to do that next.
Sending Requests to Threads via Channels
The next problem we’ll tackle is that the closures given to thread::spawn
do absolutely nothing. Currently, we get the closure we want to execute in the execute
method. But we need to give thread::spawn
a closure to run when we create each Worker
during the creation of the ThreadPool
.
We want the Worker
structs that we just created to fetch the code to run from a queue held in the ThreadPool
and send that code to its thread to run.
The channels we learned about in the previous Chapter - a simple way to communicate between two threads - would be perfect for this use case. We’ll use a channel to function as the queue of jobs, and execute
will send a job from the ThreadPool
to the Worker
instances, which will send the job to its thread. Here is the plan:
- The
ThreadPool
will create a channel and hold on to the sender.
- Each
Worker
will hold on to the receiver.
- We’ll create a new
Job
struct that will hold the closures we want to send down the channel.
- The
execute
method will send the job it wants to execute through the sender.
- In its thread, the
Worker
will loop over its receiver and execute the closures of any jobs it receives.
Let’s start by creating a channel in ThreadPool::new
and holding the sender in the ThreadPool
instance, as shown in the following Listing. The Job
struct doesn’t hold anything for now but will be the type of item we’re sending down the channel.
Filename: src/lib.rs
use std::{sync::mpsc, thread};
pub struct ThreadPool {
workers: Vec<Worker>,
sender: mpsc::Sender<Job>,
}
struct Job;
impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0);
let (sender, receiver) = mpsc::channel();
let mut workers = Vec::with_capacity(size);
for id in 0..size {
workers.push(Worker::new(id));
}
ThreadPool { workers, sender }
}
}
In ThreadPool::new
, we create our new channel and have the pool hold the sender. This will successfully compile.
Let’s try passing a receiver of the channel into each Worker
as the thread pool creates the channel. We know we want to use the receiver in the thread that the Worker
instances spawn, so we’ll reference the receiver
parameter in the closure. The code in the following Listing won’t quite compile yet.
Filename: src/lib.rs
impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0);
let (sender, receiver) = mpsc::channel();
let mut workers = Vec::with_capacity(size);
for id in 0..size {
workers.push(Worker::new(id, receiver));
}
ThreadPool { workers, sender }
}
}
impl Worker {
fn new(id: usize, receiver: mpsc::Receiver<Job>) -> Worker {
let thread = thread::spawn(|| {
receiver;
});
Worker { id, thread }
}
}
We’ve made some small and straightforward changes: we pass the receiver into Worker::new
, and then we use it inside the closure.
When we try to check this code, we get this error:
$ cargo check
Checking hello v0.1.0 (file:///projects/hello)
error[E0382]: use of moved value: `receiver`
--> src/lib.rs:26:42
|
21 | let (sender, receiver) = mpsc::channel();
| -------- move occurs because `receiver` has type `std::sync::mpsc::Receiver<Job>`, which does not implement the `Copy` trait
...
25 | for id in 0..size {
| ----------------- inside of this loop
26 | workers.push(Worker::new(id, receiver));
| ^^^^^^^^ value moved here, in previous iteration of loop
|
note: consider changing this parameter type in method `new` to borrow instead if owning the value isn't necessary
--> src/lib.rs:47:33
|
47 | fn new(id: usize, receiver: mpsc::Receiver<Job>) -> Worker {
| --- in this method ^^^^^^^^^^^^^^^^^^^ this parameter takes ownership of the value
help: consider moving the expression out of the loop so it is only moved once
|
25 ~ let mut value = Worker::new(id, receiver);
26 ~ for id in 0..size {
27 ~ workers.push(value);
|
For more information about this error, try `rustc --explain E0382`.
error: could not compile `hello` (lib) due to 1 previous error
The code is trying to pass receiver
to multiple Worker
instances. This won’t work, as you’ll recall from the previous Chapter: the channel implementation that Rust provides is multiple producer, single consumer. This means we can’t just clone the consuming end of the channel to fix this code. We also don’t want to send a message multiple times to multiple consumers; we want one list of messages with multiple Worker
instances such that each message gets processed once.
Additionally, taking a job off the channel queue involves mutating the receiver
, so the threads need a safe way to share and modify receiver
; otherwise, we might get race conditions.
Recall the thread-safe smart pointers discussed in the previous Chapter: to share ownership across multiple threads and allow the threads to mutate the value, we need to use Arc<Mutex<T>>
. The Arc
type will let multiple Worker
instances own the receiver, and Mutex
will ensure that only one Worker
gets a job from the receiver at a time. The following Listing shows the changes we need to make.
Filename: src/lib.rs
use std::{
sync::{Arc, Mutex, mpsc},
thread,
};
impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0);
let (sender, receiver) = mpsc::channel();
let receiver = Arc::new(Mutex::new(receiver));
let mut workers = Vec::with_capacity(size);
for id in 0..size {
workers.push(Worker::new(id, Arc::clone(&receiver)));
}
ThreadPool { workers, sender }
}
}
impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
}
}
In ThreadPool::new
, we put the receiver in an Arc
and a Mutex
. For each new Worker
, we clone the Arc
to bump the reference count so the Worker
instances can share ownership of the receiver.
With these changes, the code compiles! We’re getting there!
Implementing the execute Method
Let’s finally implement the execute
method on ThreadPool
. We’ll also change Job
from a struct to a type alias for a trait object that holds the type of closure that execute
receives. Type aliases allow us to make long types shorter for ease of use. Look at the following Listing.
Filename: src/lib.rs
type Job = Box<dyn FnOnce() + Send + 'static>;
impl ThreadPool {
pub fn execute<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
{
let job = Box::new(f);
self.sender.send(job).unwrap();
}
}
After creating a new Job
instance using the closure we get in execute
, we send that job down the sending end of the channel. We’re calling unwrap
on send
for the case that sending fails. This might happen if, for example, we stop all our threads from executing, meaning the receiving end has stopped receiving new messages. At the moment, we can’t stop our threads from executing: our threads continue executing as long as the pool exists. The reason we use unwrap
is that we know the failure case won’t happen, but the compiler doesn’t know that.
But we’re not quite done yet! In the Worker
, our closure being passed to thread::spawn
still only references the receiving end of the channel. Instead, we need the closure to loop forever, asking the receiving end of the channel for a job and running the job when it gets one. Let’s make the change shown in the following Listing to Worker::new
.
Filename: src/lib.rs
impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
let thread = thread::spawn(move || {
loop {
let job = receiver.lock().unwrap().recv().unwrap();
println!("Worker {id} got a job; executing.");
job();
}
});
Worker { id, thread }
}
}
Here, we first call lock
on the receiver
to acquire the mutex, and then we call unwrap
to panic on any errors. Acquiring a lock might fail if the mutex is in a poisoned state, which can happen if some other thread panicked while holding the lock rather than releasing the lock. In this situation, calling unwrap
to have this thread panic is the correct action to take. Feel free to change this unwrap
to an expect
with an error message that is meaningful to you.
If we get the lock on the mutex, we call recv
to receive a Job
from the channel. A final unwrap
moves past any errors here as well, which might occur if the thread holding the sender has shut down, similar to how the send
method returns Err
if the receiver shuts down.
The call to recv
blocks, so if there is no job yet, the current thread will wait until a job becomes available. The Mutex<T>
ensures that only one Worker
thread at a time is trying to request a job.
Our thread pool is now in a working state! Give it a cargo run
and make some requests:
$ cargo run
Compiling hello v0.1.0 (file:///projects/hello)
warning: field `workers` is never read
--> src/lib.rs:7:5
|
6 | pub struct ThreadPool {
| ---------- field in this struct
7 | workers: Vec<Worker>,
| ^^^^^^^
|
= note: `` on by default
warning: fields `id` and `thread` are never read
--> src/lib.rs:48:5
|
47 | struct Worker {
| ------ fields in this struct
48 | id: usize,
| ^^
49 | thread: thread::JoinHandle<()>,
| ^^^^^^
warning: `hello` (lib) generated 2 warnings
Finished `dev` profile [unoptimized + debuginfo] target(s) in 4.91s
Running `target/debug/hello`
Worker 0 got a job; executing.
Worker 2 got a job; executing.
Worker 1 got a job; executing.
Worker 3 got a job; executing.
Worker 0 got a job; executing.
Worker 2 got a job; executing.
Worker 1 got a job; executing.
Worker 3 got a job; executing.
Worker 0 got a job; executing.
Worker 2 got a job; executing.
Success! We now have a thread pool that executes connections asynchronously. There are never more than four threads created, so our system won’t get overloaded if the server receives a lot of requests. If we make a request to /sleep, the server will be able to serve other requests by having another thread run them.
Note: If you open /sleep in multiple browser windows simultaneously, they might load one at a time in five-second intervals. Some web browsers execute multiple instances of the same request sequentially for caching reasons. This limitation is not caused by our web server.
This is a good time to pause and consider how the code in Listings would be different if we were using futures instead of a closure for the work to be done. What types would change? How would the method signatures be different, if at all? What parts of the code would stay the same?
After learning about the while let
loop in previous Chapters, you might be wondering why we didn’t write the worker thread code as shown in the following Listing.
Filename: src/lib.rs
impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
let thread = thread::spawn(move || {
while let Ok(job) = receiver.lock().unwrap().recv() {
println!("Worker {id} got a job; executing.");
job();
}
});
Worker { id, thread }
}
}
This code compiles and runs but doesn’t result in the desired threading behavior: a slow request will still cause other requests to wait to be processed. The reason is somewhat subtle: the Mutex
struct has no public unlock
method because the ownership of the lock is based on the lifetime of the MutexGuard<T>
within the LockResult<MutexGuard<T>>
that the lock
method returns. At compile time, the borrow checker can then enforce the rule that a resource guarded by a Mutex
cannot be accessed unless we hold the lock. However, this implementation can also result in the lock being held longer than intended if we aren’t mindful of the lifetime of the MutexGuard<T>
.
The code in the previous Listing that uses let job = receiver.lock().unwrap().recv().unwrap();
works because with let
, any temporary values used in the expression on the right hand side of the equal sign are immediately dropped when the let
statement ends. However, while let
(and if let
and match
) does not drop temporary values until the end of the associated block. In the last Listing, the lock remains held for the duration of the call to job()
, meaning other Worker
instances cannot receive jobs.
Graceful Shutdown and Cleanup
The code is responding to requests asynchronously through the use of a thread pool, as we intended. We get some warnings about the workers
, id
, and thread
fields that we’re not using in a direct way that reminds us we’re not cleaning up anything. When we use the less elegant ctrl-c
method to halt the main thread, all other threads are stopped immediately as well, even if they’re in the middle of serving a request.
Next, then, we’ll implement the Drop
trait to call join
on each of the threads in the pool so they can finish the requests they’re working on before closing. Then we’ll implement a way to tell the threads they should stop accepting new requests and shut down. To see this code in action, we’ll modify our server to accept only two requests before gracefully shutting down its thread pool.
One thing to notice as we go: none of this affects the parts of the code that handle executing the closures, so everything here would be just the same if we were using a thread pool for an async runtime.
Implementing the Drop Trait on ThreadPool
Let’s start with implementing Drop
on our thread pool. When the pool is dropped, our threads should all join to make sure they finish their work. The following Listing shows a first attempt at a Drop
implementation; this code won’t quite work yet.
Filename: src/lib.rs
impl Drop for ThreadPool {
fn drop(&mut self) {
for worker in &mut self.workers {
println!("Shutting down worker {}", worker.id);
worker.thread.join().unwrap();
}
}
}
First, we loop through each of the thread pool workers
. We use &mut
for this because self
is a mutable reference, and we also need to be able to mutate worker
. For each worker, we print a message saying that this particular Worker
instance is shutting down, and then we call join
on that Worker
instance’s thread. If the call to join
fails, we use unwrap
to make Rust panic and go into an ungraceful shutdown.
Here is the error we get when we compile this code:
$ cargo check
Checking hello v0.1.0 (file:///projects/hello)
error[E0507]: cannot move out of `worker.thread` which is behind a mutable reference
--> src/lib.rs:52:13
|
52 | worker.thread.join().unwrap();
| ^^^^^^^^^^^^^ ------ `worker.thread` moved due to this method call
| |
| move occurs because `worker.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait
|
note: `JoinHandle::<T>::join` takes ownership of the receiver `self`, which moves `worker.thread`
--> /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/thread/mod.rs:1876:17
For more information about this error, try `rustc --explain E0507`.
error: could not compile `hello` (lib) due to 1 previous error
The error tells us we can’t call join
because we only have a mutable borrow of each worker
and join
takes ownership of its argument. To solve this issue, we need to move the thread out of the Worker
instance that owns thread
so join
can consume the thread. One way to do this is by taking the same approach we did in the previous Listing. If Worker held an Option<thread::JoinHandle<()>>
, we could call the take
method on the Option
to move the value out of the Some
variant and leave a None
variant in its place. In other words, a Worker
that is running would have a Some
variant in thread, and when we wanted to clean up a Worker
, we’d replace Some
with None
so the Worker
wouldn’t have a thread to run.
However, the only time this would come up would be when dropping the Worker
. In exchange, we’d have to deal with an Option<thread::JoinHandle<()>>
anywhere we accessed worker.thread
. Idiomatic Rust uses Option
quite a bit, but when you find yourself wrapping something you know will always be present in Option
as a workaround like this, it’s a good idea to look for alternative approaches. They can make your code cleaner and less error-prone.
In this case, a better alternative exists: the Vec::drain
method. It accepts a range parameter to specify which items to remove from the Vec
, and returns an iterator of those items. Passing the ..
range syntax will remove every value from the Vec
.
So we need to update the ThreadPool drop
implementation like this:
Filename: src/lib.rs
impl Drop for ThreadPool {
fn drop(&mut self) {
for worker in self.workers.drain(..) {
println!("Shutting down worker {}", worker.id);
worker.thread.join().unwrap();
}
}
}
This resolves the compiler error and does not require any other changes to our code.
Signaling to the Threads to Stop Listening for Jobs
With all the changes we’ve made, our code compiles without any warnings. However, the bad news is that this code doesn’t function the way we want it to yet. The key is the logic in the closures run by the threads of the Worker
instances: at the moment, we call join
, but that won’t shut down the threads because they loop
forever looking for jobs. If we try to drop our ThreadPool
with our current implementation of drop
, the main thread will block forever, waiting for the first thread to finish.
To fix this problem, we’ll need a change in the ThreadPool drop
implementation and then a change in the Worker
loop.
First we’ll change the ThreadPool drop
implementation to explicitly drop the sender
before waiting for the threads to finish. The following Listing shows the changes to ThreadPool
to explicitly drop sender
. Unlike with the thread, here we do need to use an Option
to be able to move sender
out of ThreadPool
with Option::take
.
Filename: src/lib.rs
pub struct ThreadPool {
workers: Vec<Worker>,
sender: Option<mpsc::Sender<Job>>,
}
impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
ThreadPool {
workers,
sender: Some(sender),
}
}
pub fn execute<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
{
let job = Box::new(f);
self.sender.as_ref().unwrap().send(job).unwrap();
}
}
impl Drop for ThreadPool {
fn drop(&mut self) {
drop(self.sender.take());
for worker in self.workers.drain(..) {
println!("Shutting down worker {}", worker.id);
worker.thread.join().unwrap();
}
}
}
Dropping sender
closes the channel, which indicates no more messages will be sent. When that happens, all the calls to recv
that the Worker
instances do in the infinite loop will return an error. In the following Listing, we change the Worker
loop to gracefully exit the loop in that case, which means the threads will finish when the ThreadPool drop
implementation calls join
on them.
Filename: src/lib.rs
impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
let thread = thread::spawn(move || {
loop {
let message = receiver.lock().unwrap().recv();
match message {
Ok(job) => {
println!("Worker {id} got a job; executing.");
job();
}
Err(_) => {
println!("Worker {id} disconnected; shutting down.");
break;
}
}
}
});
Worker { id, thread }
}
}
To see this code in action, let’s modify main
to accept only two requests before gracefully shutting down the server, as shown in the following Listing.
Filename: src/main.rs
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
let pool = ThreadPool::new(4);
for stream in listener.incoming().take(2) {
let stream = stream.unwrap();
pool.execute(|| {
handle_connection(stream);
});
}
println!("Shutting down.");
}
You wouldn’t want a real-world web server to shut down after serving only two requests. This code just demonstrates that the graceful shutdown and cleanup is in working order.
The take
method is defined in the Iterator
trait and limits the iteration to the first two items at most. The ThreadPool
will go out of scope at the end of main
, and the drop
implementation will run.
Start the server with cargo run
, and make three requests. The third request should error, and in your terminal you should see output similar to this:
$ cargo run
Compiling hello v0.1.0 (file:///projects/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.41s
Running `target/debug/hello`
Worker 0 got a job; executing.
Shutting down.
Shutting down worker 0
Worker 3 got a job; executing.
Worker 1 disconnected; shutting down.
Worker 2 disconnected; shutting down.
Worker 3 disconnected; shutting down.
Worker 0 disconnected; shutting down.
Shutting down worker 1
Shutting down worker 2
Shutting down worker 3
You might see a different ordering of Worker
IDs and messages printed. We can see how this code works from the messages: Worker
instances 0 and 3 got the first two requests. The server stopped accepting connections after the second connection, and the Drop
implementation on ThreadPool
starts executing before Worker
3 even starts its job. Dropping the sender
disconnects all the Worker
instances and tells them to shut down. The Worker
instances each print a message when they disconnect, and then the thread pool calls join
to wait for each Worker
thread to finish.
Notice one interesting aspect of this particular execution: the ThreadPool
dropped the sender
, and before any Worker
received an error, we tried to join Worker
0. Worker
0 had not yet gotten an error from recv
, so the main thread blocked waiting for Worker
0 to finish. In the meantime, Worker
3 received a job and then all threads received an error. When Worker 0 finished, the main thread waited for the rest of the Worker instances to finish. At that point, they had all exited their loops and stopped.
Congrats! We’ve now completed our project; we have a basic web server that uses a thread pool to respond asynchronously. We’re able to perform a graceful shutdown of the server, which cleans up all the threads in the pool.
Here’s the full code for reference:
Filename: src/main.rs
use hello::ThreadPool;
use std::{
fs,
io::{BufReader, prelude::*},
net::{TcpListener, TcpStream},
thread,
time::Duration,
};
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
let pool = ThreadPool::new(4);
for stream in listener.incoming().take(2) {
let stream = stream.unwrap();
pool.execute(|| {
handle_connection(stream);
});
}
println!("Shutting down.");
}
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&stream);
let request_line = buf_reader.lines().next().unwrap().unwrap();
let (status_line, filename) = match &request_line[..] {
"GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"),
"GET /sleep HTTP/1.1" => {
thread::sleep(Duration::from_secs(5));
("HTTP/1.1 200 OK", "hello.html")
}
_ => ("HTTP/1.1 404 NOT FOUND", "404.html"),
};
let contents = fs::read_to_string(filename).unwrap();
let length = contents.len();
let response =
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
stream.write_all(response.as_bytes()).unwrap();
}
Filename: src/lib.rs
use std::{
sync::{Arc, Mutex, mpsc},
thread,
};
pub struct ThreadPool {
workers: Vec<Worker>,
sender: Option<mpsc::Sender<Job>>,
}
type Job = Box<dyn FnOnce() + Send + 'static>;
impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0);
let (sender, receiver) = mpsc::channel();
let receiver = Arc::new(Mutex::new(receiver));
let mut workers = Vec::with_capacity(size);
for id in 0..size {
workers.push(Worker::new(id, Arc::clone(&receiver)));
}
ThreadPool {
workers,
sender: Some(sender),
}
}
pub fn execute<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
{
let job = Box::new(f);
self.sender.as_ref().unwrap().send(job).unwrap();
}
}
impl Drop for ThreadPool {
fn drop(&mut self) {
drop(self.sender.take());
for worker in &mut self.workers {
println!("Shutting down worker {}", worker.id);
if let Some(thread) = worker.thread.take() {
thread.join().unwrap();
}
}
}
}
struct Worker {
id: usize,
thread: Option<thread::JoinHandle<()>>,
}
impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
let thread = thread::spawn(move || {
loop {
let message = receiver.lock().unwrap().recv();
match message {
Ok(job) => {
println!("Worker {id} got a job; executing.");
job();
}
Err(_) => {
println!("Worker {id} disconnected; shutting down.");
break;
}
}
}
});
Worker {
id,
thread: Some(thread),
}
}
}