1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#![feature(type_name_of_val)]
#[allow(unused_macros)]
#[macro_export]
macro_rules! ptype {
($input:expr) => {
println!("{}", std::any::type_name_of_val($input));
};
}
#[allow(unused_macros)]
#[macro_export]
macro_rules! rtype {
($input:expr) => {
std::any::type_name_of_val($input)
};
}
#[test]
fn test_type() {
let x: u64 = 42;
let t = rtype!(&x);
ptype!(&x);
assert_eq!(t, "u64");
}
#[allow(dead_code)]
struct Tester {
x: u64,
y: f64,
}
#[test]
fn test_custom_type() {
let test_h = Tester { x: 42, y: 18.0 };
let t = rtype!(&test_h);
ptype!(&t);
assert_eq!(t, "ttype::Tester");
}
#[test]
fn test_custom_type_sub() {
let test_h = Tester { x: 42, y: 18.0 };
let t = rtype!(&test_h.x);
ptype!(&test_h.x);
assert_eq!(t, "u64");
}