Created
October 10, 2019 08:19
-
-
Save rust-play/097e4155dfea7421ed3dc576fa580c9f 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::fmt::Debug; | |
//trait ReturnType: Debug + Clone {} | |
// | |
//impl ReturnType for String {} | |
//impl ReturnType for i32 {} | |
#[derive(Debug)] | |
enum Command { | |
Create, | |
Get(usize), | |
} | |
#[derive(Debug)] | |
enum Ret { | |
RetNone, | |
RetStr(String), | |
RetI32(i32) | |
} | |
fn handle_command(command: Command) -> Result<Ret, Box<dyn std::error::Error>> { | |
match command { | |
Command::Create => Ok(Ret::RetStr("new_string".to_string())), | |
Command::Get(index) => Ok(Ret::RetI32(index as i32 + 1)), | |
} | |
} | |
fn main() { | |
let get_value = handle_command(Command::Get(1)); | |
println!("get_value: {:?}", get_value); | |
let create_value = handle_command(Command::Create); | |
println!("get_value: {:?}", create_value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment