Created
March 21, 2014 11:16
-
-
Save bvssvni/9684061 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = Some(1); | |
let y = Some(2); | |
let z = monad!( | |
None { | |
_ => x, | |
Some(x) => y, | |
Some(y) => Some(x+y) | |
} | |
); | |
assert_eq!(z, Some(3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works because in Rust you can shadow variable names. Each arm matches against the result of the previous arm. Because the pattern matching allows binding of variables to a name, you can 'unwrap' x and y before adding them together.