Created
March 16, 2018 13:25
-
-
Save rust-play/89e08e88e1907d5d18875efdeb4b3b4a to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains 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
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