[−][src]Crate libeither
Either struct
This is heavily influenced by the Either enum library. A struct version was required to be serializable to/from TOML, where enums are not valid. If you don't need struct specific serialization, you may want to use the enum Either instead.
Features
serialization
- Enable serialization via serde (on by default)unstable
- Enable unstable options (nightly only, off by default)
Examples
let mut left: Either<&str, &str> = Either::new_left("lefty"); // Check for left or right assert!(left.is_left()); assert!(!left.is_right()); // Check a reference to the contained value. assert_eq!(left.left_ref()?, &"lefty"); assert!(left.right_ref().is_err()); // Mutate the contained value. *(left.left_mut()?) = "left handed"; assert_eq!(left.left_ref()?, &"left handed"); assert!(left.right_mut().is_err()); // Flip the value let flipped = left.flip()?; assert_eq!(flipped.right_ref()?, &"left handed"); assert!(flipped.left_ref().is_err()); // Map a function over the left let mapped_left: Either<usize, _> = left.map_left(|x| x.len())?; assert_eq!(mapped_left.left_ref()?, &11); // Chain execution let new_left: Either<&str, &str> = Either::new_left("lefty"); let and_then_left: Either<usize, _> = new_left .and_then_left(|x| Either::new_left(x.len()))? .and_then_left(|x| Either::new_left(x * 10))?; assert_eq!(and_then_left.left_ref()?, &50); let new_right: Either<&str, &str> = Either::new_right("righty"); assert_eq!(new_right .and_then_left(|x: &str| Either::new_left(x.len()))?.right_ref()?, &"righty" );
Structs
Either | A struct representing either a left value, or a right value. |
Error | An error from the |
Type Definitions
Result | A result that must include an |