Reaching for Visual Cues
I’ve been working through the Rust Book recently and one thing that clicked with me that didn’t before was the behavior of the statement terminating semi-colon. In my daily driver languages for years like Python, Elixir, etc. they aren’t needed and for JavaScript, Zig, etc. you’re always including them even when not required.
Semi-colons do have some practical use in one-liner scripts like:
python -c 'import sys; print(sys.path)'.
Rust on the other hand is an expression based language with intermediate statements.
fn calculate_area(width: u32, height: u32) -> u32 {
// have any many let-bindings as you like!
let area = width * height;
let perimeter = 2 * (width + height);
// The last expression is implicitly returned
area + perimeter
}
I’m a little annoyed that using a the
returnkeyword optionally allows a semi-colon or it’s allowed in something like thisNone => { return 0; }. It’s probably a tradeoff for people coming from C where the muscle memory is to type the semi-colon after a return keyword.
Nix to the rescue
So it’s mostly muscle memory and me being a bit dumb but I kept making the same mistakes of adding or omitting the semi-colon on the last expression. So stopped at it for a good while and tried to anchor it into something that would help me remember. What helped was seeing it like a let-expression in a functional language like Nix. It’s hardly a perfect comparison because Nix uses semi-colons more like comma delimiters but that’s besides the point! Here’s an example in Nix of the function body and how I’m visually processing the layout.
let
area = width * height; # ← separates this binding...
perimeter = 2 * (width + height); # ← ...from this one
in
area + perimeter
For pure functions, even with imperative middle bits, this helped me have a quick visual shortcut for reading and I stopped making the typos.
The example isn’t terribly important
Now there’s no novel observations here and it’s unlikely this exact example would be useful to anyone but me. That said, this technique of temporarily mapping visual cues is something I reach for often when experimenting. I think it’s less about the cue or metaphor and more the act of applying attention with purpose. The meta-feeling of when I get it ‘right’ is similar to hearing a catchy tune and immediately knowing it’ll be stuck in your head for days.