option-chain-tool
A Rust macro that brings JavaScript-like optional chaining to Rust, allowing you to use the ? operator in any context - even in functions that don't return Option or Result.
Motivation
In Rust, the ? operator is a powerful tool for ergonomic error handling, but it comes with a significant limitation: it only works inside functions that return Option or Result. This restriction forces developers into verbose workarounds when working with nested optional values in regular functions.
The Problem
Consider a common scenario: you have deeply nested optional structures and want to safely access a value deep within. In Rust, you typically have three options, all with drawbacks:
- Verbose chaining with
and_thenandmap:
let value = test_struct.value
.and_then
.and_then;
This becomes increasingly unreadable as nesting increases.
- Manual unwrapping with pattern matching:
if let Some = &test_struct.value else
Even more verbose and error-prone.
- Wrapping everything in a helper function:
let value = get_nested_value;
Creates unnecessary function overhead for simple access patterns.
The Solution
JavaScript and TypeScript developers enjoy optional chaining (?.) that makes this trivial:
const value = test_struct?.?.?.;
option-chain brings this ergonomic experience to Rust with a simple macro that lets you use the natural ? operator anywhere:
let value = opt!;
Clean, readable, and efficient - without requiring your function to return Option.
Features
- ðŠķ Lightweight: Just a use native rust TokenStream, zero dependencies.
- ð Zero overhead: Compiles down to the same code as manual
if let Some(...) = ...chains. - ðŊ Intuitive: Uses Rust's familiar
?operator syntax. - ð Type-safe: Full compile-time type checking.
- ðĶ Works everywhere: Use in functions returning
(), concrete types, or anything else.
Installation
Add this to your Cargo.toml:
[]
= "0.10"
Usage
Basic Example
use opt;
Advanced Examples
Working with Vectors and Methods
use opt;
Accessing Required Fields Through Optional Chains
use opt;
Real-World Scenario
use opt;
// Regular function that doesn't return Option
Comparison
JavaScript/TypeScript:
const city = user?.?.?.;
Rust without option-chain:
let city = user.profile
.and_then
.and_then;
Rust with option-chain:
let city = opt!;
How It Works
The opt! macro transforms your code at compile time, converting the intuitive ? syntax into efficient if let Some(...) = ... chains. There's no runtime overhead - it's purely a syntactic convenience.
// What you write:
let a: = opt!;
// What the compiler sees (roughly):
let a: = if let Some = &user.profile else ;
When to Use
â Use option-chain when:
- Working with nested optional structures in functions that don't return
Option - You want to avoid verbose
and_thenchains - Accessing data from deeply nested API responses or configs
- You need readable optional access in utility functions
â Consider alternatives when:
- Your function already returns
OptionorResult(use native?operator) - You're working with flat structures (no nesting)
- You need custom error messages (consider explicit error handling)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.