← Back to Gists

Question mark operator

📝 Rust
daniel_martin9
daniel_martin9 · Level 1 ·

The question mark operator in Rust unwraps a Result or Option, returning the success value or propagating the error/None from the current function.

Rust
fn firsteven(numbers: &[i32]) -> Option<i32> { let first = numbers.first()?; if first % 2 == 0 { Some(first) } else { None }
} fn main() { let nums = [1, 3, 5, 8, 9]; match firsteven(&nums) { Some(n) => println!("Found even: {}", n), None => println!("No even number"), }
}

Comments

No comments yet. Start the discussion.