System Setup
The first thing we need to talk about is setting up your system so you can write, compile, and run Rust programs.
Installing Rust: rustup
Rust has a streamlined process of maintaining all its software packages. It is all done by a tool
called rustup
. Head over to the Rust book on
installation and follow the instructions
there to install rustup
and other software for Rust.
rustup
has many features to support Rust development, and it has its own
book.
By default, rustup
installs the latest stable version of Rust. However, this course later uses
some experimental features of Rust. Thus, it is necessary to install "nightly" Rust that supports
those experimental features. The following command will do just that.
$ rustup toolchain install nightly
If you are interested, you can read about various release channels of Rust (nightly, beta, and stable).
If later you determine that you want to use nightly Rust by default rather than stable Rust, you can do it as follows.
$ rustup default nightly
If you want to switch the default back to stable, you can do it as follows.
$ rustup default stable
If you want to list all installed versions of Rust, you can do the following.
$ rustup toolchain list
At this point, it will print out two versions, the latest stable version and the latest nightly version.
The following command updates the stable and nightly versions that you have.
$ rustup update
However, if you start using nightly Rust, it is important to not break the installation because your
code might depend on a particular version of Rust. Thus, rustup
allows you to install a specific
version of Rust based on its date. For example, the following command installs a nightly Rust based
on the source from 2021-09-01.
$ rustup toolchain install nightly-2021-09-01
Writing Rust Programs: Editors
Once you have a working installation, you need to have a proper editor/IDE to write a Rust program. There are many options available to choose from, but whatever editor/IDE you use, it is important to enable Rust plugins. Rust plugins support syntax highlighting, code formatting, error detection, etc., which will make your life easier.
- Vim/Neovim: If you use Vim or Neovim, follow the instructions here and use the combination of rust-analyzer, coc.vim, and one of the LSP clients listed in the instructions. This is the personal setup that the instructor uses for Rust development (with ALE as the LSP client).
- Emacs: You can also use rust-analyzer for Emacs. Here are the instructions.
- VS Code: VS Code has an official Rust plugin. Open VS Code, go to Extensions, and search Rust. You can also use rust-analyzer with VS Code.
- CLion: CLion is the only true IDE among the options listed here. You can get a free student license. CLion can use the IntelliJ Rust plugin.
Make sure that you install and enable
rustfmt
with your Rust plugins (e.g., on save). That way, it will automatically format your code.
Compiling Rust Programs: cargo
Once you set up an editor/IDE, you are ready to write Rust programs. Although it is possible to
create Rust source files and compile them with the Rust compiler (rustc
), you are most likely not
going to do that. Instead. cargo
is the command that you will use most of the time. Head over to
the Rust book on cargo
to learn about
cargo
.
One thing to add, due to our prior discussion on using nightly Rust, is that you can easily choose
which Rust version to use when compiling with cargo
. For example, the following command compiles
your code using nightly Rust even if you do not set it as the default.
$ cargo +nightly build
The same +
syntax works for rustc
as well.
It is also possible to set the version of Rust that you want to use for a specific directory by
using rustup
. For example, the following commands will set the default to nightly Rust for the
directory tmp/
.
$ cd tmp
$ rustup override set nightly