Display
We'll cover the following...
Using the format
macro, I can write a function that turns an i32
into a String
:
Press + to interact
fn stringy(x: i32) -> String {format!("{}", x)}fn main() {assert_eq!(stringy(42), "42".to_owned());println!("Success!");}
But we’re all about type parameters these days. Can we generalize this to any type T
?
Press + to interact
fn stringy<T>(x: T) -> String {format!("{}", x)}
Not like that apparently:
error[E0277]: `T` doesn't implement `std::fmt::Display`
--> src/main.rs:2:19
|
2 | format!("{}", x)
| ^ `T` cannot be formatted with the default
...