On Sat, Jun 1, 2013 at 9:33 PM, Ziad Hatahet <[email protected]> wrote:
> I have the following function:
>
> fn add_equal(x: &mut Complex, y: &Complex) {
> x.real += y.real;
> x.imag += y.imag;
> }
>
> Calling the function with the same variable being passed to both arguments
> (i.e. add_equal(&mut c, &c)), results in the compile error:
>
> error: cannot borrow `c` as immutable because it is also borrowed as mutable
>
> I am guessing this is to avoid aliasing issues? What is the way around this?
>
You can copy y instead of passing a reference to it:
fn add_equal(x: &mut Complex, y: Complex) { ...
Of course, that means that at the call site, you will have to write
something like add_equal(&mut c, copy c).
Unless you want to write a function that just takes one argument and
doubles it, like Abhijeet suggested, I don't know of another way
around this.
Cheers,
Tim
--
Tim Chevalier * http://catamorphism.org/ * Often in error, never in doubt
"Not a riot, it's a rebellion." -- Boots Riley
"Attention Bros and Trolls: When I call out your spew, I'm not angry,
I'm defiant." -- Reg Braithwaite
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev