
ALT struct Person { name: String, age: u32 } impl std::fmt::Display for Person { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{} ({} years old)", self.name, self.age) } } fn main() { let person = Person { name: "Alice".to_string(), age: 30 }; println!("{}", person); // Output: Alice (30 years old) }

ALT macro_rules! create_function { ($func_name:ident) => { fn $func_name() { println!("You called {:?}()", stringify!($func_name)); } }; } create_function!(foo); create_function!(bar); fn main() { foo(); bar(); }
![// notice how all the imports are revealed! #![feature(prelude_import)] #[prelude_import] use std::prelude::rust_2021::*; #[macro_use] extern crate std; // notice how the function bodies are shown! fn foo() { { ::std::io::_print(format_args!("You called {0:?}()\n", "foo")); }; } fn bar() { { ::std::io::_print(format_args!("You called {0:?}()\n", "bar")); }; } fn main() { foo(); bar(); }](https://pbs.twimg.com/media/GZTNyZmXIAA538F.jpg?name=small&format=webp)
ALT // notice how all the imports are revealed! #![feature(prelude_import)] #[prelude_import] use std::prelude::rust_2021::*; #[macro_use] extern crate std; // notice how the function bodies are shown! fn foo() { { ::std::io::_print(format_args!("You called {0:?}()\n", "foo")); }; } fn bar() { { ::std::io::_print(format_args!("You called {0:?}()\n", "bar")); }; } fn main() { foo(); bar(); }
![#[derive(Default, Debug)] struct Person { name: String, age: u32, } #[derive(Debug)] struct PersonWithCustomDefault { name: String, age: u32, } impl Default for PersonWithCustomDefault { fn default() -> Self { PersonWithCustomDefault { name: "Unknown".to_string(), age: 10 } } } fn main() { let person = Person::default(); println!("{:?}", person); // Output: Person { name: "", age: 0 } let person_with_custom_default = PersonWithCustomDefault::default(); println!("{:?}", person_with_custom_default); // Output: PersonWithCustomDefault { name: "Unknown", age: 10 } }](https://pbs.twimg.com/media/GZDxBBcXQAMLYX9.jpg?name=small&format=webp)
ALT #[derive(Default, Debug)] struct Person { name: String, age: u32, } #[derive(Debug)] struct PersonWithCustomDefault { name: String, age: u32, } impl Default for PersonWithCustomDefault { fn default() -> Self { PersonWithCustomDefault { name: "Unknown".to_string(), age: 10 } } } fn main() { let person = Person::default(); println!("{:?}", person); // Output: Person { name: "", age: 0 } let person_with_custom_default = PersonWithCustomDefault::default(); println!("{:?}", person_with_custom_default); // Output: PersonWithCustomDefault { name: "Unknown", age: 10 } }

ALT struct Animal { name: String, age: u32, fav_treat: Option<String> } fn print_treat(animal: Animal) { // println!("{} loves {}", animal.name, animal.fav_treat) // This line ^^ wouldn't compile because fav_treat is an Option<String> // and can't be printed directly. Instead lets use a match statement! match &animal.fav_treat { Some(treat) => println!("{} loves {}", animal.name, treat), None => println!("{} doesn't have a favorite treat", animal.name), } }
![enum TrafficLight { Red, Yellow, Green, } fn get_light_action(light: TrafficLight) -> &'static str { // This match does NOT compile, the compiler says the following: //error[E0004]: non-exhaustive patterns: `TrafficLight::Yellow` not covered match light { TrafficLight::Red => "Stop", TrafficLight::Green => "Go", } }](https://pbs.twimg.com/media/GY1F6XFXMAMycja.jpg?name=small&format=webp)
ALT enum TrafficLight { Red, Yellow, Green, } fn get_light_action(light: TrafficLight) -> &'static str { // This match does NOT compile, the compiler says the following: //error[E0004]: non-exhaustive patterns: `TrafficLight::Yellow` not covered match light { TrafficLight::Red => "Stop", TrafficLight::Green => "Go", } }
![#[derive(Debug, Clone)] struct Person { name: String, age: u32, } fn main() { let alice = Person { name: "Alice".into(), age: 30 }; let mut bob = alice.clone(); bob.name = "Bob".to_string(); dbg!(alice); dbg!(bob); // Outputs: // alice = Person { name: "Alice", age: 30 } // bob = Person { name: "Bob", age: 30 } }](https://pbs.twimg.com/media/GYvKpzqXYAMikj9.jpg?name=small&format=webp)
ALT #[derive(Debug, Clone)] struct Person { name: String, age: u32, } fn main() { let alice = Person { name: "Alice".into(), age: 30 }; let mut bob = alice.clone(); bob.name = "Bob".to_string(); dbg!(alice); dbg!(bob); // Outputs: // alice = Person { name: "Alice", age: 30 } // bob = Person { name: "Bob", age: 30 } }




![#[cfg(test)] mod tests { use std::thread; use std::time::Duration; #[test] fn quick_test() { assert_eq!(2 + 2, 4); } #[test] #[ignore] fn slow_test() { // Simulate a long-running test thread::sleep(Duration::from_secs(5)); assert!(true); } }](https://pbs.twimg.com/media/GZdg9nmWIAMIN_v.jpg?name=small&format=webp)

!["[rust]": { "editor.defaultFormatter": "rust-lang.rust-analyzer", "editor.formatOnSave": true }](https://pbs.twimg.com/media/GZOEMPKWQAAV9oF.jpg?name=small&format=webp)


