gerenciador de versões cargo: o gerenciador de projetos e de dependências rustc: o compilador rustdoc: o gerador de documentação rust-gdb e rust-lldb: debuggers
enum Status {Rich, Poor,} enum Work { Civilian,Soldier,} fn main() { use Status::{Poor, Rich}; use Work::*; let status = Poor; let work = Civilian; match status { Rich => println!("The rich have lots of money!"), Poor => println!("The poor have no money..."), } match work { Civilian => println!("Civilians work!"), Soldier => println!("Soldiers fight!"), } }
y: u32 } // destructure members of the struct let foo = Foo { x: (1, 2), y: 3 }; let Foo { x: (a, b), y } = foo; println!("a = {}, b = {}, y = {} ", a, b, y); // you can destructure structs and rename the variables, // the order is not important let Foo { y: i, x: j } = foo; println!("i = {:?}, j = {:?}", i, j); // and you can also ignore some variables: let Foo { y, .. } = foo; println!("y = {}", y); // this will give an error: pattern does not mention field `x` // let Foo { y } = foo; }
Dog { fn baby_name() -> String {String::from("Spot")} } impl Animal for Dog { fn baby_name() -> String {String::from("puppy")} } impl Animal for Cat { fn baby_name() -> String {String::from("zyon")} } fn main() { println!("A baby dog is called a {}", Dog::baby_name()); println!("A baby dog is called a {}", ::baby_name()); }
without resources struct Nil; #[derive(Clone, Debug)]// A tuple struct with resources that implements the `Clone` trait struct Pair(Box, Box); fn main() { let nil = Nil;// Instantiate Nil let copied_nil = nil;// Copy Nil, there are no resources to move println!("original: {:?}", nil);// Both `Nil`s can be used independently println!("copy: {:?}", copied_nil); let pair = Pair(Box::new(1), Box::new(2));// Instantiate Pair println!("original: {:?}", pair); let moved_pair = pair;// Copy pair into moved_pair, moves resources println!("copy: {:?}", moved_pair); let cloned_pair = moved_pair.clone(); // Clone moved_pair into cloned_pair (resources are included) drop(moved_pair);// Drop the original pair using std::mem::drop println!("clone: {:?}", cloned_pair);// The result from .clone() can still be used! }
std::path::Path; fn main() { // Create a path to the desired file let path = Path::new("hello.txt"); let display = path.display(); // Open the path in read-only mode, returns `io::Result` let mut file = match File::open(&path) { // The `description` method of `io::Error` returns a string that describes the error Err(why) => panic!("couldn't open {}: {}", display,why.description()), Ok(file) => file, }; // Read the file contents into a string, returns `io::Result` let mut s = String::new(); match file.read_to_string(&mut s) { Err(why) => panic!("couldn't read {}: {}", display,why.description()), Ok(_) => print!("{} contains:\n{}", display, s), } }
{ for i in 1..10 { println!("hi number {} from the spawned thread!", i); thread::sleep(Duration::from_millis(1)); } }); for i in 1..5 { println!("hi number {} from the main thread!", i); thread::sleep(Duration::from_millis(1)); } }
and Mutex use std::thread;// Import Threads from Rust Standard Library use std::time::Duration;// Import Sleep methods fn main() { let data = Arc::new(Mutex::new(vec![1u32, 2, 3])); println!("Data before thread mutation: {:?}", data); for i in 0..2 { let data = data.clone(); thread::spawn(move || { let mut data = data.lock().unwrap(); data[i] += 2; }); } println!("Data immediately after thread mutation: {:?}", data); thread::sleep(Duration::from_millis(10)); println!("Data 10ms after thread mutation: {:?}", data); thread::sleep(Duration::from_millis(50)); println!("Data 50ms after thread mutation: {:?}", data); }