-
-
Save RandyMcMillan/65f6bcae9565dfe00b646f11ca4aa233 to your computer and use it in GitHub Desktop.
print_type.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::any::Any; | |
use std::fmt::Debug; | |
fn print_type_name<T: Any + Debug>(value: &T) { | |
println!("Type of {:?} is: {}", value, std::any::type_name::<T>()); | |
} | |
fn main() { | |
let i: i32 = 42; | |
print_type_name(&i); | |
let f: f64 = 3.14159; | |
print_type_name(&f); | |
let b: bool = true; | |
print_type_name(&b); | |
let c: char = 'A'; | |
print_type_name(&c); | |
let s: &str = "Hello, world!"; | |
print_type_name(&s); | |
let v: Vec<i32> = vec![1, 2, 3]; | |
print_type_name(&v); | |
let t: (i32, String) = (10, "hello".to_string()); | |
print_type_name(&t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment