rust guessing game code example

Example: rust guessing game

//Guessing Game from Rust Book
use std::io;
use rand::Rng;
use std::cmp::Ordering;
fn main() {
    let secret:i32 = rand::thread_rng().gen_range(1, 101);
    loop{
        println!("Ingresa un número: ");
        let mut guess = String::new();
        io::stdin().read_line(&mut guess).expect("Failed to read line");
        let guess:i32 = match guess.trim().parse(){
            Ok(num) => num,
            Err(_) => continue,
        };
        //print!("tu numero {}", guess);

        match guess.cmp(&secret){
            Ordering::Less => println!("Más arriba!"),
            Ordering::Greater => println!("Más abajo!"),
            Ordering::Equal => {
                println!("Ese mismo compadre");
                break;
            }
        }        
    }
}

Tags:

Rust Example