Create a Number Guessing Game in Rust: A Beginner’s Guide!
Do you want to write your own mini-game using programming? The number guessing game is simple and fun, and you can get started in just 10 minutes! This article will guide you through creating an interactive number guessing game using Rust—voted as the “most loved” language by developers. From scratch, we will teach you how to generate random numbers, handle user input, compare values, and even implement multiple guesses. Whether you are a programming novice or a developer looking to try Rust, this tutorial will help you easily get started, learn while playing, and unlock the joy of programming!
This article will take you step by step to create a fun number guessing game using Rust: the program generates a random number between 1 and 100, and after the player makes a guess, they will receive hints of “too low,” “too high,” or “correct.” The tutorial starts with basic input, gradually introducing random number generation, number comparison, and multiple guessing features, with code that is simple and easy to understand. Whether you are a complete beginner or a developer wanting to quickly get started with Rust, you can learn about Rust’s input/output, pattern matching, and error handling through this practical project. In just 10 minutes, you can easily complete your first Rust program!
Hands-On Practice
Number Guessing Game – One Guess
Number Guessing Game – Objective
- Generate a random number between 1 and 100
- Prompt the player to input a guess
- After guessing, the program will indicate whether the guess is too low or too high
- If the guess is correct, print a congratulatory message and exit the program
Write Code

use std::io; // prelude
fn main() {
println!("Guess the number!");
println!("Please guess a number");
// let mut foo = 1;
// let bar = foo; // immutable
// foo = 2;
let mut guess = String::new();
io::stdin().read_line(&mut guess).expect("Unable to read line");
// io::Result Ok Err
println!("Your guessed number is: {}", guess);
}

Number Guessing Game – Generate the Secret Number
Rand: https://crates.io/crates/rand
➜ cd rust/guessing_game
guessing_game on main [✘!] is 📦 0.1.0 via 🦀 1.67.1
➜ cargo build
Compiling cfg-if v1.0.0
Compiling ppv-lite86 v0.2.17
Compiling libc v0.2.139
Compiling getrandom v0.2.8
Compiling rand_core v0.6.4
Compiling rand_chacha v0.3.1
Compiling rand v0.8.5
Compiling guessing_game v0.1.0 (/Users/qiaopengjun/rust/guessing_game)
Finished dev [unoptimized + debuginfo] target(s) in 1.08s
guessing_game on main [✘!] is 📦 0.1.0 via 🦀 1.67.1
➜ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.03s
guessing_game on main [✘!] is 📦 0.1.0 via 🦀 1.67.1
➜ cargo update

Random Number

Code:
use std::io; // prelude
use rand::Rng; // trait
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1..101);
println!("The secret number is: {}", secret_number);
println!("Please guess a number");
// let mut foo = 1;
// let bar = foo; // immutable
// foo = 2;
let mut guess = String::new();
io::stdin().read_line(&mut guess).expect("Unable to read line");
// io::Result Ok Err
println!("Your guessed number is: {}", guess);
}
Number Guessing Game – Compare Guessed Number with Secret Number
use std::io; // prelude
use rand::Rng; // trait
use std::cmp::Ordering; // enum type with three variants (values)
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1..101);
println!("The secret number is: {}", secret_number);
println!("Please guess a number");
// let mut foo = 1;
// let bar = foo; // immutable
// foo = 2;
let mut guess = String::new();
io::stdin().read_line(&mut guess).expect("Unable to read line");
// io::Result Ok Err
// shadow
let guess: u32 = guess.trim().parse().expect("Please type a number!"); // \n
println!("Your guessed number is: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"), // arm
Ordering::Greater => println!("Too big!"),
Ordering::Equal => println!("You win!"),
}
}
Number Guessing Game – Allow Multiple Guesses
use std::io; // prelude
use rand::Rng; // trait
use std::cmp::Ordering; // enum type with three variants (values)
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1..101);
// println!("The secret number is: {}", secret_number);
loop {
println!("Please guess a number");
// let mut foo = 1;
// let bar = foo; // immutable
// foo = 2;
let mut guess = String::new();
io::stdin().read_line(&mut guess).expect("Unable to read line");
// io::Result Ok Err
// shadow
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("Please enter a valid number");
continue;
}
};
println!("Your guessed number is: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"), // arm
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}
Conclusion
Through this number guessing game project, you not only created a fun interactive program using Rust but also grasped the essence of programming! From simple user input to random number generation, number comparison, and implementing loops for multiple guesses, each step brings you closer to mastering Rust. Key takeaways:
-
Getting Started with Rust: Learn to handle input with std::io and generate random numbers with rand, unlocking Rust’s powerful features.
-
Error Handling: Make your program more robust with match and expect.
-
Programming Mindset: Experience the joy of iterative development from zero to a complete project.
This mini-game is just the beginning! Try adding a guess limit or a cool scoring system, and continue exploring the infinite possibilities of programming with Rust! Feel free to leave a comment and share your achievements!

References
- https://crates.io/crates/rand
- https://www.rust-lang.org/zh-CN
- https://doc.rust-lang.org/stable/book/
- https://this-week-in-rust.org/
- https://rust-lang.github.io/rustup/overrides.html#toolchain-override-shorthand
- https://doc.rust-lang.org/stable/rust-by-example/
- https://rustacean.net/
- https://doc.rust-lang.org/nomicon/index.html