Game skeleton
- Rust course
- Beginner
- Lesson 60/115
- 6 min
Set up the guessing game welcome screen with main and println!.
Guessing game: the plan
In this project we build the classic number guessing game step by step: the program picks a secret number from 1 to 100, the player enters a guess, the program says "too small", "too big" or "you got it", and it repeats until the player gets it. The pieces we'll use: input with stdin, converting to a number with parse, comparing with cmp and Ordering, and repeating with loop.
$ cargo new tahmin_oyunu
$ cd tahmin_oyunu
$ cargo runThe skeleton
The first version only prints the welcome text. use std::io; will be needed for reading input; we add it now:
use std::io;
fn main() {
println!("Sayıyı tahmin et!");
println!("Tahminini gir:");
}An unused use causes the compiler's "unused import" warning; it isn't an error, and we'll use it in the next step.
Practice Welcome
Write the game skeleton: print the lines "Sayıyı tahmin et!" and "Tahminini gir:" in order.
Starter code
fn main() {
// buraya yaz
}Sample solution
fn main() {
println!("Sayıyı tahmin et!");
println!("Tahminini gir:");
}Expected output
Sayıyı tahmin et! Tahminini gir:
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which file does <code>cargo new tahmin_oyunu</code> create?
- index.rs
- src/lib.c
- src/main.rs Correct answer
- main.go
Explanation: cargo new sets up a binary project: Cargo.toml and src/main.rs with a hello world.
2 The input module
use std::____;
fn main() {
println!("Sayıyı tahmin et!");
}
use std::io;
fn main() {
println!("Sayıyı tahmin et!");
}
Explanation: std::io contains the input/output tools, such as stdin.
3 Command and its job
| Term | Meaning |
|---|---|
cargo new |
Creates a project |
cargo run |
Builds and runs |
cargo check |
Only checks |
cargo build --release |
Builds optimized |