The error E0277 occurs because the type you are trying to format with {} does not implement the std::fmt::Display trait. Add a Display bound to your generic type parameter or implement the trait for your custom type. For generic functions, add T: Display to the type constraints:
use std::fmt::Display;
fn print_value<T: Display>(value: T) {
println!("The value is: {}", value);
}
For custom structs, implement the trait manually:
use std::fmt;
struct MyStruct {
value: i32,
}
impl fmt::Display for MyStruct {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MyStruct({})", self.value)
}
}
If you only need to see the value for debugging and don't need custom formatting, use the {:?} format specifier instead, which requires the Debug trait.