Skip to content

Instantly share code, notes, and snippets.

@ericmoritz
Last active February 19, 2018 17:21
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 ericmoritz/ff7982303fa4a0acb67bb182f8cb6218 to your computer and use it in GitHub Desktop.
Save ericmoritz/ff7982303fa4a0acb67bb182f8cb6218 to your computer and use it in GitHub Desktop.
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);
}
}
}
$ 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.
@ericmoritz
Copy link
Author

I'm doing something wrong with the failure crate. I'm not sure what.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment