rust-lang/rust

How to move out selected pattern constituents while keeping the others as implicit refs with match_default_bindings?

Boscop opened this issue · 2 comments

Consider this story / situation:

It starts with:

#![feature(match_default_bindings)]

v: Vec<(usize, Foo)>
fn f(i: usize) {}

Now I want to do this:

for (i, foo) in &v { f(i); }

This results in the following errors (of course, because i is &usize):

error: mismatched types
error: expected usize, found &usize
note: expected type `usize`, found type `&usize`

Now, I want to move i out, but keep using the convenience of match_default_bindings for all other pattern constituents.
So I change it to this:

for (&i, foo) in &v { f(i); }

But now I get these errors:

error: mismatched types
error: expected usize, found reference
note: expected type `usize`, found type `&_`
help: did you mean `i: &usize`?

With match_default_bindings enabled, matching with (i, foo) (instead of &(i, foo)) turns it into (ref i, ref foo).
Why doesn't it let me cancel out the implicit ref i by writing explicit &i, like a reasonable human would expect?
Especially people who are new to Rust and would expect this to work, and isn't the whole point of the match_default_bindings ergonomics change to make Rust easier to use for newbies?
So with match_default_bindings enabled, it should be allowed to explicitly move out selected constituents (while still retaining the implicit refs for the other pattern constituents), by cancelling out the ref with a &!

Or is there already an existing way to explicitly move out selected constituents while keeping the implicit refs for the others?
If so, that way is not intuitive, but please tell me :)
(And I know, in this case, the only other constituent is foo so it wouldn't be very inconvenient to write &(i, ref foo) but it's just in this small example; I'm asking for situations where I want ALL other constituents to stay implicit refs, when there are many, such as in my real-world code that brought this up, where I often have many pattern constituents that I want to take by implicit ref and just move out a few.)

IMO, There should definitely be a way to only move out selected constituents while keeping the others as implicity refs.

(Btw, the "help" message is not correct and thus not helpful in the last situation!)

Closing in favor of the discussion here #42640 (comment)

This issue is still open, it seems, and now being tracked at #64586.