Tim
Tim

Reputation: 129

Where is the implementation of Display trait for &str

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?

Upvotes: 1

Views: 549

Answers (2)

jfMR
jfMR

Reputation: 24788

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.

Upvotes: 2

Chayim Friedman
Chayim Friedman

Reputation: 71350

It is not implemented directly for &str. Rather, there is a blanket implementation impl<T: ?Sized + Display> Display for &'_ T.

Upvotes: 0

Related Questions