Logo

Programming-Idioms

  • Rust

Idiom #94 Print the type of a variable

Print the name of the type of x. Explain if it is a static type or dynamic type.

This may not make sense in all languages.

#![feature(core_intrinsics)]
fn type_of<T>(_: &T) -> &'static str {
    std::intrinsics::type_name::<T>()
}

println!("{}", type_of(&x));

As of 2020-09 this is a nightly-only experimental API.
(type x)

New implementation...
< >
programming-idioms.org