File size: 597 Bytes
998b6cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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]);
} |