Rust Reference ยท Standard Library Modules
The std::any and std::marker modules
Reference for Rust std::any and std::marker: Any, TypeId, type_name, downcasting, plus the marker traits Send, Sync, Sized, Copy, Unpin and PhantomData.
Two small modules describe properties of types rather than behavior. std::any offers a limited form of runtime type information: the Any trait, TypeId, and type_name. std::marker holds the traits and types that tell the compiler facts about a type: Send, Sync, Sized, Copy, Unpin and PhantomData.
Any lets you hold a value of unknown type behind &dyn Any or Box<dyn Any> and ask "are you really an i32?" with downcast_ref. Every 'static type implements it automatically. The marker traits have no methods; they are labels that the compiler either derives from a type's fields (Send, Sync, Unpin, Sized) or that you opt in to (Copy).
use std::any::Any; let boxed: Box<dyn Any> = Box::new(42i32); let n = boxed.downcast_ref::<i32>();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
| Item | Description |
|---|---|
trait Any: 'static |
Method type_id(). Implemented for every 'static type. dyn Any has is, downcast_ref, downcast_mut. |
Box<dyn Any> |
downcast::<T>() returns Result<Box<T>, Box<dyn Any>>. |
TypeId::of::<T>() |
A comparable, hashable identifier for a type. |
type_name::<T>(), type_name_of_val(&v) |
A string describing the type, for diagnostics only. type_name_of_val is stable since 1.76. |
Send |
Safe to move to another thread. |
Sync |
&T is safe to share between threads (T: Sync means &T: Send). |
Sized |
Size known at compile time. Opt out of the implicit bound with ?Sized. |
Copy |
Bitwise duplication on assignment; requires Clone. |
Unpin, PhantomPinned |
Pinning markers; most types are Unpin. |
PhantomData<T> |
Zero-sized field that tells the compiler the type "owns" or "uses" a T. |
Usage notes
Anyneeds'static, so it cannot be used with borrowed data such as&'a strinside a struct with a lifetime.type_nameis not stable. Its text may change between compiler versions and it is not guaranteed unique. Never compare or parse it; useTypeIdfor identity.- Downcasting is a last resort. A trait with the needed methods, or an enum, usually models "one of several types" better.
SendandSyncare auto traits. A struct isSendif all fields are.Rcand raw pointers are neither;CellandRefCellareSendbut notSync.- Implementing
Send/Syncmanually isunsafe, because you promise to uphold the thread-safety rules yourself. PhantomDatacosts nothing at runtime but affects variance, drop checking and auto traits:PhantomData<*const T>makes the struct neitherSendnorSync.?Sizedis needed to acceptstr,[T]anddyn Trait:fn f<T: ?Sized>(x: &T).Copytypes cannot implementDrop, and all their fields must beCopytoo.