-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Description
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 ref
s 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 ref
s 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 ref
s, 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 ref
s.
(Btw, the "help" message is not correct and thus not helpful in the last situation!)