Guest User

Untitled

a guest
Aug 14th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. fn foo(input: Option<i32>) -> Option<i32> {
  2. if input.is_none() {
  3. return None;
  4. }
  5.  
  6. let input = input.unwrap();
  7. if input < 0 {
  8. return None;
  9. }
  10. Some(input)
  11. }
  12.  
  13. fn bar(input: Option<i32>) -> Result<i32, ErrNegative> {
  14. match foo(input) {
  15. Some(n) => Ok(n),
  16. None => Err(ErrNegative),
  17. }
  18. }
  19.  
  20. #[derive(Debug)]
  21. struct ErrNegative;
  22.  
  23. fn main() {
  24. println!("{:?}", foo(Some(0)));
  25. println!("{:?}", foo(None));
  26. println!("{:?}", bar(Some(42)));
  27. println!("{:?}", bar(None));
  28. }
Add Comment
Please, Sign In to add comment