0

I reduced my problem to the following code:

enum E {
    E1,
}

fn f(e1: &E, e2: &E) {
    match *e1 {
        E::E1 => (),
    }
    match (*e1, *e2) {
        (E::E1, E::E1) => (),
    }
}

fn main() {}

The first match is ok, but the second fails to compile:

error[E0507]: cannot move out of borrowed content
 --> src/main.rs:9:12
  |
9 |     match (*e1, *e2) {
  |            ^^^ cannot move out of borrowed content

error[E0507]: cannot move out of borrowed content
 --> src/main.rs:9:17
  |
9 |     match (*e1, *e2) {
  |                 ^^^ cannot move out of borrowed content

It seems that this is because I'm constructing a pair of something borrowed and Rust tries to move e1 and e2 into it. I found out that if I put "#[derive(Copy, Clone)]" before my enum, my code compiles.

Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
Philippe
  • 1,177
  • 9
  • 21

1 Answers1

5

You can match over a tuple of two references by removing the dereference operator from the variables:

enum E {
    E1,
}

fn f(e1: &E, e2: &E) {
    match *e1 {
        E::E1 => (),
    }
    match (e1, e2) {
        (&E::E1, &E::E1) => (),
    }
}
Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
swizard
  • 2,351
  • 15
  • 23