Skip Navigation

InitialsDiceBearhttps://github.com/dicebear/dicebearhttps://creativecommons.org/publicdomain/zero/1.0/„Initials” (https://github.com/dicebear/dicebear) by „DiceBear”, licensed under „CC0 1.0” (https://creativecommons.org/publicdomain/zero/1.0/)CH
china🇨🇳 @ kionite231 @lemmy.ca
Posts
133
Comments
833
Joined
2 yr. ago

  • done :D

     
        
    use std::io;
    
    fn main() {
        let mut input: String = String::new();
        let stdin = io::stdin();
    
        let x = rand::random::<u32>() % 101;
        let mut attempts = 0;
    
        let mut user_inputs: Vec<u32> = Vec::new();
        loop {
            println!("Guess a number from 0 to 100:");
            stdin.read_line(&mut input);
            input = input.to_string().replace("\n", ""); // removing the \n
            let user_input: u32 = input.parse::<u32>().unwrap();
        user_inputs.push(user_input);
            if x == user_input {
                println!("You won! attempts: {attempts}");
            println!("Your inputs:");
            for input in user_inputs {
            print!("{input} ");
            }
            println!("");
                break;
            }
            else if x < user_input {
                println!("too big");
                attempts += 1;
            }
            else {
                println!("too small");
                attempts += 1;
            }
            input.clear()
        }
    }
    
    
      
  • let is_palindrome = input.chars().eq(input.chars().rev());

    wow, this is really awesome. you just made a single liner for this whole problem. I didn't know that you could do something like this since I don't know much about Rust yet.