Skip to content
RustLearn

Module 01 Exercises — Setup, Cargo & Hello World

How to use this page

Attempt every exercise in a real Cargo project before opening a solution — a solution you have not fought for teaches almost nothing. Tick the boxes as you go; progress is saved in this browser.

Before you start, have these three things clear — every drill below leans on them:

  • cargo new / cargo run / cargo check, and what each one does differently;
  • that a compile error means no program was produced, so nothing ran at all;
  • how to read an error message top to bottom, including the help: line.

If any of those are shaky, re-read §1.5 and §1.6 first. Everything here is meant to be typed by hand into a real project — reading the answers costs you the entire benefit.

Part A — Warm-Up Drills

Short reps on the mechanics. Do them until the syntax stops costing you any thought.

🟢 Exercise 1.1 — Toolchain check

Run each of rustc --version, cargo --version, cargo clippy --version, rustup doc --std. Fix anything missing with rustup component add.

🟢 Exercise 1.2 — Formatted output

Write a program that stores your name, your age, and a floating-point height in variables and prints exactly:

Ada Lovelace is 36 years old and 1.65 m tall.

Use inline captured identifiers ({name}), and format the height to exactly 2 decimals.

Solution
rust
fn main() {
    let name = "Ada Lovelace";
    let age = 36;
    let height = 1.65;
    println!("{name} is {age} years old and {height:.2} m tall.");
}

Part B — Core Exercises

The real practice. Attempt each one fully — compiler errors included — before opening a solution.

🟡 Exercise 1.3 — Cargo project + argument

Create a project greeter. Make cargo run -- Ferris print Hello, Ferris!, and cargo run (no argument) print Hello, world!.

Hint: std::env::args() yields an iterator of String; .nth(1) gives the first real argument as an Option<String>.

Solution
rust
use std::env;

fn main() {
    let name = env::args().nth(1).unwrap_or_else(|| "world".to_string());
    println!("Hello, {name}!");
}

unwrap_or_else supplies a default when the Option is None. You'll meet Option properly in module 08 — for now, read it as "maybe there's a value".

🟡 Exercise 1.4 — Read three errors

Write three tiny programs that each produce a different compiler error, then run rustc --explain <CODE> for each and write one sentence explaining it in your own words. Suggested targets: E0308 (type mismatch), E0425 (unresolved name), E0384 (reassign an immutable binding).

Part C — Challenges

Longer builds. Expect to fight the borrow checker here; that fight is the lesson.

🔴 Exercise 1.5 — Debug vs release

Write a program that sums 0..200_000_000u64 in a loop. Time it in both profiles:

bash
time cargo run
time cargo run --release

Record the ratio. Then explain (in a comment) why release is dramatically faster and why you must never benchmark debug builds.

Solution sketch
rust
fn main() {
    let mut sum: u64 = 0;
    for i in 0..200_000_000u64 {
        sum = sum.wrapping_add(i);
    }
    println!("{sum}");
}

Expect a 20–100× difference. Debug builds keep every variable in memory for the debugger, skip inlining, and insert integer-overflow checks. Release enables LLVM optimizations — it may even constant-fold the entire loop away.

Self-check

  1. Why does println! end in !?
  2. What does cargo check do that cargo build doesn't skip?
  3. Name the compilation stage that exists in Rust but not in C or TypeScript.
  4. Where does the compiled binary land after cargo build --release?
  5. Which file should you commit for a binary project: Cargo.lock or not?
Answers
  1. It's a macro, expanded at compile time, which allows variadic args and compile-time format-string validation.
  2. cargo check runs parsing, type checking, and borrow checking but skips code generation and linking, so it's much faster.
  3. Borrow checking.
  4. target/release/<name>.
  5. Yes — commit it for binaries so builds are reproducible. (For libraries it's conventionally omitted.)

Next: Module 02 — Variables, Types & Memory Basics