用法
pub fn type_name<T>() -> &'static str where T:?Sized,
以字符串切片的形式返回类型的名称。
注意
这用于诊断用途。返回的字符串的确切内容和格式没有指定,只是对类型的尽力说明。例如,type_name::<Option<String>>()
可能返回的字符串包括 "Option<String>"
和 "std::option::Option<std::string::String>"
。
返回的字符串不能被认为是一个类型的唯一标识符,因为多个类型可能映射到同一个类型名称。同样,不能保证一个类型的所有部分都会出现在返回的字符串中:例如,当前不包括生命周期说明符。此外,输出可能会在编译器版本之间发生变化。
当前实现使用与编译器诊断和调试信息相同的基础结构,但这不能保证。
例子
assert_eq!(
std::any::type_name::<Option<String>>(),
"core::option::Option<alloc::string::String>",
);
相关用法
- Rust std::any::type_name_of_val用法及代码示例
- Rust std::any::Any.downcast_mut用法及代码示例
- Rust std::any::Any.is用法及代码示例
- Rust std::any::Any.downcast_ref用法及代码示例
- Rust std::any::Any.type_id用法及代码示例
- Rust std::any::TypeId.of用法及代码示例
- Rust std::array::try_from_fn用法及代码示例
- Rust std::alloc::GlobalAlloc用法及代码示例
- Rust std::assert用法及代码示例
- Rust std::alloc::Layout.extend用法及代码示例
- Rust std::assert_eq用法及代码示例
- Rust std::alloc::alloc用法及代码示例
- Rust std::ascii::AsciiExt用法及代码示例
- Rust std::array::IntoIter.new用法及代码示例
- Rust std::assert_matches::debug_assert_matches用法及代码示例
- Rust std::ascii::escape_default用法及代码示例
- Rust std::assert_matches::assert_matches用法及代码示例
- Rust std::alloc::System用法及代码示例
- Rust std::assert_ne用法及代码示例
- Rust std::array::from_fn用法及代码示例
注:本文由堆栈答案筛选整理自rust-lang.org大神的英文原创作品 Function std::any::type_name。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。