Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 26, 2025 13:45
Show Gist options
  • Save rust-play/b80ed124b434bb8bcf101b0d6c335fec to your computer and use it in GitHub Desktop.
Save rust-play/b80ed124b434bb8bcf101b0d6c335fec to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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