Seamus  Quitzon

Seamus Quitzon


Ditching Try / Catch And Null Checks In Rust

Learn how to implement a better way of error handling in your development using Rust’s safer Result enum and pattern matching.

This post is written by a JavaScript developer just entering the world of Rust. A JS background isn’t required to get value from this article! But if you’re a fellow web-developer-turned-Rustacean, you’ll empathize with my points a bit more.

It seems like languages built in the last decade are following a common trend: down with object-oriented models, and in with functional programming (FP).

Web developers may have seen the FP pattern emerge in modern frontend frameworks like React using their hooks model. But moving to Rust, you’ll see how powerful FP can be when you build an entire programming language around it — and the approach to the try...catch and null are just the tip of the iceberg!

Let’s explore the flaws of throwing and catching exceptions, what Rust’s Result enum and pattern matching can do for you, and how this extends to handling null values.

  • What is Rust?
  • Why you shouldn’t use try...catch blocks in Rust
  • Using Rust’s Result enum and pattern matching
    • Understanding match in Rust
    • Applying match to exceptions
    • Why pattern matching is the safer way to handle exceptions
  • How to use null in Rust
  • When to use Option vs. Result
  • Wrapping up (no pun intended)

#rust #programming #developer

Ditching Try / Catch And Null Checks In Rust