fn describe(x: &dyn std::any::Any) { | |
match x { | |
x if x.is::<i32>() => { | |
if let Some(val) = x.downcast_ref::<i32>() { | |
match val { | |
1 => println!("one"), | |
2 => println!("two"), | |
_ => println!("an integer"), | |
} | |
} | |
} | |
x if x.is::<&str>() => { | |
println!("a string"); | |
} | |
_ => { | |
println!("something else"); | |
} | |
} | |
} | |
fn main() { | |
describe(&1); | |
describe(&2); | |
describe(&"hello"); | |
describe(&vec![1, 2, 3]); | |
} |