Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Matching a reference and making the resulting value mutable in one step #45708

Closed
Boscop opened this issue Nov 2, 2017 · 3 comments
Closed
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@Boscop
Copy link

Boscop commented Nov 2, 2017

It should be possible to match a reference and make the resulting value mutable in one step.
But this doesn't work:

println!("{:?}", { let v = [1, 2, 3]; v.iter().map(|&(mut x)| { x += 1; x }).collect::<Vec<_>>() } );
error: expected one of `,` or `@`, found `)`
 --> src/main.rs:2:64
  |
2 |     println!("{:?}", { let v = [1, 2, 3]; v.iter().map(|&(mut x)| { x += 1; x }).collect::<Vec<_>>() } );
  |  

https://play.rust-lang.org/?gist=f76143789ed3a6a4fef1d950580f7904&version=stable

It only works like this:

println!("{:?}", { let v = [1, 2, 3]; v.iter().map(|&x| { let mut x = x; x += 1; x }).collect::<Vec<_>>() } );

which is very verbose. It should also be possible with &(mut x) (or mut &x).

@cuviper
Copy link
Member

cuviper commented Nov 2, 2017

I think this is not directly an issue with references, but with ()-grouping in patterns at all. You also can't write something like .map(|(x)| x + 1), but it's fine in type positions like fn zero() -> (i32) { 0 }.

References do give a strong motivation for this though, to distinguish &(mut x) from &mut x.

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jan 23, 2018
@Spoonbender
Copy link

Triage: resolved.

The example compiles and works as intended on rust 1.59.0, see here

I think we can close this one.

@cuviper
Copy link
Member

cuviper commented Mar 22, 2022

Yes, that should be pattern_parentheses (#51087) which stabilized in 1.31 (#54497).

@cuviper cuviper closed this as completed Mar 22, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants