What is the Any trait in Rust

The Any trait enables runtime type identification and safe downcasting of trait objects to their concrete types in Rust.

The Any trait in Rust allows you to downcast a trait object to a concrete type at runtime by checking its type identity. It is implemented for all types that are 'static and Sized, enabling dynamic type checking when the concrete type is not known at compile time.

use std::any::Any;

fn main() {
    let x: Box<dyn Any> = Box::new(5);
    if let Some(num) = x.downcast_ref::<i32>() {
        println!("Got an i32: {}", num);
    }
}