How does this demo illustrate the capabilities of an Arc?


#1

I watched the talk called “Rust Concurrency Explained” and in it, the speaker shows the following illustration:

However, since v2 is cloned from v, I don’t understand how this is any different from v just simply not being an Arc at all? Of course when you copy data and place it in another area of memory (variable), you can pass that to a separate thread. Can someone please elaborate a bit? Is this just an issue of making v “live long enough” to pass to the new thread? Thanks.


#2

An Arc clone just bumps the reference count, but still contains the same inner value. So v and v2 are still sharing the same Vec instance.

Since thread::spawn requires a 'static lifetime, the closure can’t just borrow a value from the local context. So here they use move to fully give v to the thread, while the local context still keeps v2. Arc is what lets these two share ownership of the same underlying value.


#3

Ahhh I got confused by the syntax here. I read clone() literally. I appreciate that info, it’s been throwing me off for a while!


#4

This is a big part of the reason people tend to prefer the let v2 = Arc::clone(&v) formulation; it’s the same thing but makes it a bit clearer what is being cloned.