Version 1.0
Initialization
use std::io;
use std::cmp::Ordering;
use rand::Rng;
use std::io;
: Imports the standard I/O library, allowing input and output operations.
use std::cmp::Ordering;
: Imports the Ordering
enumeration, which is used for comparison purposes.
use rand::Rng;
: Imports the Rng
trait, which provides methods to generate random numbers.
main
Function
- The
main
function is the entry point of the Rust program.
Language Selection
let mut language = String::new();
println!("Select your language:");
println!("1 - Português");
println!("2 - English");
io::stdin()
.read_line(&mut language)
.expect("Failed to read line");
if language.trim() == "1" {
portuguese();
} else if language.trim() == "2" {
english();
} else {
println!("Invalid input. Try again!");
main();
}
- Prompts the user to select a language (Portuguese or English) and reads their input.
- Based on the user's input, it calls the
portuguese
function if the input is '1' and the english
function if the input is '2'.
- If the user inputs an invalid option, the program displays an error message and restarts the
main
function.
english
Function
- The
english
function represents the game logic when the user selects the English language.
Number Guessing Game Logic
fn portuguese() {
// ...
}
- The
portuguese
function represents the game logic when the user selects the Portuguese language.
Repeated Game Play
println!("Do you want to play again? (s/n)");
let mut play_again = String::new();
io::stdin()
.read_line(&mut play_again)
.expect("Failed to read line");
if play_again.trim().to_lowercase() == "s" {
main();
} else {
println!("Thank you for playing!");
}
- Asks the user if they want to play the game again.
- If the user enters 's' or 'S', it restarts the
main
function for another round.
- If the user enters any other input, the program prints a thank-you message and terminates.