Skip to content

Commit de659da

Browse files
committed
Fix #1060: document impl Trait on its own page and others
1 parent 37a2861 commit de659da

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
- [Operator Overloading](trait/ops.md)
138138
- [Drop](trait/drop.md)
139139
- [Iterators](trait/iter.md)
140+
- [impl Trait](trait/impl_trait.md)
140141
- [Clone](trait/clone.md)
141142

142143
- [macro_rules!](macros.md)

src/fn/closures/output_parameters.md

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# As output parameters
22

33
Closures as input parameters are possible, so returning closures as
4-
output parameters should also be possible. However, returning closure types
5-
are problematic because Rust currently only supports returning concrete
6-
(non-generic) types. Anonymous closure types are, by definition, unknown
7-
and so returning a closure is only possible by making it concrete. This
8-
can be done via boxing.
4+
output parameters should also be possible. However, anonymous
5+
closure types are, by definition, unknown, so we have to use
6+
`impl Trait` to return them.
97

108
The valid traits for returns are slightly different than before:
119

@@ -21,16 +19,16 @@ dropped as soon as the function exited, leaving invalid references in the
2119
closure.
2220

2321
```rust,editable
24-
fn create_fn() -> Box<Fn()> {
22+
fn create_fn() -> impl Fn() {
2523
let text = "Fn".to_owned();
2624
27-
Box::new(move || println!("This is a: {}", text))
25+
move || println!("This is a: {}", text)
2826
}
2927
30-
fn create_fnmut() -> Box<FnMut()> {
28+
fn create_fnmut() -> impl FnMut() {
3129
let text = "FnMut".to_owned();
3230
33-
Box::new(move || println!("This is a: {}", text))
31+
move || println!("This is a: {}", text)
3432
}
3533
3634
fn main() {
@@ -44,10 +42,10 @@ fn main() {
4442

4543
### See also:
4644

47-
[Boxing][box], [`Fn`][fn], [`FnMut`][fnmut], and [Generics][generics].
45+
[`Fn`][fn], [`FnMut`][fnmut], [Generics][generics] and [impl Trait][impltrait].
4846

49-
[box]: ../../std/box.md
5047
[fn]: https://doc.rust-lang.org/std/ops/trait.Fn.html
5148
[fnmut]: https://doc.rust-lang.org/std/ops/trait.FnMut.html
5249
[fnbox]: https://doc.rust-lang.org/std/boxed/trait.FnBox.html
5350
[generics]: ../../generics.md
51+
[impltrait]: ../../traits/impl_trait.md

src/trait/impl_trait.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# impl Trait
2+
3+
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!
4+
5+
```rust,editable
6+
use std::iter;
7+
use std::vec::IntoIter;
8+
9+
// This function combines two Vec<i32> and returns an iterator over it.
10+
// Look how complicated its return type is!
11+
fn combine_vecs_explicit_return_type<'a>(
12+
v: Vec<i32>,
13+
u: Vec<i32>,
14+
) -> iter::Cycle<iter::Chain<IntoIter<i32>, IntoIter<i32>>> {
15+
v.into_iter().chain(u.into_iter()).cycle()
16+
}
17+
18+
// This is the exact same function, but its return type uses `impl Trait`.
19+
// Look how much simpler it is!
20+
fn combine_vecs<'a>(
21+
v: Vec<i32>,
22+
u: Vec<i32>,
23+
) -> impl Iterator<Item=i32> {
24+
v.into_iter().chain(u.into_iter()).cycle()
25+
}
26+
```
27+
28+
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:
29+
30+
```rust,editable
31+
// Returns a function that adds `y` to its input
32+
fn make_adder_function(y: i32) -> impl Fn(i32) -> i32 {
33+
let closure = move |x: i32| { x + y };
34+
closure
35+
}
36+
37+
fn main() {
38+
let plus_one = make_adder_function(1);
39+
assert_eq!(plus_one(2), 3);
40+
}
41+
```
42+
43+
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:
44+
45+
```rust,editable
46+
fn double_positives<'a>(numbers: &'a Vec<i32>) -> impl Iterator<Item = i32> + 'a {
47+
numbers
48+
.iter()
49+
.filter(|x| x > &&0)
50+
.map(|x| x * 2)
51+
}
52+
```

0 commit comments

Comments
 (0)