How to check the data type of a variable in Rust

1 Answer

0 votes
fn print_type_of<T>(_: &T) {
    println!("{}", std::any::type_name::<T>())
}

fn main() {
    let a = "Rust";
    let b = 42;
    let c = 3.14;
    let d = false;
    let e: f32 = 787.007;
    let f: i8 = 12;
    let g: char = 'a';
    let h = vec!['r', 'u', 's', 't'];
    
    print_type_of(&a); 
    print_type_of(&b); 
    print_type_of(&c); 
    print_type_of(&d); 
    print_type_of(&e); 
    print_type_of(&f); 
    print_type_of(&g); 
    print_type_of(&h); 
}



/*
run:

&str
i32
f64
bool
f32
i8
char
alloc::vec::Vec<char>

*/

 



answered Jul 8, 2024 by avibootz

Related questions

1 answer 65 views
1 answer 64 views
1 answer 60 views
1 answer 64 views
1 answer 80 views
80 views asked Dec 27, 2022 by avibootz
...