Data shapes · 11 min · free lesson

null, undefined and ?? vs Option

null and undefined become Option<T>: absence is part of the type, and ?. and ?? become and_then, map and unwrap_or.

Core idea

Use Option<T> when a value may be absent and Result<T, E> when the caller needs the reason; unwrap_or replaces ??, not ||.

TypeScript and Rust, side by side

TypeScript ?. and ?? keep a configured 0, while || replaces it with the default.
interface Config {
  network?: { retries?: number };
}

function retryLimit(config: Config): number {
  return config.network?.retries ?? 3;
}

function retryLimitBuggy(config: Config): number {
  return config.network?.retries || 3; // 0 is falsy
}

console.log(retryLimit({}), retryLimit({ network: { retries: 0 } }));
console.log(retryLimitBuggy({ network: { retries: 0 } }));
Rust and_then follows the optional chain and unwrap_or supplies the ?? default, so Some(0) stays 0.
struct Network {
    retries: Option<u32>,
}

struct Config {
    network: Option<Network>,
}

fn retry_limit(config: &Config) -> u32 {
    config.network.as_ref().and_then(|n| n.retries).unwrap_or(3)
}

fn main() {
    let empty = Config { network: None };
    let zero = Config { network: Some(Network { retries: Some(0) }) };
    println!("{} {}", retry_limit(&empty), retry_limit(&zero));
}
What changes
TypeScriptRust
retries?: number may be missing, undefined or null.retries: Option<u32> is Some(n) or None, and must be handled before use.
config.network?.retries ?? 3config.network.as_ref().and_then(|n| n.retries).unwrap_or(3)
retries || 3 turns a configured 0 into 3.unwrap_or(3) replaces only None; Some(0) stays 0.
undefined + 1 is NaN at runtime.Adding an integer to an Option<u32> is E0369 at compile time.

Understand it

TypeScript with strictNullChecks already marks absence as T | undefined, but the value can still be null, undefined or a missing property, and the checks vanish at runtime. Rust has no null. A value that may be missing has type Option<T>, which is either Some(value) or None, and you cannot use it as a T until you decide what absence means. network.retries + 1 with an Option<u32> is a compile error, not NaN.

The operators become methods. a?.b is a.as_ref().and_then(|a| a.b) when b is itself optional, and .map(...) when it is not. x ?? 3 is x.unwrap_or(3), or unwrap_or_else(|| ...) when the default is expensive to build. Inside a function that returns Option, let n = config.network.as_ref()?; returns None early, the closest thing to optional chaining across statements.

There is no || default. retries || 3 turns a configured 0 into 3, and title || "Untitled" discards an intentionally empty string. unwrap_or only replaces None, so Some(0) stays 0. If zero really should count as missing, say so with .filter(|&n| n != 0) and a comment.

Option answers whether something is there; it does not say why not. When the caller needs a reason, such as "storage unreachable" rather than "no such user", return Result<T, E>, or Result<Option<T>, E> when missing is a normal outcome and failure is a separate one. That replaces returning null from one path and throwing from another.

The TypeScript habit, written in Rust

This is how the TypeScript version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.

struct Network {
    retries: Option<u32>,
}

fn main() {
    let network = Network { retries: Some(0) };
    let limit: u32 = network.retries + 1;
    println!("{limit}");
}

Practise this code in Playground ↗

Try it

Port the TypeScript pageSize and title shown at the top of solution.rs. Both take Option<&Settings>. A missing settings value or field uses the default (20, Untitled), but a configured 0 or an empty title is kept, exactly as ?? does. In the Crabrix app, open the compiler lab and complete the supplied functions in solution.rs. The reading question is a separate exercise.

What success looks like: The local compiler accepts the changed solution and the separate verification harness passes every case, including a configured 0 and an empty title. Printing a success message alone does not complete this lab.

Check your understanding

A lookup finds nothing when the key is missing, and fails when the storage is unreachable. Which return type gives the caller both facts?

Reveal the answer
  1. Option<T>
  2. Result<Option<T>, E>
  3. T, using null for both cases

Ok(Some(t)) means found, Ok(None) simply missing, and Err(e) carries the reason the lookup itself failed.

Go deeper

Common misconception: “unwrap_or is Rust's ||: it replaces any empty-looking value.”

unwrap_or replaces only None, like ??. Some(0) and Some(String::new()) are present values and are returned unchanged.

  1. Read the types: Config.network is an Option<Network> and Network.retries an Option<u32>: two separate places where data may be missing.
  2. Follow the chain: as_ref() borrows the network instead of moving it out of config. and_then(|n| n.retries) flattens the two options, and unwrap_or(3) handles both kinds of None.
  3. Compare with evidence: Run it: 3 0, so the configured zero survives. Insert .filter(|&n| n != 0) before unwrap_or and the second number becomes 3, the || behaviour.

Transfer challenge: Write fn retries(config: &Config) -> Option<u32> using ? on config.network.as_ref() instead of and_then. Then change the return type to Result<u32, String> and use ok_or to say which field was missing.

Connections

  • TypeScript unions vs enums: Discriminated unions as enums
  • Array methods vs iterators: Lazy iterator pipelines
Side-by-side comparison: the TypeScript version on the left, the Rust version on the right, then 4 rows naming what changes.
null, undefined and ?? vs Option: the same task in TypeScript and in Rust, with the differences that matter.
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: null, undefined and ?? vs Option (courseId "rust-for-nodejs-developers", lessonId "node-null-undefined-to-option")
Link: https://crabrix.com/learn/rust-for-nodejs-developers/node-null-undefined-to-option/
Goal: Use Option<T> when a value may be absent and Result<T, E> when the caller needs the reason; unwrap_or replaces ??, not ||.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-nodejs-developers/node-null-undefined-to-option) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.