Created
October 23, 2015 09:28
-
-
Save inre/eb540f76aee994e22021 to your computer and use it in GitHub Desktop.
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; | |
use std::any::Any; | |
use std::io; | |
// Logger function for any type that implements Debug. | |
fn log<T: Any + Debug>(value: &T) { | |
let value_any = value as &Any; | |
// try to convert our value to a String. If successful, we want to | |
// output the String's length as well as its value. If not, it's a | |
// different type: just print it out unadorned. | |
match value_any.downcast_ref::<String>() { | |
Some(as_string) => { | |
println!("String ({}): {}", as_string.len(), as_string); | |
} | |
None => { | |
println!("{:?}", value); | |
} | |
} | |
} | |
fn main() { | |
loop { | |
let mut input = String::new(); | |
println!("Type:"); | |
match io::stdin().read_line(&mut input) { | |
Ok(n) => log(&input), | |
Err(error) => println!("error: {}", error), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment