🀝 #Rustlang Tip: Contribute to the Rust community!

βœ… DO:

- Report bugs you encounter
- Help answer questions on forums like users.rust-lang.org
- Contribute to open-source Rust projects
- Write blog posts or tutorials about your Rust experiences

❌ DON'T:

- Be afraid to ask for help
- Hesitate to share your knowledge, no matter your skill level

Remember: Every contribution, big or small, helps the Rust ecosystem grow! πŸ¦€

#Rust30by30

#Rustlang Tip:

βœ… DO: use &str for string slices and String for owned strings.
❌ DON'T: use &String as a function parameter.

Why? Using &str is more flexible:

1: It allows passing &str and &String as arguments.
2: It's more flexible as it can be used to pass string literals and string slices.
3: It clearly communicates that the function won't modify the string.

#Rust30by30

πŸ” #Rustlang Tip: Use the std::iter module for powerful iterator operations.

Docs: https://doc.rust-lang.org/std/iter/

#Rust30by30

std::iter - Rust

Composable external iteration.

πŸ”’ #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.

#Rust30by30

πŸ“š #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!

#Rust30by30

βœ… 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 #Rust30by30

⚠️ #Rustlang Tip: Use the #[must_use] attribute to ensure important return values aren't ignored.

Result is already marked as must_use but other types can be too!
It's good to add #[must_use] when ignoring the return value of a function is almost always a bug.

Reference: https://std-dev-guide.rust-lang.org/policy/must-use.html

#Rust30by30

When to add #[must_use] - Standard library developers Guide

Guide for standard library developers

πŸ” #Rustlang Tip: Use the std::fmt::Display trait to customize how your structs are printed!

#RustFormatting #Rust30by30 #Day18

πŸ” #Rustlang Tip: Use std::mem::size_of::<T>() to check the size of types at compile-time.

#RustPerformance #Rust30by30 #Day17

πŸ” #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!

#RustCollections #Rust30by30 #Day16