# Making patterns more ergonomic

**URL:** <https://internals.rust-lang.org/t/making-patterns-more-ergonomic/5949>\
**Category:** language design\
**Created:** [September 20, 2017, 11:23am UTC](https://internals.rust-lang.org/t/making-patterns-more-ergonomic/5949 "2017-09-20T11:23:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael-F-Bryan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/michael-f-bryan/32/2815_2.png) [@Michael-F-Bryan](https://internals.rust-lang.org/u/Michael-F-Bryan)\
**Post date:** [September 20, 2017, 11:23am UTC](https://internals.rust-lang.org/t/making-patterns-more-ergonomic/5949/1 "2017-09-20T11:23:37Z")

</div>

A while back there was talk about trying to make pattern matching (particularly when `ref`, `match`, and `*` are involved) more ergonomic and stumbled across a particularly annoying edge case which reminded me of it.

Is there any more news on this?

My particular example is that I’m iterating over a `Vec<(usize, Bot)>` and want to update each `Bot`. Currently I’ve got to write a big mess of `&mut` and `ref` to make the compiler happy:

```rust
for &mut (tok, ref mut bot) in &mut self.bots {
    let actions = bot.tick(&self.game_state);
    debug!("Bot {} executed {:?}", tok, actions);
}

```

Whereas it’d be much easier (and arguably just as understandable) if pattern matching could elide away the boilerplate so I can write something like this:

```rust
for (tok, mut bot) in &mut self.bots {
    let actions = bot.tick(&self.game_state);
    debug!("Bot {} executed {:?}", tok, actions);
}

```

In theory the compiler should be able to automatically dereference the tuple reference, then that because `mut bot` is trying to use a mutably borrowed thing it’ll automatically insert `ref mut bot`.

---

<div class="post-metadata">

**Author:** ![kennytm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kennytm/32/161_2.png) [@kennytm](https://internals.rust-lang.org/u/kennytm)\
**Post date:** [September 20, 2017, 12:42pm UTC](https://internals.rust-lang.org/t/making-patterns-more-ergonomic/5949/2 "2017-09-20T12:42:22Z")

</div>

I think this would be handled by [RFC 2005 (Default binding mode)](https://github.com/rust-lang/rfcs/blob/master/text/2005-match-ergonomics.md) as well. Maybe comment on its tracking issue [https://github.com/rust-lang/rust/issues/42640](https://github.com/rust-lang/rust/issues/42640)?

---

<div class="post-metadata">

**Author:** ![leodasvacas](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/leodasvacas/32/4060_2.png) [@leodasvacas](https://internals.rust-lang.org/u/leodasvacas)\
**Post date:** [September 20, 2017, 6:13pm UTC](https://internals.rust-lang.org/t/making-patterns-more-ergonomic/5949/3 "2017-09-20T18:13:08Z")

</div>

~~Hopefully in the future we’ll discuss how to extended that to all patterns, not only match, which would include for loops, fn/closure arguments, let bindings, etc…~~

~~But that would be a whole new RFC, so we should probably wait until we get more experience with only `match`.~~

Thanks @kennytm for the correction, glad to know the RFC already covers this!

---

<div class="post-metadata">

**Author:** ![kennytm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kennytm/32/161_2.png) [@kennytm](https://internals.rust-lang.org/u/kennytm)\
**Post date:** [September 20, 2017, 6:30pm UTC](https://internals.rust-lang.org/t/making-patterns-more-ergonomic/5949/4 "2017-09-20T18:30:04Z")

</div>

RFC 2005 already covers all patterns. Note that the examples in the RFC involve `let` bindings. Also the current implementation ([https://github.com/rust-lang/rust/pull/44614](https://github.com/rust-lang/rust/pull/44614)) [works with OP's `for` loop](https://github.com/rust-lang/rust/issues/42640#issuecomment-330862173).

[https://github.com/rust-lang/rfcs/blob/master/text/2005-match-ergonomics.md#detailed-design](https://github.com/rust-lang/rfcs/blob/master/text/2005-match-ergonomics.md#detailed-design):

> Note that this RFC applies to all instances of pattern-matching, not just `match` expressions:
> 
> ```rust
> struct Foo(i32);
> let foo = Foo(6);
> let foo_ref = &foo;
> // `foo_ref` is dereferenced, and `x` is bound like `ref x`.
> let Foo(x) = foo_ref;
> 
> ```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/making-patterns-more-ergonomic/5949/5 "2019-03-25T08:29:02Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
