rust clone

use std::sync::Arc;
let foo = Arc::new(vec![1.0, 2.0, 3.0]);
// The two syntaxes below are equivalent.
let a = foo.clone();
let b = Arc::clone(&foo);
// a, b, and foo are all Arcs that point to the same memory location
cat src/main.rs 
use std::thread;
use std::sync::Arc;

fn main() {
        let data =Arc::new(vec![1,2,3,4,5]);
        let cp =  Arc::clone(&data);
        //let cp = data.clone();
    let handle =thread::spawn( move || {
               println!("threadid   {:?} and data.len {}", thread::current().id(), cp.len());
                }
    );
    println!("threadid   {:?} and data.len {}", thread::current().id(), data.len());
        handle.join();
}
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/own`
threadid   ThreadId(1) and data.len 5
threadid   ThreadId(2) and data.len 5
原文地址:https://www.cnblogs.com/dream397/p/14201626.html