user1244932
user1244932

Reputation: 8112

Is there any difference between matching on a reference to a pattern or a dereferenced value?

Clippy warns about code like this:

fn func<T>(data: &Option<T>) {
    if let &Some(ref value) = data {}
}
warning: you don't need to add `&` to all patterns
 --> src/main.rs:2:5
  |
2 |     if let &Some(ref value) = data {}
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(match_ref_pats)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.210/index.html#match_ref_pats
help: instead of prefixing all patterns with `&`, you can dereference the expression
  |
2 |     if let Some(ref value) = *data {}
  |            ^^^^^^^^^^^^^^^   ^^^^^

Are these constructions the same from compiler point of view:

if let &Some(ref value) = data {
if let Some(ref value) = *data {

If so, what's the point in the Clippy message, just to use uniform style?

Upvotes: 4

Views: 790

Answers (1)

Shepmaster
Shepmaster

Reputation: 431449

Yes, these are the same to the compiler. In this case, there's not much benefit. The real benefit comes from the match equivalent:

fn func(data: &Foo) {
    match data {
        &Foo::One => {}
        &Foo::Two => {}
        &Foo::Three => {}
    }
}

Here, you only have to place a single dereference, not 3 references in the patterns:

fn func(data: &Foo) {
    match *data {
        Foo::One => {}
        Foo::Two => {}
        Foo::Three => {}
    }
}

And since Rust 1.26, you don't even have to dereference the expression being matched on:

fn func(data: &Foo) {
    match data {
        Foo::One => {}
        Foo::Two => {}
        Foo::Three => {}
    }
}

That's why it's the idiomatic choice.

The if let concept is just an extension from this.


You can't always dereference the value. If you tried to do the same thing for a pair of items:

fn func(data: &Foo, data2: &Foo) {
    match (*data, *data2) {
        (Foo::One, _) => {}
        (Foo::Two, _) => {}
        (Foo::Three, _) => {}
    }
}

You get the error

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

In this case, you can use the reference form:

fn func(data: &Foo, data2: &Foo) {
    match (data, data2) {
        (&Foo::One, _) => {}
        (&Foo::Two, _) => {}
        (&Foo::Three, _) => {}
    }
}

Or, since Rust 1.26, perform some implicit references:

fn func(data: &Foo, data2: &Foo) {
    match (data, data2) {
        (Foo::One, x) => {}
        (Foo::Two, _) => {}
        (Foo::Three, _) => {}
    }
}

Upvotes: 10

Related Questions