typeof for Rust
typeof for C c语言的typeof的应用: 1 2 3 #define SWAP(a, b) do { \ typeof(a) tmp = a; b = a; a = tmp; \ } while(0) 1 2 3 4 5 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) typeof for rust 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 use std::any::type_name; fn type_of<T>(_: T) -> &'static str { type_name::<T>() } fn main() { let x = 21; let y = 2.5; println!("{}", type_of(&y)); println!("{}", type_of(x)); println!("{}", type_of("abc")); println!("{}", type_of(String::from("abc"))); println!("{}", type_of(vec![1, 2, 3])); println!("{}", type_of(main)); println!("{}", type_of(&type_of::<i32>)); }