用法
pub fn downcast_mut<T:Any>(&mut self) -> Option<&mut T>
如果它是类型 T
,则返回对装箱值的一些可变引用,如果不是,则返回 None
。
例子
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");
相关用法
- Rust core::any::Any.downcast_ref用法及代码示例
- Rust core::any::Any.type_id用法及代码示例
- Rust core::any::Any.is用法及代码示例
- Rust core::any::TypeId.of用法及代码示例
- Rust core::any::type_name_of_val用法及代码示例
- Rust core::any::type_name用法及代码示例
- Rust core::assert用法及代码示例
- Rust core::assert_matches::assert_matches用法及代码示例
- Rust core::alloc::Layout.extend用法及代码示例
- Rust core::assert_matches::debug_assert_matches用法及代码示例
- Rust core::array::IntoIter.new用法及代码示例
- Rust core::array::from_fn用法及代码示例
- Rust core::assert_eq用法及代码示例
- Rust core::assert_ne用法及代码示例
- Rust core::array::try_from_fn用法及代码示例
- Rust core::ascii::escape_default用法及代码示例
- Rust core::alloc::GlobalAlloc用法及代码示例
- Rust core::cell::Cell.as_slice_of_cells用法及代码示例
- Rust core::sync::atomic::AtomicU32.load用法及代码示例
- Rust core::sync::atomic::AtomicU32.fetch_update用法及代码示例
注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 core::any::Any.downcast_mut。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。