Thanks everyone. I actually thought about the two suggestions before posting. I thought there might be some common paradigm for this in the language though.
So I take it that implementing a += operator overload function would not have a generic way to handle the case where the same parameter is passed on both sides? -- Ziad On Sat, Jun 1, 2013 at 9:42 PM, Tim Chevalier <catamorph...@gmail.com>wrote: > On Sat, Jun 1, 2013 at 9:33 PM, Ziad Hatahet <hata...@gmail.com> 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 Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev