0

I'm learning rust, my goal is to create a cli menu that will loop the options in match arms until 3 is pressed to break out of it.

Currently my code does what is supposed to the first iteration, but when doing the second the conditions in the match arms don't get triggered, not sure why.

use std::io;

pub fn run() {
    let mut user_input = String::new();
    loop {
        println!("Welcome!");
        println!("Press 1 to add employee");
        println!("Press 2 to show employees by department");
        println!("Press 3 to to quit");

        io::stdin()
            .read_line(&mut user_input)
            .ok()
            .expect("Failed to read line");

        let user_number: u8 = match user_input.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        match user_number {
            1 => println!("1 Selected"),
            2 => println!("2 Selected"),
            3 => {
                println!("3 Selected");
                break;
            }
            _ => println!("not recognized"),
        }
    }
}
Chris
  • 9,043
  • 4
  • 12
  • 31
RenierC
  • 31
  • 2
  • 7

0 Answers0