std::any::Any - Rust
Description: API documentation for the Rust `Any` trait in crate `std`.
Keywords: rust, rustlang, rust-lang, Any

# 1.0.0[−][src]Trait std::any::Any

    pub trait Any: 'static {
        fn type_id(&self) -> TypeId;
    }

A type 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) -> TypeId`
1.34.0

Gets the `TypeId` of `self`.

# Examples

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

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

    fn main() {
        assert_eq!(is_string(&0), false);
        assert_eq!(is_string(&"cookie monster".to_string()), true);
    }Run

Loading content...

## Methods

### `impl dyn Any + 'static`[src]

#### `pub fn is<T>(&self) -> bool where
T: Any,` [src]

Returns `true` if the boxed type is the same as `T`.

# Examples

    use std::any::Any;

    fn is_string(s: &dyn Any) {
        if s.is::<String>() {
            println!("It's a string!");
        } else {
            println!("Not a string...");
        }
    }

    fn main() {
        is_string(&0);
        is_string(&"cookie monster".to_string());
    }Run

#### `pub fn downcast_ref<T>(&self) -> Option<&T> where
T: Any,` [src]

Returns some reference to the boxed value if it is of type `T`, or `None` if it isn't.

# Examples

    use std::any::Any;

    fn print_if_string(s: &dyn Any) {
        if let Some(string) = s.downcast_ref::<String>() {
            println!("It's a string({}): '{}'", string.len(), string);
        } else {
            println!("Not a string...");
        }
    }

    fn main() {
        print_if_string(&0);
        print_if_string(&"cookie monster".to_string());
    }Run

#### `pub fn downcast_mut<T>(&mut self) -> Option<&mut T> where
T: Any,` [src]

Returns some mutable reference to the boxed value if it is of type `T`, or `None` if it isn't.

# Examples

    use std::any::Any;

    fn modify_if_u32(s: &mut dyn Any) {
        if let Some(num) = s.downcast_mut::<u32>() {
            *num = 42;
        }
    }

    fn main() {
        let mut x = 10u32;
        let mut s = "starlord".to_string();

        modify_if_u32(&mut x);
        modify_if_u32(&mut s);

        assert_eq!(x, 42);
        assert_eq!(&s, "starlord");
    }Run

### `impl dyn Any + 'static + Send`[src]

#### `pub fn is<T>(&self) -> bool where
T: Any,` [src]

Forwards to the method defined on the type `Any`.

# Examples

    use std::any::Any;

    fn is_string(s: &(dyn Any + Send)) {
        if s.is::<String>() {
            println!("It's a string!");
        } else {
            println!("Not a string...");
        }
    }

    fn main() {
        is_string(&0);
        is_string(&"cookie monster".to_string());
    }Run

#### `pub fn downcast_ref<T>(&self) -> Option<&T> where
T: Any,` [src]

Forwards to the method defined on the type `Any`.

# Examples

    use std::any::Any;

    fn print_if_string(s: &(dyn Any + Send)) {
        if let Some(string) = s.downcast_ref::<String>() {
            println!("It's a string({}): '{}'", string.len(), string);
        } else {
            println!("Not a string...");
        }
    }

    fn main() {
        print_if_string(&0);
        print_if_string(&"cookie monster".to_string());
    }Run

#### `pub fn downcast_mut<T>(&mut self) -> Option<&mut T> where
T: Any,` [src]

Forwards to the method defined on the type `Any`.

# Examples

    use std::any::Any;

    fn modify_if_u32(s: &mut (dyn Any + Send)) {
        if let Some(num) = s.downcast_mut::<u32>() {
            *num = 42;
        }
    }

    fn main() {
        let mut x = 10u32;
        let mut s = "starlord".to_string();

        modify_if_u32(&mut x);
        modify_if_u32(&mut s);

        assert_eq!(x, 42);
        assert_eq!(&s, "starlord");
    }Run

### `impl dyn Any + 'static + Sync + Send`[src]

#### `pub fn is<T>(&self) -> bool where
T: Any,`
1.28.0
[src]

Forwards to the method defined on the type `Any`.

# Examples

    use std::any::Any;

    fn is_string(s: &(dyn Any + Send + Sync)) {
        if s.is::<String>() {
            println!("It's a string!");
        } else {
            println!("Not a string...");
        }
    }

    fn main() {
        is_string(&0);
        is_string(&"cookie monster".to_string());
    }Run

#### `pub fn downcast_ref<T>(&self) -> Option<&T> where
T: Any,`
1.28.0
[src]

Forwards to the method defined on the type `Any`.

# Examples

    use std::any::Any;

    fn print_if_string(s: &(dyn Any + Send + Sync)) {
        if let Some(string) = s.downcast_ref::<String>() {
            println!("It's a string({}): '{}'", string.len(), string);
        } else {
            println!("Not a string...");
        }
    }

    fn main() {
        print_if_string(&0);
        print_if_string(&"cookie monster".to_string());
    }Run

#### `pub fn downcast_mut<T>(&mut self) -> Option<&mut T> where
T: Any,`
1.28.0
[src]

Forwards to the method defined on the type `Any`.

# Examples

    use std::any::Any;

    fn modify_if_u32(s: &mut (dyn Any + Send + Sync)) {
        if let Some(num) = s.downcast_mut::<u32>() {
            *num = 42;
        }
    }

    fn main() {
        let mut x = 10u32;
        let mut s = "starlord".to_string();

        modify_if_u32(&mut x);
        modify_if_u32(&mut s);

        assert_eq!(x, 42);
        assert_eq!(&s, "starlord");
    }Run

## Trait Implementations

### `impl Debug for dyn Any + 'static + Send`[src]

#### `fn fmt(&self, f: &mut Formatter) -> Result<(), Error>`[src]

Formats the value using the given formatter. Read more

### `impl Debug for dyn Any + 'static`[src]

#### `fn fmt(&self, f: &mut Formatter) -> Result<(), Error>`[src]

Formats the value using the given formatter. Read more

### `impl Debug for dyn Any + 'static + Sync + Send`
1.28.0
[src]

#### `fn fmt(&self, f: &mut Formatter) -> Result<(), Error>`[src]

Formats the value using the given formatter. Read more

## Implementors

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `fn type_id(&self) -> TypeId`[src]

Loading content...

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)