Rust 打印变量类型

Rust About 269 words

方法一

直接打印

fn print_type_of<T>(_: T) {
    println!("type is {}", std::any::type_name::<T>())
}

方法二

返回全生命周期的字面量引用。

type_name源码就是返回的&'static str

fn type_of<T>(_: T) -> &'static str {
    std::any::type_name::<T>()
}

备注

Rust 1.38中添加该特性。

Views: 779 · Posted: 2023-04-21

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh