Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created March 16, 2018 13:25
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 rust-play/89e08e88e1907d5d18875efdeb4b3b4a to your computer and use it in GitHub Desktop.
Save rust-play/89e08e88e1907d5d18875efdeb4b3b4a to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::io::{Error, ErrorKind};
enum Command {
Cmd1,
Cmd2,
}
struct CmdParseResult ( Result<Command, Error> );
impl <'a>From<&'a str> for CmdParseResult {
fn from(value: &str) -> Self {
match value {
"cmd1" => CmdParseResult(Ok(Command::Cmd1)),
"cmd2" => CmdParseResult(Ok(Command::Cmd2)),
_ => CmdParseResult(Err(Error::new(ErrorKind::Other, "oh no!")))
}
}
}
fn main() {
let parsing_result: CmdParseResult = "cmd1".into();
match parsing_result {
CmdParseResult(Ok(cmd)) => match cmd {
Command::Cmd1 => println!("cmd1"),
Command::Cmd2 => println!("cmd2"),
},
CmdParseResult(Err(error)) => println!("{}", error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment