Get Started with Rust and Set up VS Code.

You can get pretty far with Rust using the Rust Playground or even just using GitHub Actions if you really need to.

Eventually though, you’ll want to have Rust installed on your computer. This will make the feedback loop between writing your program and compiling or running it much shorter.

Let’s get Rust installed and then talk about setting up the right VS Code extensions and Cargo subcommands so that you have a productive Rust environment.

To install Rust we’ll first install Rustup from rustup.rs. If you’re coming from node.js, Rustup is similar to nvm except that it is an official Rust project, so you don’t have to choose between Rustup and 3 other options.

Rustup is installed via a curl command on Mac and Linux, so we’ll grab a terminal window. In this case I’m on a fresh m1 macbook pro and have opened the Terminal app.

On Windows, you can download the .exe.

After executing the curl command we’re met with a “Welcome to Rust” message. I suggest reading this so you’re aware of the various directories Rust will store configurations in.

There’s a directory for Rustup, a directory for Cargo and a directory for any binaries we install via Cargo from crates.io.

The message also lets us know which shell startup files it’s going to modify to add the Cargo binaries directory to our PATH. This is what lets us have access to binaries installed from Cargo when we type their names.

We are given three options: Proceed with the installation, customize the installation, or cancel. We’re going to proceed with the default installation today.

Rustup then sets the default toolchain to use to the one that is most reasonable for your computer. I have an m1 mac, so the toolchain that’s being installed for me as my default toolchain is stable-aarch64-apple-darwin.

This identifier includes the release channel and the target triple for your platform.

Rust has three release channels: Stable, Beta, and Nightly so what this means is that we’re installing the latest stable Rust release for the m1 mac.

Rustup will download a series of components for this toolchain as well, which includes a series of tools that are useful for developing Rust.

In this case we’ve downloaded:

  • Cargo, the package manager
  • Clippy, the linter. Comparable to ESLint.
  • rustc, the Rust compiler
  • rustfmt, the Rust code autoformatter. Comparable to prettier.

We won’t cover the full list of components today. Though it is worth knowing that you can choose to install more or fewer components if you want to save space or take advantage of different tools.

After installing Rustup and our first version of Rust we need to either run the source command that’s printed out or open a new shell so that we have access to the newly installed components.

You can type rustc --version and cargo --version to ensure that you’ve installed Rust correctly.


Now that we have Rust installed, we should also set up VS Code (or your favorite editor of choice) to use Rust Analyzer.

Rust Analyzer is an editor extension adds superpowers to your Rust development workflow.

Rust Analyzer can write code for you, organize your imports at the top of a file, tell you where a problem is in your source code, and my favorite, show you the types of everything in your application using inlay hints.

In VS Code, go to the extensions store and pick Rust Analyzer.

A short aside: There is an older extension called “Rust” that used to be the recommended extension. You should use Rust Analyzer instead, and only Rust Analyzer. Disable the Rust extension if you’ve installed it previously as the two extensions can conflict if they’re both enabled.

I also use the crates extension for automatically checking the versions of dependencies in my Cargo.toml, and the even better toml extension for .toml syntax highlighting. These are both very optional.


Additionally, Cargo allows us to extend its behavior using binaries we install from crates.io. Here’s a few I use to make my life easier.

cargo-edit

If you’re used to using “npm install” or “yarn add” to add dependencies to your package.json, you’ll want to make sure you install cargo-edit, which adds the add , remove, and upgrade subcommands, as well as the cargo set-version subcommand.

cargo-watch

cargo-watch adds the ability to “watch” your files to recompile when something changes in your project. The most awesome part of this plugin is the ability to chain multiple commands. For example whenever a change is made, you could typecheck using cargo check and if that succeeds, run cargo test like this.

cargo watch -x check -x test

cargo-outdated

cargo-outdated will give you a bunch of information about the dependencies in your project and which versions are currently available.