liveBook · Manning

Any

As explained in Idiomatic Rust

The Rust Any trait enables runtime type identification and checked conversion to a concrete type for suitable values.

Use `Any` to recover a concrete type from a trait object when needed. Add an `as_any` method returning `&dyn Any`, then use `downcast_ref` to check for a concrete type; it returns `Some` when the type matches. section 2.2.6

How Idiomatic Rust covers it

How deep it goes
Demonstrates `as_any` and `downcast_ref` without further implementation detail.
Examples
Checks trait objects for `MyStruct1` or `MyStruct2` and calls their concrete methods.
What it assumes
Assumes familiarity with trait objects and trait implementations.
What you can do after
Apply checked downcasting with `as_any` and `downcast_ref`.
How it connects
Connects `Any` with trait objects and dynamic dispatch.

Also explained in

Compare how 2 books explain any →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Idiomatic Rust. The section links point to the parts of the book it draws on.