Common Newbie Mistakes and Bad Practices in Rust: Bad Habits

submited by
Style Pass
2021-09-27 13:00:08

This is more of an extended essay than an article to be read in a single sitting. Feel free to read it one piece at a time, or just skip to the bits that look interesting.

Often this is awesome because it means you aren’t learning programming from scratch! However, you can also bring along bad habits which can lead you down the wrong rabbit hole or make you write bad code.

The code written in this article is available on the Rust Playground using the various (playground) links dotted throughout. Feel free to browse through and steal code or inspiration.

In most C-based languages (C, C#, Java, etc.), the way you indicate whether something failed or couldn’t be found is by returning a “special” value. For example, C#’s String.IndexOf() method will scan an array for a particular element and return its index. Returning -1 if nothing is found.

You see this sort of “use a sentinel value to indicate something special” practice all the time. Other sentinel values you might find in the wild are "", or null (someone once referred to this as their [“billion-dollar mistake”][billion-dollar-mistak]).

Leave a Comment