On page 298 of Programming Rust you can find the following statement
The standard library automatically implements the trivial conversion from each type to itself: every type
T
implementsFrom<T>
andInto<T>
However it doesn't actually say why that would be useful or why the standard library would automatically do that? What's the reason for this?
It can be convenient when writing functions/types with generic arguments. For instance, if I write a function:
fn do_something_with_string<S: Into<String>>(s: S) {}
That function will work equally for String
and anything which can be turned into one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With