3

I'm trying to match the user input, but it always returns this error:

enter image description here

What am I doing wrong?

fn main(){
    let board: [[i32; 3]; 3] = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
    println!("{:?}", board);
    println!("Who will start, X or O?");
    let mut to_start = String::new();
    io::stdin().read_line(&mut to_start).expect("An error was encountered");
    match to_start {
        "X" => println!("Player 1 starts"),
        "O" => println!("Player 2 starts"),
        _ => println!("Please enter a valid input"),
        
    };  
}
Nicke7117
  • 187
  • 1
  • 10
  • In the left side of a `match` arm, `x` is treated not as the existing variable `x`, but as a *pattern* which binds whatever it matches to the *new* variable `x`. – trent Nov 10 '21 at 17:37
  • I added a duplicate for the updated question. I suppose I might as well link [Why does my string not match when reading user input from stdin?](https://stackoverflow.com/q/27773313/3650362) preemptively. You can fix both issues in one shot by writing `match to_start.trim()`. – trent Nov 10 '21 at 18:25
  • [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Nov 15 '21 at 23:37

0 Answers0