Where is the implementation of Display trait for &str - string

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?

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.

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

Related

How to document feature-gated derived traits?

I understand that I can use the cfg_attr attribute to conditionally derive Trait implementations for types. For example, A struct MyStruct which always implements Clone, Copy, and Debug, but which implements PartialEq if and only if the my-feature feature is enabled, can be defined as follows:
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "my-feature", derive(PartialEq)]
struct MyStruct;
But is it possible to make the generated documentation reflect this? I would like the rustdoc-generated documentation to state that PartialEq is implemented for MyStruct if and only if the my-feature feature is enabled.
So far, I have only accomplished two outcomes, neither of which I want:
If I compile the docs with the my-feature feature enabled, the generated documentation just says that MyStruct implements PartialEq, with no mention of feature gating
If I compile the docs without the my-feautre feature enabled, generated documentation does not indicate that PartialEq is or can be implemented for MyStruct.

Will GATs allow HKTs for non-associated generic parameters?

When looking into Higher-kinded types (HKTs), Generic associated types (GATs) constantly pop up as "the solution". They allow you to express things like this (which I think is really cool):
trait Foo {
type Bar<T>;
fn process<T>(&mut self, ctx: Self::Bar<T>);
}
I'm really excited about GATs being close to landing, but from what I understand it only enables HKTs for traits that use associated types. Or is there a way to express something like this too? (And if not, is there a feature flag or working issue for it?)
trait Foo<for<T> Bar> {
fn process<T>(&mut self, ctx: Self::Bar<T>);
}

Is there a way for me to use the standard Result<T, E> type inside a Substrate module?

Substrate already defines its own result type as Result<(), &'static str> and doesn't let me use the generic type. How can I use the Rust standard Result<T, E> type?
It's possible to use the Rust standard Result type in a module's private function, but not the dispatchable function.
You need to import it first by use rstd::result, then use it like result::Result<your-value-type, your-error-type>.

Why does implementing Display for a struct add the ability to call to_string() on it? [duplicate]

I have a type Foo that I want to be able to display to the end user as a string, is it more idiomatic to do this by implementing Display or by implementing ToString?
If Display is the way to go, how would I actually end up with a String? I suspect I need to make use of write!, but I'm not entirely sure how.
You should not implement ToString manually. The ToString trait is already implemented for all types which implement fmt::Display:
impl<T> ToString for T
where
T: Display + ?Sized,
{ /* ... */ }
If you implement Display, to_string() will be available on your type automatically.
fmt::Display is intended to be implemented manually for those select few types which should be displayed to the user, while fmt::Debug is expected to be implemented for all types in such a way that represents their internals most nicely (for most types this means that they should have #[derive(Debug)] on them).
In order to obtain the string representation of fmt::Debug output you need to use format!("{:?}", value), with {:?} being a placeholder for types which implement fmt::Debug.
RFC 565 defines guidelines for when to use fmt::Debug and fmt::Display.

How to find in documentation that to_string is available for &str?

I was trying to understand what's going on when reading a line, but I couldn't find that there is a method to_string in the documentation for str, even though I know it's there.
There is no direct link between &str and ToString::to_string in the docs because ToString has a blanket implementation:
impl<T> ToString for T where T: Display, T: ?Sized
This means that ToString is implemented for any item that implements Display. &str does implement Display.
(Moved from a comment).

Resources