CourseLesson 2 · Installing Rust and the Cargo Workflow

Lesson 02

Installing Rust and the Cargo Workflow

What Rust Is

Rust is a compiled programming language. You write your program in a text file. A program called the compiler reads that file and produces a single executable file. You then run that executable. Nothing else has to be installed on the machine that runs it.

Rust checks a lot of things before it produces the executable. It checks the types. It checks that every variable holds a valid value. It checks that no part of the program uses memory that has already been given back to the system. If a check fails, the compiler refuses to build the program and prints an error message.

Key idea

In Rust most mistakes are caught at compile time, not while the program runs. A Rust program that compiles is far less likely to crash than a program in a language that does fewer checks.

The Three Programs

Installing Rust gives you three command line programs. You will use all three, but not equally often.

ProgramWhat it does
rustupInstalls Rust and keeps it up to date. You run it a few times a year.
rustcThe compiler. It turns one source file into one executable.
cargoThe build manager. It calls rustc for you, keeps track of your files, downloads libraries and runs your tests. You use this every day.

Installing Rust

On Linux and macOS, open a terminal and run this one command.

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

The command downloads the rustup installer and runs it. The installer asks what you want to do. Press 1 and then Enter to accept the standard installation.

On Windows, download and run rustup-init.exe from https://rustup.rs instead. The installer may ask you to install the Microsoft C++ build tools first. Say yes: Rust uses the system linker to produce the final executable.

The installer puts the programs in ~/.cargo/bin and adds that folder to your PATH, so the shell can find them by name. This change only takes effect in a new terminal. Close the terminal and open a new one, or run this command.

source "$HOME/.cargo/env"

Checking the Installation

Ask each program for its version number. If all three answer, the installation worked.

rustc --version
cargo --version
rustup --version
rustc 1.94.1 (e408947bf 2026-03-25)
cargo 1.94.1 (29ea6fb6a 2026-03-24)
rustup 1.29.0 (28d1352db 2026-03-05)

Your version numbers will be higher than these. Rust releases a new version every six weeks. To move to the newest one, run rustup update.

Compiling One File with rustc

Start with the compiler on its own. Create a file called greet.rs and put these three lines in it. Rust source files end in .rs.

fn main() {
    println!("Hello from rustc!");
}
  • fn starts a function. This one is called main. Every Rust program starts running at main.

  • The body of the function sits between the braces { and }.

  • println! prints a line of text to the screen. The exclamation mark is part of the name. It marks println! as a macro, which is a piece of code the compiler expands before it compiles.

  • The line ends with a semicolon.

Now compile it and run the result.

rustc greet.rs
./greet
Hello from rustc!

rustc greet.rs produces an executable file called greet in the same folder. ./greet runs it. The ./ in front means “the file called greet in the current folder”.

This works for one file. A real program has many files, and often uses code written by other people. Typing the right rustc command by hand every time becomes hard. That is the job cargo does for you.

Creating a Project with cargo new

cargo new creates a new project folder with everything already in place.

cargo new hello
    Creating binary (application) `hello` package

It creates three files inside a new folder called hello.

hello
  Cargo.toml
  src
    main.rs
  • Cargo.toml describes the project: its name, its version, and the libraries it needs.

  • src holds the source code. All your .rs files go here.

  • src/main.rs is the file that holds main. This is where the program starts.

cargo new also starts a Git repository in the folder. Move into the project before you run any other cargo command.

cd hello

The Cargo.toml File

[package]
name = "hello"
version = "0.1.0"
edition = "2024"

[dependencies]

The name is the name of the program, and the name of the executable cargo will build. The edition says which release of the Rust language this project is written in. Under [dependencies] you list libraries your project uses. A new project uses none, so the list is empty.

The main.rs File

cargo new writes a working program for you.

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

Building and Running

cargo run compiles the project and then runs the program it produced.

cargo run
   Compiling hello v0.1.0 (/home/student/hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.78s
     Running `target/debug/hello`
Hello, world!

The first three lines come from cargo. The last line is your program’s output. cargo tells you what it is doing at each step: it compiled the package, it finished, and then it ran the executable at target/debug/hello.

cargo build does the first part only. It compiles the project and stops.

cargo build
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s

This build printed no Compiling line and took no time. Nothing in the source changed since the last build, so cargo had no work to do. It only rebuilds what changed.

Everything cargo produces goes into a folder called target. The executable itself is target/debug/hello, and you can run it directly.

./target/debug/hello

Debug builds and release builds

The word debug in the path means the program was built for development. It compiles quickly and keeps the information a debugger needs, but it runs slowly. When you want the fast version, run cargo build -{}-release. That executable lands in target/release instead. It takes longer to build, and it can run many times faster.

Checking Without Building

cargo check runs the compiler’s checks and reports any errors, but it does not produce an executable.

cargo check
    Checking hello v0.1.0 (/home/student/hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s

Producing the executable is the slow part of a build. cargo check skips it, so it answers much faster than cargo build. Use it while you are writing code and only want to know whether it compiles. Use cargo run when you want to see the program work.

Reading a Compiler Error

Errors are normal. The Rust compiler is strict, and it explains itself carefully. Change src/main.rs to leave out one semicolon.

fn main() {
    let name = "Rust"
    println!("Hello, {name}!");
}
cargo check
error: expected `;`, found `println`
 --> src/main.rs:2:22
  |
2 |     let name = "Rust"
  |                      ^ help: add `;` here
3 |     println!("Hello, {name}!");
  |     ------- unexpected token

error: could not compile `hello` (bin "hello") due to 1 previous error

Read the message from the top.

  • The first line says what is wrong. The compiler expected a semicolon and found the word println instead.

  • The arrow --> gives the position: file src/main.rs, line 2, character 22.

  • The compiler then prints the lines themselves, with a mark under the exact spot, and often a suggested fix.

Put the semicolon back at the end of line 2 and the program compiles.

Running Tests

A test is a function that checks that another function gives the right answer. cargo test builds your project and runs every test in it. Put this in src/main.rs.

fn double(n: i32) -> i32 {
    n * 2
}

fn main() {
    println!("double(21) = {}", double(21));
}

#[test]
fn double_works() {
    assert_eq!(double(21), 42);
}
  • fn double(n: i32) -> i32 declares a function named double. It takes one number called n and gives back a number. i32 is a whole number stored in 32 bits.

  • #[test] marks the function under it as a test. Only cargo test runs it. It is left out of the normal build.

  • assert_eq! compares two values. If they are equal the test passes. If they differ the test fails and prints both values.

cargo test
   Compiling hello v0.1.0 (/home/student/hello)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.16s
     Running unittests src/main.rs (target/debug/deps/hello-d13472da73f5d32d)

running 1 test
test double_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

One test ran and it passed. Change the 42 to 43 and run cargo test again to see what a failure looks like.

Command Reference

CommandWhat it does
rustup updateInstall the newest version of Rust.
rustc file.rsCompile one file into an executable.
cargo new nameCreate a new project folder called name.
cargo checkCheck the code for errors. Build no executable.
cargo buildCompile the project into target/debug.
cargo build -{}-releaseCompile the fast version into target/release.
cargo runCompile the project and run it.
cargo testCompile the project and run every test.
cargo cleanDelete the target folder.