skip to content
Ahmed Hossam

TIL: Rust Option Methods

Today I learned about some lesser-known Option methods in Rust that can make code cleaner:

  • Option::inspect() - Run a closure on the value if Some, useful for logging
  • Option::filter() - Convert Some(x) to None if predicate returns false
  • Option::zip() - Combine two Options into Option<(A, B)>
let x = Some(5);
x.inspect(|v| println!("Value: {v}"))
.filter(|v| *v > 3)
.zip(Some("hello"))

These are great for building pipelines without early returns!