std-method
Option::and_then
Chain a lookup or checked operation that may itself return None. The closure runs only for Some. Unlike map, it does not introduce another Option layer. Expected stdout (not executed): Some(6) Common mistake: Using map with an optional-returning closure and overlooking Option<Option<U>>.
Formal Syntax & Structure
rust syntaxrust
fn and_then<U, F>(self, f: F) -> Option<U> where F: FnOnce(T) -> Option<U>Working Example
Example Snippetrust
fn main() { let amount = Some(12u32); let half = amount.and_then(|n| n.checked_div(2)); println!("{half:?}");}