still has ice Instant, } fn main() { let first_cup: Coffee = Coffee::Iced(true); let second_cup: Coffee = Coffee::Hot(212); println!("Drink {:?} then {:?}.", first_cup, second_cup); }
Color { Red, Yellow, Green }; } // in project_b/src/main.rs extern crate project_a; use traffic::Color; fn main() { let stoplight = Color::Red; println!("Imported a {:?} stoplight!", stoplight); }
let dupped = coffee.clone(); } :18:25: 18:32 error: type `Box<Coffee>` does not implement any method in scope named `clone` :18 let dupped = coffee.clone(); ^~~~~~~ :18:25: 18:32 help: methods from traits can only be called if the trait is implemented and in scope; the following trait defines a method `clone`, perhaps you need to implement it: :18:25: 18:32 help: candidate #1: `core::clone::Clone`
let stoplight = Color::Red; // access value of stoplight println!("the value of stoplight: {:?}", stoplight); // owner `stoplight` falls out of scope // owner drops its property }
= Vec::new(); for _ in 0..4 { drink_caddie.push(Coffee::Hot(212)); } println!("all the coffee: {:?}", drink_caddie); // owner `coffee` falls out of scope // owner drops its property // drop the Vec --> drop each Coffee --> drop the u8 }
customer = coffee_shop; println!("the value of customer: {:?}", customer); println!("the value of coffee_shop: {:?}", coffee_shop); // => error! use of moved value `coffee_shop`! }
for task_num in 0..8 { let tx = tx.clone(); Thread::spawn(move || { let msg = format!("Task {:?} done!", task_num); tx.send(msg).unwrap(); }).detach(); } for data in rx.iter() { println!("{:?}", data); } }
channel(); let huge_struct = HugeStruct::new(); let arc = Arc::new(Mutex::new(huge_struct)); for task_num in 0..8 { let tx = tx.clone(); let arc = arc.clone(); Thread::spawn(move || { let mut guard = arc.lock().unwrap(); guard.access_count += 1; let msg = format!("Task {:?}: Accessed Count {:?}", task_num, guard.access_count); tx.send(msg).unwrap(); // drop the arc -> drop the guard -> unlock the mutex }).detach(); } for data in rx.iter() { println!("{:?}", data); // 10x => Task N: Accessed Count: M } }