Use TypeId::of::<T>() to get a unique identifier for a type at runtime, then compare it against another TypeId to check for equality. This works for types that implement Any.
use std::any::TypeId;
fn main() {
let id = TypeId::of::<String>();
let is_string = id == TypeId::of::<String>();
println!("Is String: {}", is_string);
}
For dynamic dispatch, cast a trait object to Any and call type_id() on the reference.
use std::any::{Any, TypeId};
fn main() {
let s: &dyn Any = &String::from("hello");
let id = s.type_id();
let is_string = id == TypeId::of::<String>();
println!("Is String: {}", is_string);
}