home.social

#rusttricks — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #rusttricks, aggregated by home.social.

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

    Be careful though, sometimes the Default implementation is not what you want!

    For instance, if you have a String field, the default implementation will create an empty string, which if often not what I want.

    In those cases you can implement Default manually for custom behavior.

    #RustTricks #Rust30by30 #Day10

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

    Rust doesn't have null values, but it does have Option<T>, which is an enum that represents a value that might be present or absent.

    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

  3. #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 when you make changes to your code.

    #RustTricks #Rust30by30 #Day8