# Match ergonomics 2024 poll

**URL:** <https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772>\
**Category:** language design\
**Created:** [May 5, 2024, 4:35pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772 "2024-05-05T16:35:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [May 5, 2024, 4:35pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/1 "2024-05-05T16:35:30Z")

</div>

I'm working on changes to match ergonomics for the 2024 edition. As part of that, I would like people's opinion on a design question. Consider the pattern below, which does not use match ergonomics:

```rust
let &[&mut ref x] = &[&mut 42];

```

# What should the match-ergonomics version of the above pattern look like?

_Poll ([view on site](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/1))_

## If you chose one of the first 2 options: if you had to choose between both or neither, which would you prefer?

_Poll ([view on site](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/1))_

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [May 5, 2024, 4:41pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/2 "2024-05-05T16:41:17Z")

</div>

In `let [&x] = &[&mut 42]; or let &[x] = &[&mut 42];`, how can you possibly match `&mut 42` with `&x`? Does this means that `x` would have type `&42` here, and with this, would be read only?

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [May 5, 2024, 4:53pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/3 "2024-05-05T16:53:29Z")

</div>

> [@dlight](#):
>
> Does this means that `x` would have type `&42` here, and with this, would be read only?

Correct.

---

<div class="post-metadata">

**Author:** ![jplatte](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jplatte/32/11437_2.png) [@jplatte](https://internals.rust-lang.org/u/jplatte)\
**Post date:** [May 5, 2024, 5:11pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/4 "2024-05-05T17:11:10Z")

</div>

> [@Jules-Bertholet](#):
>
> `let &[x] = &[&mut 42];`

This is the only thing in the OP that _kind of_ makes sense to me. If you had asked whether the initial non-'ergonomic' example even compiles, I would probably have said no, but with very little confidence.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [May 5, 2024, 5:44pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/5 "2024-05-05T17:44:12Z")

</div>

You presented three scenarios but only two options.

I don't think 2 or of the 3 scenarios should work and there's no poll option corresponding to that. If both scenarios "must" work for the second option, I'd vote for none working.

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [May 5, 2024, 5:48pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/6 "2024-05-05T17:48:01Z")

</div>

> [@quinedot](#):
>
> If both scenarios "must" work for the second option

That's what I was going for yes. However, I'd be curious to hear which single scenario of the latter two you think should be supported, and why.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [May 5, 2024, 6:25pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/7 "2024-05-05T18:25:30Z")

</div>

My thinking is that getting `&mut` out of it is not possible, so `& &mut` could be simplified to `& &`. So then to get a reference to x:

```
let [x] = 

```

or

```
 let &[x] =

```

And anything with `&x` or `&mut x` should give a dereferenced value.

* * *

The original `&mut ref` syntax is explicitly performing the only sensible conversion there can be to get a reference, so it's a needless detail that can be omitted entirely.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [May 5, 2024, 6:27pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/8 "2024-05-05T18:27:00Z")

</div>

> [@Jules-Bertholet](#):
>
> However, I'd be curious to hear which single scenario of the latter two you think should be supported, and why.

My comment was prompted more by a negative reaction to one of the two scenarios. I think this scenario:

```rust
let [& x] =
    &[&mut i];

```

should _ **not** _ be supported, because "unwrapping" a `&mut` with a `&` should be an error. Moreover, knowing what `let [&x] = &[&i]` does, I'd expect it to copy (or attempt to move) `i`, if anything.

* * *

I'm not sure I'd really want `let &[x]` to work either. But if changing the meaning of `[let &mut x]`\[1\] is on the table, I'd probably have to read up on the entire gamut of changes to form a solid opinion. Consistency across the whole system is important.

* * *

1. which compiles today

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [May 5, 2024, 6:52pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/9 "2024-05-05T18:52:12Z")

</div>

> [@quinedot](#):
>
> I think this scenario:
> 
> ```rust
> let [& x] =
> &[&mut i];
> 
> ```
> 
> should _ **not** _ be supported, because "unwrapping" a `&mut` with a `&` should be an error.

In the current draft design, if this example were to be supported, then the `&` in the pattern would unwrap the outer `&` from the scrutinee, not the inner `&mut`. (The design does allow `&` patterns to "unwrap" `&mut`, but that's not what going on here.)

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [May 5, 2024, 6:57pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/10 "2024-05-05T18:57:08Z")

</div>

> [@quinedot](#):
>
> But if changing the meaning of `[let &mut x]` is on the table,

(Assuming you mean `let [&mut x]` ) The plan is to change the meaning, yes. You can find the draft RFC [here](https://hackmd.io/@Jules-Bertholet/SJr9kXmfC). (Please use this topic to discuss only the options in the poll; other comments about the RFC draft should go to [this Zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Match.20ergonomics.202024.20bikeshed).)

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [May 5, 2024, 7:33pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/11 "2024-05-05T19:33:07Z")

</div>

> [@Jules-Bertholet](#):
>
> the `&` in the pattern would unwrap the outer `&` from the scrutinee

The combination of "`&` can match `&mut`" and "an inner `&` can match an outer `&`" seems error-prone, particularly if someone isn't completely sure what the type of the scrutinee is. For instance, suppose that you don't realize the thing you're matching has a reference on the outside of the type (e.g. `&[&mut T]`), and then you write `[&x]` expecting the `x` to match the type `T`. This would then end up producing a type other than the one you expected, because your `&`, rather than being an error, matches the outer `&`.

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [May 5, 2024, 7:37pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/12 "2024-05-05T19:37:39Z")

</div>

> [@josh](#):
>
> For instance, suppose that you don't realize the thing you're matching has a reference on the outside of the type (e.g. `&[&mut T]`), and then you write `[&x]` expecting the `x` to match the type `T`.

I don't see how `&` matching against `&mut` makes a difference here? You'd have the result matching an `[&x]` pattern against a `&[&T]` scrutinee.

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [May 5, 2024, 9:14pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/13 "2024-05-05T21:14:18Z")

</div>

I've added a [followup question](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772#if-you-chose-one-of-the-first-2-options-if-you-had-to-choose-between-both-or-neither-which-would-you-prefer-2) to the poll, for those who chose one of the first 2 options.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [May 5, 2024, 9:59pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/14 "2024-05-05T21:59:48Z")

</div>

I voted for `&[x]` / `[&x]` working to bind `x: &i32`, but with a small caveat — if/when patterns see through "`DerefPure`" containers, the same adjustment from binding `P<T>` to binding `&T` should apply to them, and to do so for `let &x = &&mut 42` as well.

I also voted for neither being preferable to both. I can see `[&mut x]` potentially binding `mut x: &i32`, `x: i32`, or `x: &i32` depending on specifics. Looking at it more makes me more understanding of how the third option gets accepted, but the ambiguity between `&«mut x»` and `«&mut» x` doesn't sit well with me.

This exploration has made me more appreciative of the current state. Although for the most case, default binding modes prefers you using a `[x]` pattern, getting `x: &&mut i32`, and utilizing deref coercion, anyway. I'm almost down to just entirely deprecate `&` patterns in non-default binding modes, honestly.

(That could be more interesting than it first seems, even — co-opt `&` patterns to shift from a `ref` binding mode back to by-move, but not for destructuring references anymore, instead using the general deref pattern for explicit destructure.)

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [May 5, 2024, 10:20pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/15 "2024-05-05T22:20:45Z")

</div>

My main gripe about how default binding modes work today is that (at least it feels like) you have to explain/understand all of binding modes in order to explain/understand what gets bound in a pattern that utilizes `mut` or `&` in a position with a non-default default binding mode. I have no idea how to address this (feeling).

Default binding modes do generally feel good when no explicit manipulation of the binding mode is done. It's the complexity cliff this results in when you want something other than that convenient default that things get awkward.

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [May 6, 2024, 12:24am UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/16 "2024-05-06T00:24:06Z")

</div>

> [@CAD97](#):
>
> You have to explain/understand all of binding modes in order to explain/understand what gets bound in a pattern that utilizes `mut` or `&` in a position with a non-default default binding mode.

The aim of the edition changes is to simplify these things:

- `mut` will no longer have any effect on whether the binding is by-reference or by-value
- `&` and `&mut` will ~always strip off a single reference

---

<div class="post-metadata">

**Author:** ![simonbuchan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/simonbuchan/32/9390_2.png) [@simonbuchan](https://internals.rust-lang.org/u/simonbuchan)\
**Post date:** [May 6, 2024, 9:40am UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/17 "2024-05-06T09:40:51Z")

</div>

If it helps, after reading the thread, I feel like I failed a college entrance exam! 😅

"Neither" seems safer now.

---

<div class="post-metadata">

**Author:** ![Vorpal](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@Vorpal](https://internals.rust-lang.org/u/Vorpal)\
**Post date:** [May 6, 2024, 10:00am UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/18 "2024-05-06T10:00:02Z")

</div>

I'm not voting, because to be honest I don't even know what this should do currently. My reaction was "Is that valid? What does it even mean?".

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [May 6, 2024, 8:50pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/19 "2024-05-06T20:50:26Z")

</div>

RFC is posted: [Match ergonomics 2024 by Jules-Bertholet · Pull Request #3627 · rust-lang/rfcs · GitHub](https://github.com/rust-lang/rfcs/pull/3627)

It specifies that none of the examples from the poll should work.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [May 9, 2024, 6:51pm UTC](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772/20 "2024-05-09T18:51:57Z")

</div>

The question is unclear to me – I think the poll sorts answers by popularity, so "first two" is ambiguous.

Also one of the answers included "or" with two examples, do you mean that answer? And btw the option with "or" includes one half that I agree with and other that I disagree with, so I didn't want to vote for such mixed option.

[Next page](https://internals.rust-lang.org/t/match-ergonomics-2024-poll/20772.md?page=2)
