Lesson 1: Installing Rust

There are three main pieces of software that are needed for developing Rust code.

  • rustc is the Rust language compiler. It turns .rs files into executables.
  • cargo is the Rust package manager. It downloads library dependencies and can call rustc for you.
  • rustup is the rust toolchain manager, it installs the above components for you, keeps them up to date, and can dynamically change your toolchain version to comply with the version requirements of the project you are building.

Those are mandatory to have a good development experience, but you probably also want the following.

  • rustfmt formats your code to comply with the format used by the standard library. You can customise this format, but for the sake of everyone who has to read your code please don't.
  • rust-analyzer is the Rust language server. This is used by your editor of choice to provide you with inlay hints, error messages, code navigation, and many other quality of life improvements.
  • clippy is a linter which can check your code for common bad practice and likely errors. It is significantly more useful than the paperclip it is named after.

Fortunately, all of these tools can be installed by rustup, so you only need to install that and the rest can be bootstrapped from there.

You can install this from your system package manager of choice, or you can follow the instructions on https://rustup.rs for your system.

If you are using windows, I strongly recommend you install a WSL2 distribution and use that instead. I'm sure you can use google to figure out how to do this. Most WSL users are using Ubuntu, I personally quite like Fedora.

Once you have rustup installed, you will need to chose a toolchain version to load. I recommend you set your system default toolchain with:

rustup default stable

By default, rustup uses the default profile, which will install all of the components I listed above except for Rust Analyzer. You can remedy this with:

rustup component add rust-src rust-analyzer

Alternatively, you can create a rust-toolchain.toml in your project directory to specific a toolchain which will overwrite the system default chain for commands run in that directory.

# rust-toolchain.toml

[toolchain]
channel = "1.70.0"
components = [ "rust-src", "rust-analyzer" ]

If you are using Nix for reproducibility, which you should be if you're a company, have a look at this example flake you can see more examples or learn about Nix on https://zero-to-nix.com/. If you're not a company that needs reproducibility then please skip over that link before you fall down a software engineering rabbit hole. Do return to explore the rabbit hole later though, as it's quite a fun one.

Once you've got your toolchain installed and configured, you should be able to run the following commands:

rustup --version
rustc --version
cargo --version
cargo clippy --version
cargo fmt --version
rust-analyzer --version

If you don't get a sane output from any of those, please reread the lesson and check you didn't miss anything then file an issue on github.

If you do get a sane output from each of those commands, you have finished this lesson. Congratulations. Now it's time to do a lot of theory learning before you use any of the tools we've installed!