Trait core::any::Any1.0.0[][src]

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

trait 来模拟动态类型。

大多数类型实现 Any。但是,任何包含非 `static’ 引用的类型都不会。 有关更多详细信息,请参见 模块级文档

Required methods

获取 selfTypeId

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);
Run

Implementations

如果 boxed 类型与 T 相同,则返回 true

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

is_string(&0);
is_string(&"cookie monster".to_string());
Run

如果 boxed 的类型为 T,则返回一些引用,如果不是,则返回 None

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

print_if_string(&0);
print_if_string(&"cookie monster".to_string());
Run

如果 boxed 的类型为 T,则返回一些可变引用; 如果不是,则返回 None

Examples

use std::any::Any;

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

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

转发到在 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...");
    }
}

is_string(&0);
is_string(&"cookie monster".to_string());
Run

转发到在 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...");
    }
}

print_if_string(&0);
print_if_string(&"cookie monster".to_string());
Run

转发到在 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;
    }
}

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

转发到在 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...");
    }
}

is_string(&0);
is_string(&"cookie monster".to_string());
Run

转发到在 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...");
    }
}

print_if_string(&0);
print_if_string(&"cookie monster".to_string());
Run

转发到在 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;
    }
}

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

使用给定的格式化程序格式化该值。 Read more

使用给定的格式化程序格式化该值。 Read more

使用给定的格式化程序格式化该值。 Read more

Implementors