Skip to content

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

Closed
@Boscop

Description

@Boscop

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions