Logo

Programming-Idioms

History of Idiom 94 > diff from v26 to v27

Edit summary for version 27 by TheVTM:
[Rust] Fixed demo link

Version 26

2017-04-27, 09:17:11

Version 27

2017-04-29, 07:10:55

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

println!("{}", type_of(&x));
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.
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
Doc URL
https://doc.rust-lang.org/core/intrinsics/fn.type_name.html
Origin
http://stackoverflow.com/a/29168659/1255542
Origin
http://stackoverflow.com/a/29168659/1255542
Demo URL
https://is.gd/x55v7D
Demo URL
https://play.rust-lang.org/?gist=ed73485fc19b1c387e4572f85734f79e&version=nightly&backtrace=0