The Rust Programming Language 6 - 10
The Rust Programming Language 6 - 10
best practices for sharing your libraries with others. Chapter 15 discusses smart pointers
that the standard library provides and the traits that enable their functionality.
In Chapter 16, we’ll walk through different models of concurrent programming and talk
about how Rust helps you to program in multiple threads fearlessly. Chapter 17 looks at
how Rust idioms compare to object-oriented programming principles you might be familiar
with.
Chapter 18 is a reference on patterns and pattern matching, which are powerful ways of
expressing ideas throughout Rust programs. Chapter 19 contains a smorgasbord of
advanced topics of interest, including unsafe Rust, macros, and more about lifetimes, traits,
types, functions, and closures.
In Chapter 20, we’ll complete a project in which we’ll implement a low-level multithreaded
web server!
Finally, some appendices contain useful information about the language in a more
reference-like format. Appendix A covers Rust’s keywords, Appendix B covers Rust’s
operators and symbols, Appendix C covers derivable traits provided by the standard library,
Appendix D covers some useful development tools, and Appendix E explains Rust editions.
In Appendix F, you can find translations of the book, and in Appendix G we’ll cover how Rust
is made and what nightly Rust is.
There is no wrong way to read this book: if you want to skip ahead, go for it! You might have
to jump back to earlier chapters if you experience any confusion. But do whatever works for
you.
An important part of the process of learning Rust is learning how to read the error messages
the compiler displays: these will guide you toward working code. As such, we’ll provide many
examples that don’t compile along with the error message the compiler will show you in
each situation. Know that if you enter and run a random example, it may not compile! Make
sure you read the surrounding text to see whether the example you’re trying to run is meant
to error. Ferris will also help you distinguish code that isn’t meant to work:
Ferris Meaning
In most situations, we’ll lead you to the correct version of any code that doesn’t compile.
https://doc.rust-lang.org/book/print.html 6/625
11/14/24, 8:42 PM The Rust Programming Language
Source Code
The source files from which this book is generated can be found on GitHub.
https://doc.rust-lang.org/book/print.html 7/625
11/14/24, 8:42 PM The Rust Programming Language
Getting Started
Let’s start your Rust journey! There’s a lot to learn, but every journey starts somewhere. In
this chapter, we’ll discuss:
https://doc.rust-lang.org/book/print.html 8/625
11/14/24, 8:42 PM The Rust Programming Language
Installation
The first step is to install Rust. We’ll download Rust through rustup , a command line tool
for managing Rust versions and associated tools. You’ll need an internet connection for the
download.
Note: If you prefer not to use rustup for some reason, please see the Other Rust
Installation Methods page for more options.
The following steps install the latest stable version of the Rust compiler. Rust’s stability
guarantees ensure that all the examples in the book that compile will continue to compile
with newer Rust versions. The output might differ slightly between versions because Rust
often improves error messages and warnings. In other words, any newer, stable version of
Rust you install using these steps should work as expected with the content of this book.
In this chapter and throughout the book, we’ll show some commands used in the
terminal. Lines that you should enter in a terminal all start with $ . You don’t need to
type the $ character; it’s the command line prompt shown to indicate the start of each
command. Lines that don’t start with $ typically show the output of the previous
command. Additionally, PowerShell-specific examples will use > rather than $ .
If you’re using Linux or macOS, open a terminal and enter the following command:
The command downloads a script and starts the installation of the rustup tool, which
installs the latest stable version of Rust. You might be prompted for your password. If the
install is successful, the following line will appear:
You will also need a linker, which is a program that Rust uses to join its compiled outputs
into one file. It is likely you already have one. If you get linker errors, you should install a C
https://doc.rust-lang.org/book/print.html 9/625
11/14/24, 8:42 PM The Rust Programming Language
compiler, which will typically include a linker. A C compiler is also useful because some
common Rust packages depend on C code and will need a C compiler.
$ xcode-select --install
Linux users should generally install GCC or Clang, according to their distribution’s
documentation. For example, if you use Ubuntu, you can install the build-essential
package.
The rest of this book uses commands that work in both cmd.exe and PowerShell. If there are
specific differences, we’ll explain which to use.
Troubleshooting
To check whether you have Rust installed correctly, open a shell and enter this line:
$ rustc --version
You should see the version number, commit hash, and commit date for the latest stable
version that has been released, in the following format:
If you see this information, you have installed Rust successfully! If you don’t see this
information, check that Rust is in your %PATH% system variable as follows.
In PowerShell, use:
https://doc.rust-lang.org/book/print.html 10/625