// => ERROR Strongly Typed: let a = 3.14_f64; let b = "100"; let c = [1u32, 2, 3]; a * b // => ERROR b / a // => ERROR c + a // => ERROR c + c // => ERROR
F Iced(bool), // 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); }
Iced(bool), // still has ice Instant, // :'( } fn main() { let cup = Coffee::Iced(true); let snob_filtered = match cup { Instant => false, _ => true }; println!("Drink from cup? {:?}", snob_filtered); }
Iced(bool), // still has ice Instant, // :'( } fn main() { let cup = Coffee::Hot(212); let safe = match cup { Coffee::Iced(x) if x => true, Coffee::Hot(120...150) => true, _ => false, }; println!("Drink from cup? {:?}", safe); }
and run tests coffee.rs:5:3: 5:10 warning: variant is never used: `Instant`, #[warn(dead_code)] on by default coffee.rs:5 Instant, // :'( ^~~~~~~ coffee.rs:10:3: 12:4 warning: method is never used: `fresh`, #[warn(dead_code)] on by default coffee.rs:10 fn fresh() -> Coffee { coffee.rs:11 Coffee::Hot(212) coffee.rs:12 } coffee.rs:24:1: 28:2 warning: function is never used: `main`, #[warn(dead_code)] on by default coffee.rs:24 fn main() { coffee.rs:25 let cup_o_joe = Coffee::fresh(); coffee.rs:26 coffee.rs:27 println!("Fresh cup of {:?}", cup_o_joe); coffee.rs:28 } running 1 test test avoid_lukewarm_coffee ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
standard library Some(T), None } fn main() { let a = Some(10u32); let b = None; let squared = match a { Some(x) => x * x, None => 0 }; let either = b.or(a); println!("Squared: {:?}, Either: {:?}", squared, either); // => Squared: 100, Either: Some(10) }
Treasure structs 2. Write a default constructor for Point 3. Write a function that returns the treasures's position if it has not been disturbed 4. Write a function that gives (x,y) directions from the Archaeologist to the Treasure 5. Write tests for all 3 functions
Ok(T), Err(E), } fn main() { let pot_contents = Coffee::Hot(180); let could_fail = match pot_contents { cup @ Coffee::Hot(130...212) => Ok(cup), _ => Err("Time to brew a new pot."), }; let no_excuses: Option<Coffee> = could_fail.ok(); let cant_wait = no_excuses.unwrap_or("To the coffee shop!"); }
allocate some memory println!("the value of r: {:?}", r); // access r let color = r; // transfer ownership to `color` println!("the value of color: {:?}", color); // access color println!("the value of color: {:?}", r); // => error: use of moved value: `r` // owner `color` falls out of scope // owner drops its property }
// allocate some memory println!("the value of r: {:?}", r); // access r let color: &Box<Color> = &r; // borrow a reference // access borrowed value println!("the value of color: {:?}", color); // access owned value println!("the value of color: {:?}", r); // borrower & owner are both dropped }
= "Hello"; let rude: String = polite.to_ascii_uppercase(); println!("{:?} is polite.", polite); println!("{:?} is rude.", rude); // polite is dropped, but it owns nothing // rude is dropped, its owned str primitive is dropped }
Color::Green, Color::Red]; // if we shallow copied... let y = x; // x frees its vector contents // y frees the *same* vector contents // double free! SEGFAULT }
= vec![Color::Red, Color::Green, Color::Red]; // the ownership of the vec *moves* to y let y = x; println!("{:?}", y); // => ok, y owns it println!("{:?}", x); // => error! use of moved value // x is already invalidated // y frees the vector contents }
Color::Green; // x is the owner of memory let y = &x; // y borrows x // everyone can read immutably borrowed memory! println!("{:?}", x); // ok! println!("{:?}", y); // ok! let k = x; // => error: cannot move out of `x` because it is borrowed }
box Color::Green; // x is the owner of memory let y = &x; // y borrows x // everyone can read immutably borrowed memory! println!("{:?}", x); // ok! println!("{:?}", y); // ok! // *copy* the reference to the Box let k = y; println!("{:?}", k); // ok! println!("{:?}", y); // ok! }
for your stoplight Color 2. Heap allocate a Color, borrow it, peek at it with the borrow 3. Write a "go_through" method for your stoplight Color, return a Result 4. Don't forget to write your tests!
let a = 100u32; let b = a; // b is a new copy // Coffee does _not_ impl Copy let c = Coffee::Hot(212); let d = c; // ownership of existing Coffee *moved* to d }
implements Copy let a = (0u32, 2, 5); // when x is bound, it is *copied* let b = match a { (x,_,_) if x > 0 => x * x, (_,x,_) if x > 0 => x * x, (_,_,x) if x > 0 => x * x, _ => 0 }; println!("a: {:?}, b: {:?}", a, b); }
// ownership of the coffee is moved let microwaved = match cup { Coffee::Hot(x) if x < 130 => Coffee::Hot(200), hot @ Coffee::Hot(_) => hot, _ => Coffee::Hot(212), }; println!("cup: {:?}, microwaved: {:?}", cup, microwaved); // => error: use of moved value: `cup` }
= Color::Green; // => error: re-assignment of immutable variable `x` // move to mutable ownership let mut y = x; *y = Color::Green; // move to immutable ownership let z = y; println!("{:?}", z); }
box Color::Red; { let y = &mut x; let z = &mut x; // => error! cannot borrow `x` as // mutable more than once at a time x = box Color::Yellow; // => error! cannot assign to `x` // because it is borrowed *y = box Color::Green; // => ok! } println!("{:?}", x); // => Color::Green }
= Point { x: 25.0, y: 25.0 }; reset(&mut foo); // => error! can't take mutable reference of immutable variable let mut foo = foo; // transfer ownership to mutable scope { let ref_foo = &foo; // takes a immutable reference let bar = dup(ref_foo); println!("Copy: {:?}", bar); // immutable ref to foo falls out of scope } reset(&mut foo); // takes a mutable reference println!("New Position: {:?}", foo); }
Make functions that heal it, or hurt it. 3. Make a Weapon type. 4. Make a function that prints all the bot's weapons. 5. Make an attack function. 6. impl Rand for BattleBot 7. Make a randomly generated BattleBot royale!
{ Red, Yellow, Green }; } // in project_b/src/main.rs extern crate project_a; // link to project_a use traffic::Color; // import type from `traffic` module fn main() { let stoplight = Color::Red; println!("Imported a {:?} stoplight!", stoplight); }
Color { Red, Yellow, Green }; } // in project_b/src/main.rs extern crate project_a; use traffic::Color{Red,Yellow,Green}; fn main() { let stoplight = Red; println!("Imported a {:?} stoplight!", stoplight); }
{ Red, Yellow, Green }; } // in project_b/src/city_planning/roadways.rs extern crate project_a; pub use traffic::Color{Red,Yellow,Green}; // in project_c/src/main.rs extern crate project_b; use city_planning::roadways::Color;
rx) = channel(); Thread::spawn(move || { tx.send("Data produced in child task").unwrap(); }).detach(); let data = rx.recv().unwrap(); println!("{:?}", data); // 1x => Data produced in child task }
(tx, rx) = channel(); for task_num in range(0u8, 8) { let tx = tx.clone(); // <-- will be captured by thread Thread::spawn(move || { let msg = format!("Task {:?} done!", task_num); tx.send(msg).unwrap(); }).detach(); } for _ in range(0u8, 8) { let data = rx.recv().unwrap(); println!("{:?}", data); } // 10x => Task N done! }
channel(); let huge_struct = Arc::new(HugeStruct::new()); for task_num in range(0u8, 8) { let tx = tx.clone(); let huge_struct = huge_struct.clone(); spawn(proc() { let msg = format!("Task {:?}: Accessed {:?}", task_num, huge_struct.huge_name); tx.send(msg); }); } drop(tx); // => force last transmitter to hang up for data in rx.iter() { println!("{:?}", data); // 10x => Task N: Accessed I'M HUGE } }
channel(); let huge_struct = HugeStruct::new(); let huge_struct = Arc::new(Mutex::new(huge_struct)); for task_num in range(0u8, 8) { let tx = tx.clone(); let huge_struct = huge_struct.clone(); // clone the arc Thread::spawn(move || { random_sleep(); // <-- without this, they always grab the lock in spawn order let mut inner_struct = huge_struct.lock().unwrap(); inner_struct.access_count += 1; let msg = format!("Task {:?}: Accessed Count {:?}", task_num, inner_struct.access_count); tx.send(msg).unwrap(); }).detach(); } drop(tx); // => force last transmitter to hang up for data in rx.iter() { println!("{:?}", data); // 10x => Task N: Accessed I'M HUGE } }