Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created October 10, 2019 08:19
Show Gist options
  • Save rust-play/097e4155dfea7421ed3dc576fa580c9f to your computer and use it in GitHub Desktop.
Save rust-play/097e4155dfea7421ed3dc576fa580c9f to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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