// run-pass #![feature(try_trait)] use std::ops::Try; enum MyResult { Awesome(T), Terrible(U) } impl Try for MyResult { type Ok = U; type Error = V; fn from_ok(u: U) -> MyResult { MyResult::Awesome(u) } fn from_error(e: V) -> MyResult { MyResult::Terrible(e) } fn into_result(self) -> Result { match self { MyResult::Awesome(u) => Ok(u), MyResult::Terrible(e) => Err(e), } } } fn f(x: i32) -> Result { if x == 0 { Ok(42) } else { let y = g(x)?; Ok(y) } } fn g(x: i32) -> MyResult { let _y = f(x - 1)?; MyResult::Terrible("Hello".to_owned()) } fn h() -> MyResult { let a: Result = Err("Hello"); let b = a?; MyResult::Awesome(b) } fn i() -> MyResult { let a: MyResult = MyResult::Terrible("Hello"); let b = a?; MyResult::Awesome(b) } fn main() { assert!(f(0) == Ok(42)); assert!(f(10) == Err("Hello".to_owned())); let _ = h(); let _ = i(); }