Skip to content

Instantly share code, notes, and snippets.

@KodrAus
Last active August 22, 2016 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KodrAus/880cc3a7c24f80d964e2a05b9d078344 to your computer and use it in GitHub Desktop.
Save KodrAus/880cc3a7c24f80d964e2a05b9d078344 to your computer and use it in GitHub Desktop.
Rust Results
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