← blog.mackieburgess

Rust, React, and helpful intuition

Mackie Burgess · 2023-11-27

Two of my favourite programming features are React’s Strict Mode and the Rust compiler. I think these two tools have a lasting impact on the developer: even outside their home languages.

Strict Mode

In React, anything wrapped in <StrictMode> tags is treated strictly. This means that all components are rendered twice, all effects are run twice, and deprecation warnings are turned on.

This only happens when you’re running in dev. When you deploy these “features” get turned off.

To some this may seem like plain annoyance, but there’s a niceness to it: React sometimes doesn’t render things as expected. Using Strict Mode, React always doesn’t render things as expected. This means when you’re designing your application, you account for these weird “rare” edge cases, and you write better code as a result of it.

Notably, code becomes more idempotent. Idempotent functions are functions which do the same thing when called multiple times, as if they’re called just once.

Imagine the buttons in a lift/elevator: you press a button once, it selects the floor. You press a button twice, it selects the floor.

# In an testing framework which can measure side effects...

def g1(f):
    f()

def g2(f):
    f()
    f()

assert g1 `does the same as` g2

This is a really good way of writing code. Even if it doesn’t always save you, it sometimes saves you, and it enforces good habits.

The Rust Compiler

The rust compiler has a lot going on. Today I’m only looking at one part of it: pattern matching.

Let’s start with another language which has static typing: Typescript.

enum Direction {
  North,
  South,
  East,
  West
}

function to_shetland_dialect(dir: Direction) {
  switch (dir) {
    case Direction.North:
      console.log("nort");
      break;
    case Direction.South:
      console.log("sooth");
      break;
    case Direction.East:
      console.log("aist");
  }
}

Yay, code. Here’s the same code in Rust:

enum Direction {
  North,
  South,
  East,
  West
}

fn to_shetland_dialect(dir: Direction) {
  match dir {
    Direction::North => println!("nort"),
    Direction::South => println!("sooth"),
    Direction::East => println!("aist"),
  }
}

These two snippets do the same thing, with one slight difference: Rust will not compile my code, because my code is broken. It’s broken in both statements! West isn’t handled. Rust will instead give a very big error, explaining exactly what you did wrong.

To see the full error message, go to play.rust-lang.org and paste the snippet.

To complete my Rust code, I can handle the final case:

  match dir {
    // ...
    Direction::West => println!("wast"),
  }

Or give a blanket case which covers all scenarios not previously handled:

  match dir {
    // ...
    _ => println!("I dunna keyn"),
    // alternatively, this gives a particular runtime error if it ever gets called.
    _ => todo!()
  }

This insistence forces you to write code which handles all possibilities, cutting out the chance that you write bad code. This is super helpful if your enum is in a different file, and your codebase is huge. You never know when you might want to add a secret fifth direction. In this case, Rust helps you write consistently better code.


The reason why these two language features are so nice is when you spend a lot of time living with them, they start to become part of your brain. Once that happens, these good practices start extending to other languages. Particularly in Rust, the compiler fights with you for a while, but it’s secretly training you to write better code.

What it boils down to is that systems which train “good intuition” are a good way of building up transferable skills while writing better code in the here-and-now.

There are other systems which do similar things for different aspects of programming. It’d be interesting for someone to compile a list of languages/tools which have these sorts of features. You could design a programming class which secretly teaches you a bunch of good habits.

For now, I’ll continue reaping the present and accumulative benefits of React’s Strict Mode and Rust’s compiler.