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
  • 388,571
  • 95
  • 1,107
  • 1,366
Philippe
  • 1,287
  • 11
  • 23

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
  • 388,571
  • 95
  • 1,107
  • 1,366
swizard
  • 2,551
  • 1
  • 18
  • 26