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
matchexpressions andOrderingfor 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, notrust!
- You should have
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:
cargo runfrom terminal- Click the little
Runthat decorates abovefn 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 overviewgit log --oneline- Commit historygit 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:
- Remove the secret reveal - no cheating!
- Add a loop - keep playing until correct
- Compare numbers - too high? too low?
- Handle invalid input - what if they type "banana"?
But before we proceed, create a topic branch by
- clicking on
mainin 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 docand 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.rsand 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)
- Go here: https://classroom.github.com/a/XY-1jTAX
- Sign into GitHub if you aren't signed in, then select your name from the list
- 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
- Click "Accept this assignment"
- 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
randto see definition pop up - Right-click on
rand- Try "Go to Definition" to jump to code - Open integrated terminal (
Ctrl+`orView -> Terminal) - Run
cargo runfrom 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:
- Fixing the bugs (could be one person or split among two people)
- Adding some comments into
src/main.rsto explain how the code works - Editing the
README.mdfile 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
- Go to your team's GitHub repository in your browser
- Click the yellow "Compare & pull request" button (or go to "Pull requests" → "New pull request")
- Make sure the base is
mainand compare is your branch - Write a title like "Fix semicolon bug"
- Click "Create pull request"
Step 6: Review PRs and Merge
- Look at someone else's pull request (not your own!)
- Click "Files changed" to see their changes
- Leave feedback or request other changes if you want
- When you're ready, go to "Review changes" -> "Approve" -> "Submit review"
- 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!