Guessing Game Part 2: VSCode & Completing the Game

Learning objectives

By the end of class today you should be able to:

  • Use VSCode with rust-analyzer and the integrated terminal for Rust development
  • Start using loops and conditional logic in Rust
  • Use match expressions and Ordering for comparisons
  • Keep your code tidy and readable with clippy, comments, and doc strings

Why VSCode for Rust?

  • Rust Analyzer: Real-time error checking, autocomplete, type hints
  • Integrated terminal: No more switching windows
  • Git integration: Visual diffs, staging, commits

Setting up VSCode for Rust

You'll need to have

  • Installed VSCode
  • Installed Rust
  • Installed the rust-analyzer extension

Opening our project

To make sure we're all at the same starting point, we'll recreate the project.

From MacOS terminal or Windows PowerShell (not git-bash):

# change to your projects folder
cd ~/projects

cargo new guessing_game

cd guessing_game

# check what the default branch name is
git branch

# if default branch is called `master`, rename it to `main`
git branch -m master main

# Add the `rand` crate to the project
cargo add rand

# start VS Code in the current directory
code .

or use File → Open Folder from VSCode and open ~/projects/guessing_game.

VSCode Features Demo

Side Panel

  • Explorer
    • single click and double click filenames
    • split editor views
  • Search,
  • Source Control,
  • Run and Debug,
  • Extensions
    • You should have rust-analyzer, not rust!

Integrated Terminal

  • View → Terminal, Terminal → New
  • You can have multiple terminals
  • Same commands as before: cargo run, cargo check

Rust Analyzer in Action

What you'll see:

  • Red squiggles - Compiler errors
  • Yellow squiggles - Warnings
  • Hover tooltips - Type information
  • Autocomplete - As you type suggestions
  • Format on save - Automatic code formatting

Let's see it in action!

Completing Our Guessing Game

Restore Guessing Game

Replace the content in main.rs with the following:

use std::io;
use rand::Rng;

fn main() {
    let secret_number = rand::rng().random_range(1..=100);
    //println!("The secret number is: {secret_number}");

    println!("Guess the number between 1 and 100!");
    println!("Please input your guess.");

    let mut guess = String::new();

    io::stdin()
        .read_line(&mut guess)
        .expect("Failed to read line");

    println!("You guessed: {}", guess);

}

Running from VSCode

You have different ways to run the program:

  1. cargo run from terminal
  2. Click the little Run that decorates above fn main() {

VSCode Git Integration

Visual Git Features:

  • Source Control panel - See changed files
  • Diff view - Side-by-side comparisons
  • Stage changes - Click the + button
  • Commit - Write message and commit

Still use terminal for:

  • git status - Quick overview
  • git log --oneline - Commit history
  • git push / git pull - Syncing

Create Git Commit

Let's use the visual interface to make our initi commit.

You can always do this via the integrated terminal instead.

Click the Source Control icon on the left panel.

Click + to stage each file or stage all changes.

Write the commit message: "Initial commit" and click Commit

Now you can see on the left pane that we have one commit.

Making it a real game:

  1. Remove the secret reveal - no cheating!
  2. Add a loop - keep playing until correct
  3. Compare numbers - too high? too low?
  4. Handle invalid input - what if they type "banana"?

But before we proceed, create a topic branch by

  • clicking on main in the bottom left
  • Select Create new branch...
  • Give it a name like compare

Step 1: Comparing Numbers

First, we need to convert the guess to a number and compare:

#![allow(unused)]
fn main() {
// add at top of file after other `use` statements
use std::cmp::Ordering;

// Add this after reading input:
let guess: u32 = guess.trim().parse().expect("Please enter a number!");

match guess.cmp(&secret_number) {
    Ordering::Less => println!("Too small!"),
    Ordering::Greater => println!("Too big!"),
    Ordering::Equal => println!("You win!"),
}
}

Now run the program to make sure it works.

If it does, then commit the changes to your topic branch.

Note how you can see the changes in the Source Control panel.

Merge Topic Branch

If you had a remote repo setup like on GitHub, you would then:

  • push your topic branch to the remote git push origin branch_name
  • ask someone to review and possible make changes and push those to the remote

But for now, we are just working locally.

git checkout main
# or use VSCode to switch to main

# merge changes from topic branch into main
git merge compare # replace 'compare' with your branch name

# delete your topic branch
git branch -d compare

Step 2: Adding the Loop

Now, we want to wrap the input/comparison in a loop.

But first create a new topic branch, e.g. loop

#![allow(unused)]
fn main() {
loop {
    println!("Please input your guess.");
    
    // ... input code ...
    
    match guess.cmp(&secret_number) {
        Ordering::Less => println!("Too small!"),
        Ordering::Greater => println!("Too big!"),
        Ordering::Equal => {
            println!("You win!");
            break;  // Exit the loop
        }
    }
}
}

You can indent multiple lines of code by selecting all the lines and then pressing TAB.

Try the code and if it works, commit, checkout main, merge topic branch and then delete topic branch.

Step 3: Handling Invalid Input

Run the program again and then try typing a word instead of a number.

Not great behavior, right?

Replace .expect() with proper error handling, but first create a topic branch.

#![allow(unused)]
fn main() {
let guess: u32 = match guess.trim().parse() {
    Ok(num) => num,
    Err(_) => {
        println!("Please enter a valid number!");
        continue;  // Skip to next loop iteration
    }
};
}

Replace the relevant code, run and debug and do the git steps again.

You should end up on the main branch with all the changes merged and 4 commits.

Final Complete Game

use std::io;
use rand::Rng;
use std::cmp::Ordering;

fn main() {
    let secret_number = rand::rng().random_range(1..=100);
    //println!("The secret number is: {secret_number}");

    println!("Guess the number between 1 and 100!");

    loop {
        println!("Please input your guess.");

        let mut guess = String::new();

        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");

        println!("You guessed: {}", guess);

        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("Please enter a valid number!");
                continue;  // Skip to next loop iteration
            }
        };

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;  // exit the loop
            }
        }
    }

}

