0

I just started learning Rust, and I'm currently trying to create a simple rock paper scissors game. I decided to create a main menu to choose between singleplayer and multiplayer as this

io::stdin()
            .read_line(&mut choix)
            .expect("Failed to read line");
    choix = str::to_lowercase(&choix);
    match choix.as_str() {
        "singleplayer" => singleplayer(),
        "multiplayer" => multiplayer(),
        _ => println!("Unknown choice, please choose 'singleplayer' or 'multiplayer'"),
    }
    println!("End of game");

but the pattern matching does not seem to work properly, as shown by this execution: singleplayer execution what am I doing wrong? Thanks!

DreadFog
  • 1
  • 1
  • `choix = choix.trim().to_lowercase();` – kmdreko Jul 30 '22 at 22:14
  • So this is the correct answer because the line break was also added to choix? – DreadFog Jul 30 '22 at 23:16
  • Yes. Entering "singleplayer" into the terminal would yield `"singleplayer\n"` in `choix`. Using `.trim()` will remove any beginning or trailing whitespace, which will make your `match` behave as you expect. – kmdreko Jul 30 '22 at 23:28

0 Answers0