Some general guidelines:
std::mem::swapwhen you can.
- Prefer passing references to the data over owned data. (Don't take
&str. Don't take
- Use generics, traits, and other abstractions Rust provides.
- Avoid using lossy conversions (for example: don't do
my_u32 as u16 == my_u16, prefer
my_u32 == my_u16 as u32).
- Prefer in place (
boxkeyword) when doing heap allocations.
- Prefer platform independently sized integer over pointer sized integer (
usize, for example).
- Follow the usual idioms of programming, such as "composition over inheritance", "let your program be divided in smaller pieces", and "resource acquisition is initialization".
unsafeis unnecessary, don't use it. 10 lines longer safe code is better than more compact unsafe code!
- Be sure to mark parts that need work with
- Use the compiler hint attributes, such as
#[cold], etc. when it makes sense to do so.
- Try to banish
expect()from your code in order to manage errors properly. Panicking must indicate a bug in the program (not an error you didn't want to manage). If you cannot recover from an error, print a nice error to stderr and exit. Check Rust's book about unwrapping.