W3cubDocs

/Rust

Trait std::any::Any

pub trait Any: 'static {
    fn get_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

🔬 This is a nightly-only experimental API. (get_type_id #27745)this method will likely be replaced by an associated static

Gets the TypeId of self.

Examples

#![feature(get_type_id)]

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

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

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

Methods

impl Any + 'static [src]

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

Examples

use std::any::Any;

fn is_string(s: &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());
}

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: &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());
}

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 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");
}

impl Any + 'static + Send [src]

Forwards to the method defined on the type Any.

Examples

use std::any::Any;

fn is_string(s: &(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());
}

Forwards to the method defined on the type Any.

Examples

use std::any::Any;

fn print_if_string(s: &(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());
}

Forwards to the method defined on the type Any.

Examples

use std::any::Any;

fn modify_if_u32(s: &mut (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");
}

Trait Implementations

impl Debug for Any + 'static [src]

Formats the value using the given formatter.

impl Debug for Any + 'static + Send [src]

Formats the value using the given formatter.

Implementors

  • impl<T> Any for T where T: 'static + ?Sized

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/any/trait.Any.html