简体   繁体   中英

How do I convert a mutable reference to self into an immutable reference to be used as an argument for a method?

I have following code that can't be compiled:

struct A {
    x: i32,
}

impl A {
    fn add_assign(&mut self, other: &Self) {
        self.x += other.x;
    }

    fn double(&mut self) {
        self.add_assign(self);
    }
}

The error is:

error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
  --> src/lib.rs:11:9
   |
11 |         self.add_assign(self);
   |         ^^^^^----------^----^
   |         |    |          |
   |         |    |          immutable borrow occurs here
   |         |    immutable borrow later used by call
   |         mutable borrow occurs here

How to pass self as the argument of add_assign ? I have tried &self , *self , &*self without success.

For the current version of the question

fn add_assign(&mut self, other: &Self)

Your request is impossible.

You cannot have a mutable reference and an immutable reference to the same value at the same time. This is a fundamental aspect of Rust.

Please re-read the rules of references .

See also:

For the first version of the question

fn add_assign(&mut self, other: Self)

Your request is impossible.

You need one instance of struct A to call the method on and another instance of A to pass as the argument. Your type does not implement Copy or Clone or provide any equivalent methods so there is no way to get a second instance.

Beyond that, there's no universal way to take a mutable reference to a value and get an owned value out of it.

See also:

Workarounds

If you implement Copy or Clone , then you can get a second value from the original and then call either of your versions.

If you implemented Copy :

  • (other: Self)

     self.add_assign(*self);
  • (other: &Self)

     let other = *self; self.add_assign(&other);

If only Clone :

  • (other: Self)

     self.add_assign(self.clone());
  • (other: &Self)

     self.add_assign(&self.clone());

You probably want to implement the AddAssign trait to provide syntax sugar. Assuming you've implemented Copy :

impl A {
    fn double(&mut self) {
        *self += *self;
    }
}

impl std::ops::AddAssign<Self> for A {
    fn add_assign(&mut self, other: Self) {
        self.x += other.x;
    }
}

Stargateur's comment may also be applicable, as i32 implements Copy :

impl A {
    fn double(&mut self) {
        *self += self.x;
    }
}

impl std::ops::AddAssign<i32> for A {
    fn add_assign(&mut self, other: i32) {
        self.x += other;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM