I also have difficulties to come up with one, but maybe the following one can demonstrate the issues you can run into.
Consider this:
pub trait TrtA: From<String> {
type Associated;
}
impl TrtA for String {
type Associated = ();
}
We could also attempt to use Into
here:
pub trait TrtB: Sized
where
String: Into<Self>,
{
type Associated;
}
impl TrtB for String {
type Associated = ();
}
But now using TrtA
is easier than using TrtB
:
pub struct A<T: TrtA> {
pub field1: T,
pub field2: T::Associated,
}
pub struct B<T: TrtB>
where
String: Into<T>, // we need this
{
pub field1: T,
pub field2: T::Associated,
}
Here, struct B
requires an extra bound because the compiler can't deduce that T: TrtB
implies String: Into<T>
.
This gets worse with impl Trait
, I believe:
pub fn foo() -> A<impl TrtA> {
A {
field1: "Hello".to_string(),
field2: (),
}
}
pub fn bar() -> B<impl TrtB> {
B {
field1: "Hello".to_string(),
field2: (),
}
}
(Playground)
Errors:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `impl TrtB: From<String>` is not satisfied
--> src/lib.rs:38:17
|
38 | pub fn bar() -> B<impl TrtB> {
| ^^^^^^^^^^^^ the trait `From<String>` is not implemented for `impl TrtB`
|
= note: required for `String` to implement `Into<impl TrtB>`
note: required by a bound in `B`
--> src/lib.rs:25:13
|
23 | pub struct B<T: TrtB>
| - required by a bound in this struct
24 | where
25 | String: Into<T>, // we need this
| ^^^^^^^ required by this bound in `B`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to previous error
The same issue can happen vice versa, where using From
as bound makes the problems (and where I had to use Into
instead).