Last active
February 19, 2018 17:21
-
-
Save ericmoritz/ff7982303fa4a0acb67bb182f8cb6218 to your computer and use it in GitHub Desktop.
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
extern crate failure; | |
#[macro_use] | |
extern crate failure_derive; | |
use std::process; | |
use std::io::{self, Read}; | |
use failure::Error; | |
#[derive(Debug, Fail)] | |
enum MainErr { | |
#[fail(display = "No Harrys allowed")] | |
NoHarrys | |
} | |
fn get_name() -> Result<String, Error> { | |
let mut buffer = String::new(); | |
io::stdin().read_to_string(&mut buffer)?; | |
let name = buffer.trim_right(); | |
match name { | |
"Harry" => Err(MainErr::NoHarrys), | |
_ => Ok(String::from(name)) | |
} | |
} | |
fn run() -> Result<(), Error> { | |
let name = get_name()?; | |
println!("Hello, {}!", name); | |
Ok(()) | |
} | |
fn main() { | |
match run() { | |
Ok(_) => (), | |
Err(e) => { | |
eprintln!("{}\n\n {}", e, e.backtrace()); | |
process::exit(1); | |
} | |
} | |
} |
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
$ cargo run | |
Compiling hellostdin v0.1.0 (file:///Users/emoritz/Data/Projects/hellostdin) | |
error[E0308]: mismatched types | |
--> src/main.rs:21:24 | |
| | |
21 | "Harry" => Err(MainErr::NoHarrys), | |
| ^^^^^^^^^^^^^^^^^ expected struct `failure::Error`, found enum `MainErr` | |
| | |
= note: expected type `failure::Error` | |
found type `MainErr` | |
error: aborting due to previous error | |
error: Could not compile `hellostdin`. | |
To learn more, run the command again with --verbose. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm doing something wrong with the failure crate. I'm not sure what.