Logo

Programming-Idioms

History of Idiom 94 > diff from v25 to v26

Edit summary for version 26 by TheVTM:
New Rust implementation by user [TheVTM]

Version 25

2016-11-10, 08:46:15

Version 26

2017-04-27, 09:17:11

Idiom #94 Print type of 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.

Idiom #94 Print type of 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.

Imports
#![feature(core_intrinsics)]
Code
fn type_of<T>(_: &T) -> String {
    format!("{}", unsafe { std::intrinsics::type_name::<T>() })
}

println!("{}", type_of(&x));
Comments bubble
As of 4/2017 this is a nightly-only experimental API.
Doc URL
https://doc.rust-lang.org/core/intrinsics/fn.type_name.html
Origin
http://stackoverflow.com/a/29168659/1255542
Demo URL
https://is.gd/x55v7D