Rust: Provide suggestion for use after move caused by match expression

Created on 11 Mar 2019  路  2Comments  路  Source: rust-lang/rust

Provide suggestion for use after move in match arm, either using ref in the pattern or borrowing the moved variable:

enum Message {
    Quit,
    Move {x: String, y: String},
    Write(String),
}

fn main() {
    let msg = Message::Move { x: String::new(), y: String::new()};
    match msg {
        Message::Quit => println!("q"),
        Message::Move {x, y} => println!("m {} {}", x, y),
        Message::Write(s) => println!("w: {}", s),
    }
    if let Message::Move { x, y } = msg {
        println!("{} {}", x, y);
    }
}

Has the following output:

error[E0382]: use of moved value
  --> src/main.rs:14:28
   |
11 |         Message::Move {x, y} => println!("m {} {}", x, y),
   |                        - value moved here
...
14 |     if let Message::Move { x, y } = msg {
   |                            ^ value used here after move
   |
   = note: move occurs because value has type `std::string::String`, which does not implement the `Copy` trait

error[E0382]: use of moved value
  --> src/main.rs:14:31
   |
11 |         Message::Move {x, y} => println!("m {} {}", x, y),
   |                           - value moved here
...
14 |     if let Message::Move { x, y } = msg {
   |                               ^ value used here after move
   |
   = note: move occurs because value has type `std::string::String`, which does not implement the `Copy` trait

_Reported at https://mobile.twitter.com/mountain_ghosts/status/1105168489160032257_


A possible output:

error[E0382]: use of moved value
  --> src/main.rs:14:28
   |
 9 |    match msg {
   |          --- help: consider borrowing instead of consuming here: `&msg`
...
11 |         Message::Move {x, y} => println!("m {} {}", x, y),
   |                        -
   |                        |
   |                        value moved here
   |                        help: borrow the binding to avoid consuming `msg`: `ref x`
...
14 |     if let Message::Move { x, y } = msg {
   |                            ^ value used here after move
   |
   = note: move occurs because value has type `std::string::String`, which does not implement the `Copy` trait
A-diagnostics A-suggestion-diagnostics C-enhancement D-papercut T-compiler

Most helpful comment

I am new to Rust and just came here after searching for 'rust value moved after match' on the internet.
The suggestion --- help: consider borrowing instead of consuming here: &msg has worked perfectly for me. I didn't know/wasn't aware that values are also moved in match expressions.

Thank you very much for the suggestion. I'd really like to see this in the compiler.

All 2 comments

I am new to Rust and just came here after searching for 'rust value moved after match' on the internet.
The suggestion --- help: consider borrowing instead of consuming here: &msg has worked perfectly for me. I didn't know/wasn't aware that values are also moved in match expressions.

Thank you very much for the suggestion. I'd really like to see this in the compiler.

We now suggest using ref in the pattern, which will always work, if not be completely stylistic.

Was this page helpful?
0 / 5 - 0 ratings