The below code compiles and runs fine:
use std::fmt::Display;
fn display(x: &str) {
println!("{}", x);
}
fn main() {
let s: &str = "hi there";
display(s);
}
However, if you change the display
function to be
fn display(x: &Display)
It gives the following error:
src/main.rs:9:13: 9:14 error: the trait `core::marker::Sized` is not implemented for the type `str` [E0277]
src/main.rs:9 display(s);
By changing display(s)
to display(&s)
it works again.
What is going on here? Clearly the type is &str
, but when &Display
is the input argument it doesn't recognize that.
Note: &34
also works as an argument just fine. Is this because Display
is actually implemented for &str
and not str
?