Option

In languages that have the notion of nullable values, there is a defensive code style that programmers adopt to perform operations on any value that can possibly be null. Taking an example from Kotlin/Java, it appears something like this:

// kotlin pseudocodeval container = collection.get("some_id")if (container != null) {    container.process_item();} else {    // no luck}

First, we check that container is not null and then call process_item on it. If we forget the null safety check, we'll get the infamous NullPointerException when we try to invoke container.process_item() you only get to know this at runtime when it throws the exception. Another downside is the fact that we can't deduce right away whether container is null just by looking ...

Get Mastering Rust - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.