Installing Rust on macOS

Introduction

Rust is a relatively young language in the field of systems programming. Its first stable release came out in 2015, though it was announced back in 2010. Rust blends several programming paradigms — functional, object-oriented, and concurrent — making it easy for programmers from different backgrounds to find familiar concepts. However, those unfamiliar with some paradigms may initially find parts of it challenging.

Installation

The easiest way to install Rust is by using rustup, an official Rust tool. To do this, visit rustup.rs, copy the curl command provided, and run it in your terminal. To uninstall later, simply run:

$ rustup self uninstall

Alternatively, you can download Rust packages for various operating systems from the official Rust website. However, using rustup is strongly recommended, as it’s a powerful tool that simplifies Rust installation and management. For example, updating Rust to the latest version is as easy as running:

$ rustup update

After installing Rust with rustup, be sure to restart your shell.

After Installation

Once Rust is installed, three new commands become available. It’s a good idea to check that everything works by running each command with the --version option:

$ rustc --version
$ rustdoc --version
$ cargo --version
  • rustc is the Rust compiler.
  • rustdoc generates documentation (in HTML format).
  • cargo is the main tool for building and managing Rust packages. While you can compile directly with rustc, it’s usually better to use cargo.

Creating a New Rust Project

With cargo, you can quickly set up the basic structure for a new project or package. The following command creates a new package called “hello”:

$ cargo new hello

This creates a folder named “hello”, containing a Cargo.toml file and a src subfolder. As you might guess, the src directory holds your code files. Initially, there’s just one file: main.rs.

hello
  ├── Cargo.toml
  └── src
      └── main.rs

The main.rs File

Let’s look at the main.rs file inside the src directory:

$ cd hello/src

After navigating to this directory, you can launch Visual Studio Code from the terminal with:

$ code .

The main.rs file contains just three lines of code. It defines a main() function — introduced by the fn keyword — which uses the macro println!() (note the exclamation mark) to print the text “Hello, world!”.

You can run this code in the terminal. In Visual Studio Code, open a terminal via:

> Terminal > New Terminal

Then enter:

$ cargo run

or simply:

$ cargo r

This will compile the code and run the program, displaying “Hello, world!” as output.

Additionally, a new target directory will be created. Inside the debug subfolder, you’ll find the files generated by the compiler, including the executable “hello” (on Windows, “hello.exe”).

To clean up the files created during the build process, run:

$ cargo clean

This deletes the target directory.

If you want to build the project without running it, use:

$ cargo build

Another useful command is:

$ cargo check

This checks for warnings or errors in the code — essentially verifying whether it compiles — but it does not produce an executable.

Now you know how to get started with Rust! To dive deeper, be sure to check out the official documentation.