Installing the Rust compiler toolchain
Let's start by installing the Rust compiler toolchain. Rust has three official channels: stable, beta, and nightly. The Rust language uses Git as its version control system. People add new features and bug fixes to the master branch. Every night, the source code from the master branch is compiled and released to the nightly channel. After six weeks, the code will be branched off to the beta branch, compiled, and released to the beta channel. People will then run various tests in the beta release, most often in their CI (Continuous Integration) installation. If a bug is found, the fix will be committed to the master branch and then backported to the beta branch. Six weeks after the first beta branch-off, the stable release will be created from the beta branch.
We will use the compiler from the stable channel throughout the book, but if you feel adventurous, you can use the other channels as well. There's no guarantee the program we're going to create will compile if you use another channel though because people add new features and there might be regression introduced in the new version.
There are several ways to install the Rust toolchain in your system, such as bootstrapping and compiling it from scratch or using your OS package manager. But, the recommended way to install the Rust toolchain in your system is by using rustup
.
The definition on its website (https://rustup.rs) is very simple: "rustup is an installer for the systems programming language Rust." Now, let's try following these instructions to install rustup
.
Installing rustup on the Linux OS or macOS
These instructions apply if you are using a Debian 10 Linux distribution, but if you are already using another Linux distribution, we're going to assume you are already proficient with the Linux OS and can adapt these instructions suitable to your Linux distribution:
- Open your terminal of choice.
- Make sure you have cURL installed by typing this command:
curl
- If cURL is not installed, let's install it:
apt install curl
If you are using macOS, you will most likely already have cURL installed.
- After that, follow the instructions on https://rustup.rs:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- It will then show a greeting and information, which you can customize; for now, we're just going to use the default setup:
... 1) Proceed with installation (default) 2) Customize installation 3) Cancel installation >
- Type
1
to use the default installation. - After that, reload your terminal or type this in the current terminal:
source $HOME/.cargo/env
- You can confirm whether the installation was successful or not by typing
rustup
in the Terminal and you should see the usage instruction for rustup. - Now, let's install the stable Rust toolchain. Type the following in the terminal:
rustup toolchain install stable
- After the toolchain has been installed into your OS, let's confirm whether we can run the Rust compiler. Type
rustc
in the terminal and you should see the instructions on how to use it.
Installing a different toolchain and components
Right now, we have the stable toolchain installed, but there are two other default channels that we can install: nightly and beta.
Sometimes, you might want to use a different toolchain for various reasons. Maybe you want to try a new feature, or maybe you want to test regression in your application against an upcoming version of Rust. You can simply install it by using rustup
:
rustup toolchain install nightly
Each toolchain has components, some of which are required by the toolchain, such as rustc
, which is the Rust compiler. Other components are not installed by default, for example, clippy
, which provides more checks not provided by the rustc
compiler and gives code style suggestions as well. To install it is also very easy; you can use rustup component add <component>
as shown in this example:
rustup default stable rustup component add clippy
Updating the toolchain, rustup, and components
The Rust toolchain has a regular release schedule of around every three months (six weeks plus six weeks), but sometimes there's an emergency release for a major bug fix or a fix for a security problem. As a result, you sometimes need to update your toolchain. Updating is very easy. This command will also update the components installed in the toolchain:
rustup update
Besides the toolchain, rustup
itself might also be updated. You can update it by typing the following:
rustup self update
Now that we have the Rust compiler toolchain installed in our system, let's write our first Rust program!