Skip to content

Rust - Environment Setup

Setting up a modern Rust development environment is straightforward thanks to rustup, the official tool for managing Rust versions and associated tools. This guide will walk you through the process on any major operating system.

rustup installs the Rust compiler (rustc), the package manager and build tool (cargo), the standard library documentation (rust-doc), and more.

Open a terminal and run the following command. It will download a script and start the installation.

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

You’ll be prompted to choose an installation type. The default option (1) is recommended for most users. If the installation is successful, you will see:

Rust is installed now. Great!

The installer will also attempt to configure your shell’s PATH environment variable. You may need to restart your terminal or run source $HOME/.cargo/env for the changes to take effect.

Rust on Windows requires the Microsoft C++ Build Tools. You do not need the full Visual Studio IDE.

  1. Go to the official Visual Studio downloads page and get the “Build Tools for Visual Studio”.
  2. Run the installer. In the “Workloads” tab, select “C++ build tools” and ensure the latest Windows SDK and C++ toolset are selected on the right-hand side. Then click Install.
  3. Once the build tools are installed, visit the official Rust website and download rustup-init.exe.
  4. Run rustup-init.exe. It will guide you through the installation process. The default settings are fine for most users.

Once installed, open a new terminal and check the versions of the installed tools. Your version numbers may be different, which is perfectly normal.

# Check the Rust compiler version
$ rustc --version
rustc 1.78.0 (9b00956e5 2024-04-29)
# Check the Cargo version
$ cargo --version
cargo 1.78.0 (54d881520 2024-04-01)

Step 3: Your First Rust Program with Cargo

Section titled “Step 3: Your First Rust Program with Cargo”

Cargo is the heart of the Rust ecosystem. It handles creating projects, compiling code, managing dependencies, running tests, and more. You should always use Cargo to create new Rust projects.

  1. Create a new project: cargo new hello_world
  2. Navigate into the project directory: cd hello_world
  3. Build the project: cargo build
  4. Run the project: cargo run
$ cargo new hello_world
Created binary (application) `hello_world` package
$ cd hello_world
$ cargo run
Compiling hello_world v0.1.0 (/path/to/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.50s
Running `target/debug/hello_world`
Hello, world!

Get familiar with these commands as you’ll use them daily:

  • cargo new <name>: Creates a new executable Rust project.
  • cargo new <name> --lib: Creates a new library project.
  • cargo build: Compiles your project in debug mode.
  • cargo build --release: Compiles your project with optimizations for production.
  • cargo run: Compiles and runs your project.
  • cargo check: Quickly checks your code for errors without producing an executable.
  • cargo test: Runs all the tests in your project.
  • cargo fmt: Formats your code according to the official Rust style guidelines.
  • cargo clippy: A powerful linter that catches common mistakes and suggests improvements.

Rust has a rapid, 6-week release cycle. To update your Rust toolchain to the latest version, simply run:

$ rustup update

For quick experiments without a local setup, you can use an online environment like the official Rust Playground or the Tutorialspoint Coding Ground. These are great for testing small code snippets and sharing code.