Rust from Zero Module 1: Hello, Rust Lesson 1.4 15 min

Project: start your text adventure game

Use everything from Module 1 to build the opening scene of The Rusty Cave, a text adventure you will grow through the whole course.

Time to build something. Over this course you will make The Rusty Cave, a small text adventure game that runs in the terminal. Each module adds a new feature, using exactly what that module taught you. By the end, it is a real program you can run on your own computer and show off.

The Rusty Cave roadmap: Module 1 the opening scene, 2 choices and a score, 3 rooms as functions, 4 an inventory, 5 the world as types, 6 safe error handling, 7 a map of rooms, 8 a real program built with Cargo

Today: the opening scene. It uses only println!, {} blanks, and variables.

Step 1: a title screen

Start with the title. Run it, then make the title your own.

main.rs Open in Playground
fn main() {
    println!("==============================");
    println!("        THE RUSTY CAVE        ");
    println!("==============================");
}
Expected output
==============================
        THE RUSTY CAVE        
==============================

Step 2: the player and their stats

Games are full of values: a name, health, coins. Store them in variables so you can use them anywhere. Notice which ones are mut: the name will not change during the game, but health and coins will.

main.rs Open in Playground
fn main() {
    let player = "Asha";
    let mut health = 100;
    let mut coins = 0;

    println!("Welcome, {player}.");
    println!("Health: {health}  Coins: {coins}");
}
Expected output
Welcome, Asha.
Health: 100  Coins: 0

Run it. Below the output, the compiler prints two warnings (in yellow): variable does not need to be mutable. Warnings are not errors: the program still runs. Rust is pointing out that we made health and coins changeable but never changed them. We will fix that in Step 4.

Step 3: set the scene

A good opening describes where the player is. Use blanks to put the player’s name into the story.

main.rs Open in Playground
fn main() {
    let player = "Asha";

    println!("You wake up on a cold stone floor, {player}.");
    println!("Water drips somewhere in the dark.");
    println!("A faint orange glow comes from a tunnel to the north.");
}
Expected output
You wake up on a cold stone floor, Asha.
Water drips somewhere in the dark.
A faint orange glow comes from a tunnel to the north.

Step 4: something happens

Now the stats change. The player finds a few coins and scrapes a knee on the way. This is exactly what mut is for.

Predict the output

Before you run the full scene below, predict the last line it prints.

fn main() {
    let mut health = 100;
    let mut coins = 0;
    health -= 5;
    coins += 3;
    println!("Health: {health}  Coins: {coins}");
}
Before you run the full scene below, predict the last line it prints.
Show the real output
Health: 95  Coins: 3

Health starts at 100 and loses 5, so it is 95. Coins start at 0 and gain 3, so there are 3. The final println! shows the values after both changes.

The whole opening scene

Here is everything together. Run it, then make it yours: change the name, the story, the numbers. Add a line. Add a variable.

main.rs Open in Playground
fn main() {
    let player = "Asha";
    let mut health = 100;
    let mut coins = 0;

    println!("==============================");
    println!("        THE RUSTY CAVE        ");
    println!("==============================");
    println!();
    println!("You wake up on a cold stone floor, {player}.");
    println!("Water drips somewhere in the dark.");
    println!("A faint orange glow comes from a tunnel to the north.");
    println!();
    println!("You crawl toward the glow and scrape your knee.");
    health -= 5;
    println!("Something glints in a crack: three old coins!");
    coins += 3;
    println!();
    println!("{:<8}{:>4}", "Health", health);
    println!("{:<8}{:>4}", "Coins", coins);
}
Expected output
==============================
        THE RUSTY CAVE        
==============================

You wake up on a cold stone floor, Asha.
Water drips somewhere in the dark.
A faint orange glow comes from a tunnel to the north.

You crawl toward the glow and scrape your knee.
Something glints in a crack: three old coins!

Health    95
Coins      3

println!(); with nothing inside prints an empty line, a quick way to space things out. The last two lines use the width trick from Lesson 1.2 to line the stats up.

Challenge: make it yours

Pick one or more, run your version, and tell us what you made in the comments.

  1. Add a torch_light variable that starts at 10 and goes down by 2 when the player crawls.
  2. Add a second discovery (a key? a map?) and a line that announces it.
  3. Use shadowing to make a title variable that starts as "THE RUSTY CAVE", then becomes "THE RUSTY CAVE: PART 1", and print it on the title screen.

Quick check

Quick check

  1. Why are health and coins declared with let mut, but player is not?
  2. The compiler printed a warning in Step 2. What does that mean?
  3. What does println!(); print?

Module 1 recap

You can now run Rust programs, print text and values, use placeholders and widths, store values in variables, change them with mut, and reuse names with shadowing. That is enough to build a real opening scene.

Next, Module 2 (coming soon): numbers and text in depth, if and else for choices, and loops. Your game will let the player make a choice.

Leave a Reply

Your email address will not be published. Required fields are marked *