Skip to content

Commit 6b75f9e

Browse files
authored
Update impl-trait.md (#249)
Adding an extra speaker note why `impl Display` is used very appropriately here and what would go wrong if we tried `T: Display`.
1 parent cde1c43 commit 6b75f9e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/generics/impl-trait.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@ The meaning of `impl Trait` is a bit different in the different positions.
2828
without naming the type. This can be useful when you don't want to expose the concrete type in a
2929
public API.
3030

31+
This example is great, because it uses `impl Display` twice. It helps to explain that
32+
nothing here enforces that it is _the same_ `impl Display` type. If we used a single
33+
`T: Display`, it would enforce the constraint that input `T` and return `T` type are the same type.
34+
It would not work for this particular function, as the type we expect as input is likely not
35+
what `format!` returns. If we wanted to do the same via `: Display` syntax, we'd need two
36+
independent generic parameters.
37+
3138
</details>

0 commit comments

Comments
 (0)