---
title: "Install Rust and run your first program"
slug: "hello-cargo"
description: "Install Rust with rustup, create a project with cargo new and run it with cargo run. See what fn main and println! do and how run, build and check differ."
published: "2026-12-03"
updated: "2026-12-03"
canonical: "https://aifree.dev/courses/learn-rust/hello-cargo"
---

# Install Rust and run your first program

How do you get from nothing to a running Rust program?

## Predict

```rust
fn main() {
    println!("Hello");
    println!("{} + {} = {}", 2, 3, 2 + 3);
}
```

What does this program print?

- Hello 2 + 3 = 5 on one line
- Hello, then 2 + 3 = 5 on the next line
- Hello, then {} + {} = {}

Answer: Hello, then 2 + 3 = 5 on the next line — Each {} takes the next argument in order, and println! adds the newline.

Output:

```
Hello
2 + 3 = 5
```

Two calls, two lines. The three {} are filled with 2, 3 and the result of 2 + 3, in order.

## Install Rust

Install Rust with **rustup**, the installer and version manager the Book recommends. On Linux and macOS you run one command from rustup.rs; on Windows you download the installer. Then open a new terminal and type `rustc --version`. If it prints a version number, Rust is installed. `rustup update` later moves you to a newer release.

## Make a project with Cargo

**Cargo** is Rust's build system and package manager, and it comes with the installer. `cargo new hello` makes a folder with a `Cargo.toml` file, which names the package and lists its dependencies, and `src/main.rs`, which holds a "Hello, world!" program. `cargo run` builds and runs it. `cargo build` only builds. `cargo check` only checks that the code compiles, and is usually faster.

## A first program with println!

```rust
fn main() {
    let name = "Ferris";
    let age = 4;
    println!("Hello, {name}!");
    println!("{} is {} years old", name, age);
    println!("Next year: {}", age + 1);
}
```

Output:

```
Hello, Ferris!
Ferris is 4 years old
Next year: 5
```

`{name}` puts a variable into the text. A plain `{}` takes the next argument, so `{} is {} years old` uses `name` and then `age`. You can also put an expression such as `age + 1` in the arguments.

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=da05ba407e3e1034d5fcc43c67ca7e60

## What the ! and the ; mean

`fn main()` is the function Rust runs first. The `!` in `println!` means you call a **macro**, not a normal function; write `println` without it and you call something else. A statement ends with `;`, as the `println!` lines do. The Book formats code with four spaces, not a tab.

## Fix it

```rust
fn main() {
    println("Hello, Rust");
}
```

This program does not compile. What is the bug?

- fn main needs a return type
- println is a macro and needs the ! after its name
- The text must be wrapped in single quotes

Answer: println is a macro and needs the ! after its name — Without the !, Rust looks for a function named println and finds none.

Compiler message:

```
error[E0423]: cannot find function `println` in this scope
```

Fixed:

```rust
fn main() {
    println!("Hello, Rust");
}
```

Output:

```
Hello, Rust
```

## Say it back

In your own words: what is the difference between cargo run and cargo build?

Our sentence: `cargo run` builds the project if needed and then starts the program; `cargo build` only builds it and leaves the executable in `target/debug`.

## Mission (8 min)

Install Rust, check the version, make a project called hello and run the program Cargo generated. Then change the text in src/main.rs and run it again.

```sh
rustc --version
cargo new hello
cd hello
cargo run
```

## Common questions

### How do I install Rust?

Use rustup, the installer the Rust Book recommends. On Linux and macOS you run the command shown on rustup.rs in a terminal; on Windows you download and run the installer. When it finishes, open a new terminal and type rustc --version. A printed version number means Rust works. Later, rustup update installs the newest release.

### What is Cargo in Rust?

Cargo is the build system and package manager that comes with Rust. It creates projects with cargo new, builds them, runs them and downloads the libraries your code depends on. Almost every Rust project uses it, so the commands you learn here, such as cargo run, work the same on Linux, macOS and Windows.

### What is the difference between cargo run, cargo build and cargo check?

The cargo build command compiles your project and leaves an executable in the target/debug folder. The cargo run command builds the project if needed and then starts it. The cargo check command only checks that the code compiles and makes no executable, so it is usually faster while you are still writing the code.

## Sources

- https://doc.rust-lang.org/book/ch01-01-installation.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch01-02-hello-world.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch01-03-hello-cargo.html (read 2026-12-03)

Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03.
