Trending

#RustTricks

Latest posts tagged with #RustTricks on Bluesky

Latest Top
Trending

Posts tagged #RustTricks

#[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 } }

#[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 } }

🏗️ #Rustlang Tip: Use the #[derive(Default)] attribute for struct initialization if all your fields have a Default implementation.

Be careful though, for Strings field, the default implementation will create an empty string!

#RustTricks #Rust30by30 #Day10

0 0 0 0
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),     } }

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), } }

#Rustlang Tip: use Option<T> for values that might be absent.

Rust doesn't have null values, but it does have Option<T>

This lets the compiler help check for those pesky "null" cases for you, and make sure you handle the Option::None case!

#RustTricks #Rust30by30 #Day9

1 0 0 0

#Rustlang Tip: Speed up dev cycles by using cargo check instead of cargo build during development

It skips building everything but still checks your code for any compilation errors!
You can also use cargo watch to automatically run cargo check!

#RustTricks #Rust30by30 #Day8

1 0 0 0