Installing the Rust compiler and toolchain
The Rust toolchain has two major components: the compiler, rustc, and the package manager, cargo, which helps manage Rust projects. The toolchain comes in three release channels:
- Nightly: The daily successful build from the master development branch. This contains all the latest features, many of which are unstable.
- Beta: This is released every six weeks. A new beta branch is taken from nightly. It contains only features that are flagged as stable.
- Stable: This is released every six weeks. The previous beta branch becomes the new stable release.
Developers are encouraged to use the stable release channel. However, the nightly version enables bleeding edge features, and some libraries and programs require it. You can change to the nightly toolchain easily with rustup. We'll see how we can do that in a moment.
Using rustup.rs
Rustup is a tool to that installs the Rust compiler on all supported platforms. To make it easier for developers on different platforms to download and use the language, the Rust team developed rustup. It's a command-line tool written in Rust that provides an easy way to install pre-built binaries of the compiler and binary builds of the standard library for cross compiling needs. It can also install other components, such as the Rust source code, documentation, Rust formatting tool (rustfmt), Rust Language Server (RLS for IDEs), and other developer tools, and it runs on all platforms, including Windows.
From their official page at https://rustup.rs, the recommended way to install the toolchain is to run the following command:
curl https://sh.rustup.rs -sSf | sh
By default, the installer installs the stable version of the Rust compiler, its package manager, Cargo, and the language's standard library documentation so that it can be viewed offline. These are installed by default under the ~/.cargo
directory. Rustup also updates your PATH
environment variable to point to this directory.
The following is a screenshot of running the preceding command on Ubuntu 16.04:
If you need to make any changes to your installation, choose 2
. However, the defaults are fine for us, so we'll go ahead and choose 1
. Here's the output after the installation:
Rustup also has other capabilities, such as updating the toolchain to the latest version, which can be done by running rustup update
. It can also update itself via rustup self update
. It also provides directory-specific toolchain configuration. The default toolchain is set globally to whatever toolchain gets installed, which in most cases is the stable toolchain. You can view the default one by invoking rustup show
. If you want to use the latest nightly toolchain for one of your projects, you can tell rustup to switch to nightly for that particular directory by running rustup override set nightly
. If, for some reason, someone wants to use an older version of the toolchain or downgrade (say, the nightly build on 2016-06-03), rustup can also download that if we were to run rustup install nightly-2016-06-03
, followed by setting the same using the override
sub-command. More information on rustup can be found at https://github.com/rust-lang-nursery/rustup.rs.
Note
Note: All of the code examples and projects in this book are based on compiler version rustc 1.32.0 (9fda7c223 2019-01-16)
.
Now, you should have everything you need to compile and run programs written in Rust. Let's get Rusty!