impl Trait
impl Trait
can be used in two locations:
- as an argument type
- as a return type
As an argument type
If your function is generic over a trait but you don't mind the specific type, you can simplify the function declaration using impl Trait
as the type of the argument.
For example, consider the following code:
parse_csv_document
is generic, allowing it to take any type which implements BufRead, such as BufReader<File>
or [u8]
,
but it's not important what type R
is, and R
is only used to declare the type of src
, so the function can also be written as:
Note that using impl Trait
as an argument type means that you cannot explicitly state what form of the function you use, i.e. parse_csv_document::<std::io::Empty>(std::io::empty())
will not work with the second example.
As a return type
If your function returns a type that implements MyTrait
, you can write its
return type as -> impl MyTrait
. This can help simplify your type signatures quite a lot!
More importantly, some Rust types can't be written out. For example, every
closure has its own unnamed concrete type. Before impl Trait
syntax, you had
to allocate on the heap in order to return a closure. But now you can do it all
statically, like this:
You can also use impl Trait
to return an iterator that uses map
or filter
closures! This makes using map
and filter
easier. Because closure types don't
have names, you can't write out an explicit return type if your function returns
iterators with closures. But with impl Trait
you can do this easily: