Common newbie mistakes or bad practices

  • Path::to_str().unwrap() instead of Path::display() to put a path into an error message
  • Trait over-use: implementing From for something which could have been a named factory function; introducing a trait where a bunch of inherent methods would do.
  • Trait miss-use: implementing TryFrom<&str> instead of FromStr.
  • Unnecessary .unwraps: if opt.is_some() { opt.unwrap() }.
  • Generic bloat: pub fn do_something(path: impl AsRef<Path>) { 200 line function to monomorphise in every crate }
  • Cyclic dependencies between crates (when a leaf crate have a dev-dependency on the root crate)
  • Somewhat arbitrary splitting of code into crates in general.
  • Error management. Rust has the tools to implement the best possible error management of any language, but there's no pit of success there. One stable state is a giant enum which combines errors from different subsystems, has an Other(String) variant just in case, and which is used for basically anything.
14 Likes