True. I suppose I meant generic and memory efficient at the same time. I guess it shouldn't be too much of a hassle to implement the specialized case of this function if memory is absolutely critical.
Thanks -- Ziad On Sat, Jun 1, 2013 at 10:03 PM, Abhijeet Gaiha <[email protected]>wrote: > The 'copy' parameter is the most generic way for the client to handle this > situation. > > -- > Abhijeet Gaiha > http://about.me/abhijeet.gaiha > > On Sunday, 2 June 2013 at 10:32 AM, Ziad Hatahet wrote: > > 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 <[email protected]>wrote: > > 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 > > >
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
