Last active
August 22, 2016 10:26
-
-
Save KodrAus/880cc3a7c24f80d964e2a05b9d078344 to your computer and use it in GitHub Desktop.
Rust Results
This file contains hidden or 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
fn maybe_works(i: i32) -> Result<(), String> { | |
//The try! macro makes early return from errors easier. There's a '?' operator coming soon | |
//Composing error types and implementing Error can suck, so libraries like error_chain help | |
//Result has similar helpers to Option | |
try!(must_not_be_negative(i)); | |
Ok(()) | |
} | |
fn must_not_be_negative(i: i32) -> Result<(), &'static str> { | |
if i > 0 { | |
Ok(()) | |
} | |
else { | |
Err("error") | |
} | |
} | |
fn main() { | |
println!("{:?}", maybe_works(1)); | |
println!("{:?}", maybe_works(-1)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment