Trait Any

trait Any: 'static

A trait to emulate dynamic typing.

Most types implement Any. However, any type which contains a non-'static reference does not. See the module-level documentation for more details.

Required Methods

fn type_id(self: &Self) -> TypeId

Gets the TypeId of self.

If called on a dyn Any trait object (or a trait object of a subtrait of Any), this returns the TypeId of the underlying concrete type, not that of dyn Any itself.

Examples

use std::any::{Any, TypeId};

fn is_string(s: &dyn Any) -> bool {
    TypeId::of::<String>() == s.type_id()
}

assert_eq!(is_string(&0), false);
assert_eq!(is_string(&"cookie monster".to_string()), true);

Implementors