std-method

Option::and / or / xor

Select a second optional value only if the first is present (and), choose the first present value (or), or accept exactly one present value (xor). Arguments are evaluated before the call. Expected stdout (not executed): Some("ready") Some(5) None Common mistake: Using xor as a fallback when both values are present.

Formal Syntax & Structure

rust syntaxrust
fn and<U>(self, optb: Option<U>) -> Option<U>; fn or(self, optb: Option<T>) -> Option<T>; fn xor(self, optb: Option<T>) -> Option<T>

Working Example

Example Snippetrust
fn main() {    println!("{:?}", Some(2).and(Some("ready")));    println!("{:?}", None.or(Some(5)));    println!("{:?}", Some(2).xor(Some(5)));}
Rust Reference