I know that str implements the trait Display. However, It is said that &str also implements this trait.
I wanna inspect the implementation detail of Display trait for &str, but I searched the resource code base of Rust and didn't find it. It doesn't exist in the std::str or the std::fmt::Display crates.
Maybe I had miss something I didn't realize. Can anyone give me some clue about where to find it?
CodePudding user response:
First, str
implements Display
, i.e.: impl Display for str
.
Second, there is a blanket implementation that implements Display
for every reference type whose referent type also implements Display
, i.e., impl<'_, T> Display for &'_ T where T: Display ?Sized
Therefore, &str
implements Display
because its referent type, str
, also does.
CodePudding user response:
It is not implemented directly for &str
. Rather, there is a blanket implementation impl<T: ?Sized Display> Display for &'_ T
.