pub fn type_name_of_val<T>(_val: &T) -> &'static strwhere
T: ?Sized,
Returns the type name of the pointed-to value as a string slice.
This is the same as type_name::<T>(), but can be used where the type of a variable is not easily available.
Like type_name, this is intended for diagnostic use and the exact output is not guaranteed. It provides a best-effort description, but the output may change between versions of the compiler.
In short: use this for debugging, avoid using the output to affect program behavior. More information is available at type_name.
Additionally, this function does not resolve trait objects. This means that type_name_of_val(&7u32 as &dyn Debug) may return "dyn Debug", but will not return "u32" at this time.
Prints the default integer and float types.
use std::any::type_name_of_val;
let s = "foo";
let x: i32 = 1;
let y: f32 = 1.0;
assert!(type_name_of_val(&s).contains("str"));
assert!(type_name_of_val(&x).contains("i32"));
assert!(type_name_of_val(&y).contains("f32"));
© 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/fn.type_name_of_val.html