Skip to content

Instantly share code, notes, and snippets.

@inre
Created October 23, 2015 09:28
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 inre/eb540f76aee994e22021 to your computer and use it in GitHub Desktop.
Save inre/eb540f76aee994e22021 to your computer and use it in GitHub Desktop.
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