Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Forked from rust-play/playground.rs
Last active January 26, 2025 13:46
Show Gist options
  • Save RandyMcMillan/65f6bcae9565dfe00b646f11ca4aa233 to your computer and use it in GitHub Desktop.
Save RandyMcMillan/65f6bcae9565dfe00b646f11ca4aa233 to your computer and use it in GitHub Desktop.
print_type.rs
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