Advertisement
Guest User

Rust cow race

a guest
Mar 3rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.56 KB | None | 0 0
  1. use std::{thread,time};
  2. use std::sync::{Arc, RwLock};
  3. use std::borrow::Cow;
  4.  
  5. fn main() {
  6.     let mut list=Cow::from(vec![2,3,5,7,11,13,17]);
  7.     let shared=Arc::new(RwLock::new(list.clone()));
  8.     let h={
  9.         let mut shared=shared.clone();
  10.         thread::spawn(move || {
  11.             let list_copy=shared.read().unwrap().clone();
  12.             println!("{:?}", list_copy);
  13.         })
  14.     };
  15.     list.to_mut().push(19);
  16.     //Comment the sleep line and the other thread should get the new list.
  17.     thread::sleep(time::Duration::from_millis(10));
  18.     *shared.write().unwrap()=list.clone();
  19.     h.join().unwrap();
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement