« Back to Index

Rust: Print the type of a reference

View original Gist on GitHub

Tags: #rust #type #reflection

use std::any::type_name;

fn type_of<T>(_: &T) {
    println!("{}", type_name::<T>())
}

fn main() {
    type_of(&"foo");               // &str
    type_of(&String::from("foo")); // alloc::string::String
    type_of(&123);                 // i32
    type_of(&1.23);                // f64
}