Comments & Documentation Best Practices

What would happen if you came back to this program in a month?

Inline Comments (//)

  • Explain why, not what the code does
  • Bad: // Create a random number
  • Good: // Generate secret between 1-100 for balanced difficulty
  • If it's not clear what the code does you should edit the code!

Doc Comments (///)

  • Document meaningful chunks of code like functions, structs, modules
  • Show up in cargo doc and IDE tooltips
/// Prompts user for a guess and validates input
/// Returns the parsed number or continues loop on invalid input
fn get_user_guess() -> u32 {
    // implementation...
}

You can try putting a doc comment right before fn main() {

The Better Comments extension

See it on VS Code marketplace

  • Color-codes different types of comments in VSCode - let's paste it into main.rs and see
// TODO: Add input validation here
// ! FIXME: This will panic on negative numbers
// ? Why does this work differently on Windows?
// * Important: This function assumes sorted input

Hello VSCode and Hello Github Classroom!

Part 1: GitHub Classroom Set-up

Step 1: Accept the Assignment (One Person Per Group)

  1. Go here: https://classroom.github.com/a/XY-1jTAX
  2. Sign into GitHub if you aren't signed in, then select your name from the list
  3. Create or join a team:
    • If you're first in your group: Click "Create a new team" and name it (e.g., "team-alice-bob")
    • If teammate already started: Find and click on your team name
  4. Click "Accept this assignment"
  5. Click on repository URL to open it - it will look something like this:
    https://github.com/cdsds210-fall25-b1/activity6-team-alice-bob
    

Step 2: Clone the Repository (Everyone)

Open a terminal and navigate to where you keep your projects (optional, but recommended for organization).

cd path/to/your/projects/

In the GitHub webpage for your group, click the green "code" button, and copy the link.

Then clone the repo in your terminal. Your clone command will look like one of these:

git clone https://github.com/cdsds210-fall25-b1/your-team-repo-name.git # HTTPS

Troubleshooting:

  • If HTTPS asks for password: Use your GitHub username and a personal access token (not your GitHub password)

Step 3: Open in VSCode (Everyone)

cd your-team-repo-name
code .

You may see recommendations for a few extensions - do not install them. Carefully select extensions to install.

Step 4: VSCode Exploration

From within your project, open src/main.rs in the navigation sidebar.

Explore These Features:

  • Hover over variables - What type information do you see?
  • Type println! and wait - Notice the autocomplete suggestions
  • Introduce a typo (like printl!) - See the red squiggle error
  • Hover over rand to see definition pop up
  • Right-click on rand - Try "Go to Definition" to jump to code
  • Open integrated terminal (Ctrl+` or View -> Terminal)
  • Run cargo run from the VSCode terminal

Part 2: Making contributions

Step 1: Make a plan as a team

Take a look at src/main.rs the repo as a group and identify two errors using VSCode hints and/or cargo check (you may need to fix one bug first in order to find the other). Then divide up these tasks among your team:

  1. Fixing the bugs (could be one person or split among two people)
  2. Adding some comments into src/main.rs to explain how the code works
  3. Editing the README.md file to include a short summary of how you found the bugs, anything that was confusing or rewarding about this activity, or any other reflections

Step 2: Make individual branches

Make a branch that includes your name and what you're working on, eg. ryan-semicolon-bug-fix or kia-adding-comments

git checkout -b your-branch-name

Step 3: Fix your bug and/or add comments

Talk to each other if you need help!

Step 4: Commit and push

git add . # or add specific files
git commit -m "fix missing semicolon" # your own descriptive comment here 
git push -u origin ryan-semicolon-bug-fix # your own branch name here

Step 5: Create a Pull Request

  1. Go to your team's GitHub repository in your browser
  2. Click the yellow "Compare & pull request" button (or go to "Pull requests" → "New pull request")
  3. Make sure the base is main and compare is your branch
  4. Write a title like "Fix semicolon bug"
  5. Click "Create pull request"

Step 6: Review PRs and Merge

  1. Look at someone else's pull request (not your own!)
  2. Click "Files changed" to see their changes
  3. Leave feedback or request other changes if you want
  4. When you're ready, go to "Review changes" -> "Approve" -> "Submit review"
  5. Click "Merge pull request" -> "Confirm merge"

If you encounter "merge conflicts" try following these instructions.

Step 7: Is it working?

Run git checkout main and git pull when you're all done, and cargo run to see if your final code is working!

There's no "submit" button / step in GitHub Classroom - when you're done and your main branch is how you want it, you're done!

Wrap-up

What we've accomplished so far:

  • Can now use shell, git, and rust all in one place (VSCode)
  • We built a complete, functional game from scratch
  • Started learning key Rust concepts: loops, matching, error handling
  • We've practiced using GitHub Classroom - you'll use it for HW2!