Skip to content
Tutorials Center
My learning

Topics

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).

Rust
use std::any::Any;

let boxed: Box<dyn Any> = Box::new(42i32);
let n = boxed.downcast_ref::<i32>();

Try it

Rust Any, TypeId and type_name

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.

Rust PhantomData and marker traits

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

  • Any needs 'static, so it cannot be used with borrowed data such as &'a str inside a struct with a lifetime.
  • type_name is not stable. Its text may change between compiler versions and it is not guaranteed unique. Never compare or parse it; use TypeId for identity.
  • Downcasting is a last resort. A trait with the needed methods, or an enum, usually models "one of several types" better.
  • Send and Sync are auto traits. A struct is Send if all fields are. Rc and raw pointers are neither; Cell and RefCell are Send but not Sync.
  • Implementing Send/Sync manually is unsafe, because you promise to uphold the thread-safety rules yourself.
  • PhantomData costs nothing at runtime but affects variance, drop checking and auto traits: PhantomData<*const T> makes the struct neither Send nor Sync.
  • ?Sized is needed to accept str, [T] and dyn Trait: fn f<T: ?Sized>(x: &T).
  • Copy types cannot implement Drop, and all their fields must be Copy too.