Trait core::any::Any 1.0.0[−][src]
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 get_type_id(&self) -> TypeId
[−]
🔬 This is a nightly-only experimental API. (get_type_id
#27745)
this method will likely be replaced by an associated static
Methods
impl Any
[src]
[−]
impl Any
pub fn is<T: Any>(&self) -> bool
[src]
[−]
pub fn is<T: Any>(&self) -> bool
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()); }Run
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
[src]
[−]
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
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()); }Run
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
[src]
[−]
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
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"); }Run
impl Any + Send
[src]
[−]
impl Any + Send
pub fn is<T: Any>(&self) -> bool
[src]
[−]
pub fn is<T: Any>(&self) -> bool
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()); }Run
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
[src]
[−]
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
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()); }Run
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
[src]
[−]
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
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"); }Run
impl Any + Send + Sync
[src]
[−]
impl Any + Send + Sync
pub fn is<T: Any>(&self) -> bool
1.28.0[src]
[−]
pub fn is<T: Any>(&self) -> bool
1.28.0
[src]Forwards to the method defined on the type Any
.
Examples
use std::any::Any; fn is_string(s: &(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: Any>(&self) -> Option<&T>
1.28.0[src]
[−]
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
1.28.0
[src]Forwards to the method defined on the type Any
.
Examples
use std::any::Any; fn print_if_string(s: &(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: Any>(&mut self) -> Option<&mut T>
1.28.0[src]
[−]
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
1.28.0
[src]Forwards to the method defined on the type Any
.
Examples
use std::any::Any; fn modify_if_u32(s: &mut (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 Any
[src]
[+]
impl Debug for Any
impl Debug for Any + Send
[src]
[+]
impl Debug for Any + Send
impl Debug for Any + Send + Sync
1.28.0[src]
[+]
impl Debug for Any + Send + Sync
1.28.0
[src]Implementors
impl<T: 'static + ?Sized> Any for T