0

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

    println!("You entered {symbol}");

    let symbol_x: String = format!("{}", ValidMove::X);
    let symbol_o: String = format!("{}", ValidMove::O);

    match &symbol {
        symbol_x => ok = true,
        symbol_o => ok = true,
        _ => ok = false,
    }

The compiler warns that

  • the 3rd arm is unreachable
  • symbol_x is unused
  • symbol_y is unused

why!?!?

realtebo
  • 23,922
  • 37
  • 112
  • 189
  • 1
    You can't match against variables like this, you have to match against a [pattern](https://doc.rust-lang.org/book/ch18-00-patterns.html), i.e. a literal, destructured tuple, etc. What you're doing here is matching against a _new_ variable called `sumbol_x` that will store the content of `symbol`. That's an _irrefutable_ pattern and that's why it tells you the other ones are unreachable. – isaactfa Aug 05 '22 at 11:50

1 Answers1

1

You cannot match against variables, therefore your match statement is equivalent to:

match &symbol {
    zzz => ok = true,
    yyy => ok = true,
    _ => ok = false,
}
hkBst
  • 2,818
  • 10
  • 29
  • "You cannot match against variables" ... ah, this is new for me... thanks. I'll use an if a this lap – realtebo Aug 05 '22 at 12:00
  • See the link from @Jmb in the comment on your question. That also provides a *match guard* suggestion which is what you probably want to do. – Kevin Anderson Aug 05 '22 at 14:09