Logo

Programming-Idioms

History of Idiom 94 > diff from v28 to v29

Edit summary for version 29 by Boiethios:
[Rust] No need to allocate a String

Version 28

2017-06-09, 10:38:29

Version 29

2018-01-10, 13:05:36

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) -> &'static str {
    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://play.rust-lang.org/?gist=ed73485fc19b1c387e4572f85734f79e&version=nightly&backtrace=0
Demo URL
https://play.rust-lang.org/?gist=ed73485fc19b1c387e4572f85734f79e&version=nightly&backtrace=0