pub trait Is {
// Required method
fn is<T>() -> bool
where T: Any;
}
Expand description
Checks if the current type “is” another type, using a TypeId
equality comparison.
Required Methods§
Sourcefn is<T>() -> boolwhere
T: Any,
fn is<T>() -> boolwhere
T: Any,
Checks if the current type “is” another type, using a TypeId
equality comparison.
This is most useful in the context of generic logic.
fn greet_if_u32<T: Any>() {
if T::is::<u32>() {
println!("Hello");
}
}
// this will print "Hello"
greet_if_u32::<u32>();
// this will not print "Hello"
greet_if_u32::<String>();
assert!(u32::is::<u32>());
assert!(!usize::is::<u32>());
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.