Going by the suggested solution of using Atomic Reference Counters (std::arc) 
for sharing immutable data across different tasks, I would presume that the 
most generic solution would be to use the arc module. You can see an example of 
this in action here : 
https://github.com/smadhueagle/rustlings/blob/master/borrow.rs
Madhu

From: hata...@gmail.com
Date: Sat, 1 Jun 2013 22:30:35 -0700
To: abhijeet.ga...@gmail.com
CC: rust-dev@mozilla.org
Subject: Re: [rust-dev] Mutability and borrowing

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 <abhijeet.ga...@gmail.com> 
wrote:



                The 'copy' parameter is the most generic way for the client to 
handle this situation.

                
-- Abhijeet Gaihahttp://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 <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 
listrust-...@mozilla.org

https://mail.mozilla.org/listinfo/rust-dev
                 
                 
                 
                 
                
                 
                
                    

                
            


_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev                                      
  
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to