Rust Get Started

Getting started with Rust means installing rustup, understanding cargo, and compiling your first program.

Installing Rust with rustup

The recommended way to install Rust is through rustup, the official toolchain installer and version manager. Rustup installs the Rust compiler (rustc), the package manager and build tool (cargo), and standard library documentation, and it makes it easy to update or switch between stable, beta, and nightly releases.

Install Rust (macOS/Linux)

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

On Windows, you download and run rustup-init.exe from the official Rust website, which walks through the same setup, including installing the Microsoft C++ build tools if they are not already present.

Verifying the Installation

After installation completes, open a new terminal and check that both the compiler and cargo are available. This confirms rustup added them to your system PATH correctly.

Check installed versions

rustc --version
cargo --version

Understanding Cargo

Cargo is Rust's build system and package manager, roughly equivalent to npm for JavaScript or pip combined with a build tool for Python. Almost every real Rust project is managed through cargo rather than by invoking rustc directly.

  • cargo new creates a new project with a standard folder layout
  • cargo build compiles the project into an executable
  • cargo run compiles and immediately runs the executable
  • cargo check quickly verifies the code compiles without producing a binary
  • cargo test runs the project's test suite
  • cargo add adds a dependency from crates.io, the Rust package registry

Creating Your First Project

Running cargo new generates a directory containing a Cargo.toml manifest file (which lists dependencies and project metadata) and a src folder with a starter main.rs file already containing a working 'Hello, world!' program.

Create and run a project

fn main() {
    println!("Hello, world!");
}

A project with a function

fn greet(name: &str) {
    println!("Welcome to Rust, {}!", name);
}

fn main() {
    greet("Developer");
}

Compiling manually with rustc

fn main() {
    let version_note = "Compiled directly with rustc, no cargo project needed";
    println!("{}", version_note);
}
CommandPurpose
cargo new my_appScaffold a new binary project named my_app
cargo build --releaseCompile with full optimizations for production
cargo runBuild and execute in one step during development
cargo fmtAutomatically format code to the standard style
cargo clippyLint code for common mistakes and non-idiomatic patterns
Note: Always run cargo build --release before measuring performance or shipping a binary. The default debug build is unoptimized and can be many times slower.
Note: rustup update keeps your toolchain current, which matters because Rust ships a new stable release every six weeks.

Exercise: Rust Get Started

Which tool is commonly used to install and manage Rust versions?