@@ -1,6 +1,6 @@
|
||||
# `impl Trait` in argument position
|
||||
|
||||
In the previous section, we saw how `impl Trait` can be used to return a type without specifying its name.
|
||||
In the previous section, we saw how `impl Trait` can be used to return a type without specifying its name.\
|
||||
The same syntax can also be used in **argument position**:
|
||||
|
||||
```rust
|
||||
@@ -11,7 +11,7 @@ fn print_iter(iter: impl Iterator<Item = i32>) {
|
||||
}
|
||||
```
|
||||
|
||||
`print_iter` takes an iterator of `i32`s and prints each element.
|
||||
`print_iter` takes an iterator of `i32`s and prints each element.\
|
||||
When used in **argument position**, `impl Trait` is equivalent to a generic parameter with a trait bound:
|
||||
|
||||
```rust
|
||||
@@ -27,6 +27,6 @@ where
|
||||
|
||||
## Downsides
|
||||
|
||||
As a rule of thumb, prefer generics over `impl Trait` in argument position.
|
||||
Generics allow the caller to explicitly specify the type of the argument, using the turbofish syntax (`::<>`),
|
||||
which can be useful for disambiguation. That's not the case with `impl Trait`.
|
||||
As a rule of thumb, prefer generics over `impl Trait` in argument position.\
|
||||
Generics allow the caller to explicitly specify the type of the argument, using the turbofish syntax (`::<>`),
|
||||
which can be useful for disambiguation. That's not the case with `impl Trait`.
|
||||
|
||||
Reference in New Issue
Block a user