trait

Any — inspect concrete types at runtime

Any supports checking and downcasting values whose types satisfy the static lifetime bound. A downcast checks the exact concrete type, not a conversion. Borrowed data with nonstatic references cannot generally be stored as Any. Prefer enums when the set of possible types is known. Expected stdout (not executed): Some(42) None Common mistake: Expecting downcast to numerically convert u32 to i32. Static Rust 2024 example; not a verified ByteMind executable assessment.

Formal Syntax & Structure

rust syntaxrust
dyn Any; downcast_ref::<T>() -> Option<&T>

Working Example

Example Snippetrust
use std::any::Any;fn main(){let value: &dyn Any=&42u32; println!("{:?} {:?}",value.downcast_ref::<u32>(),value.downcast_ref::<i32>());}
Rust Reference