Skip to content

Can't call &mut method inside expression used as argument for a &mut method of the same struct #88167

Closed
@JakubKoralewski

Description

@JakubKoralewski

I tried this code:

fn main() {
    struct Xd;
    impl Xd {
        fn x(&mut self, y: &str) {println!("{}", y)}
    }
    let mut xd = Xd;
    xd.x(match "x" {
        "x" => {
            xd.x("x");
            "x"
        },
        _ => "y"
    })
}

I expected to see this happen: print x twice

Instead, this happened:

   Compiling playground v0.0.1 (/playground)
error[E0499]: cannot borrow `xd` as mutable more than once at a time
 --> src/main.rs:9:13
  |
7 |     xd.x(match "x" {
  |     -- - first borrow later used by call
  |     |
  |     first mutable borrow occurs here
8 |         "x" => {
9 |             xd.x("x");
  |             ^^ second mutable borrow occurs here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0499`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Meta

rustc --version --verbose:

1.54.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions