ohai.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
A cozy, fast and secure Mastodon server where everyone is welcome. Run by the folks at ohai.is.

Administered by:

Server stats:

1.8K
active users

#rust30by30

0 posts0 participants0 posts today

🔒 #Rustlang Tip: Use the #[non_exhaustive] attribute for future-proofing your enums and structs.

This allows you to add new variants later without breaking existing code.

Sidenote: The #[non_exhaustive] attribute forces people to have a _ pattern in their matches that will match any new items you add later.

📚 #Rustlang Tip: Use cargo doc to generate documentation for your project.

Doc comments use three slashes /// and support Markdown
They appear in the generated documentation
Regular double line comments // won't be included in the documentation.

example:
Run cargo doc --open to generate and view your documentation!

✅ DO: Use std::collections::HashSet for storing unique values.
❌ DON'T: Reinvent the wheel with custom data structures.

Sidenote: A HashSet is implemented as a HashMap where the value is the empty tuple (). So HashSet<String> is essentially the same as HashMap<String, ()>!

🔍 #Rustlang Tip: Use Vec::with_capacity(n) when you know the approximate size of your vector beforehand. This will reserve memory for the vector to avoid multiple allocations.

This is a performance optimization and shouldn't change the behavior of the code in most situations.

Be careful though, if you overestimate the size of the vector you will waste memory!

🏗️ #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.

⚡ #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!

🔧 #Rustlang Tip
Use the #[derive(Debug, Clone)] attribute to automatically generate implementations for the Debug and Clone traits for your structs and enums!

💡 Clone is useful when you need a copy of the struct
💡 Debug is useful for printing the struct for debugging purposes, and can be used with the dbg! macro

⚠️ #RustLang Tip: Don't use .unwrap()!
Use expect() instead in tests or when you're absolutely sure a Result/Option is Some/Ok but you can't convey that in the type system.
❌ DON'T: Use either in production code where errors are possible.

expect() is like unwrap() but with a custom message!

🔍 Better alternatives:
❓ ? operator, to forward the error up the call stack
🧩 match

📚 Read more: doc.rust-lang.org/book/ch09-02

doc.rust-lang.orgRecoverable Errors with Result - The Rust Programming Language