You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/trait/impl_trait.md
+13-6Lines changed: 13 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,13 @@
1
-
# impl Trait
1
+
# `impl Trait`
2
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!
3
+
If your function returns a type that implements `MyTrait`, you can write its
4
+
return type as `-> impl MyTrait`. This can help simplify your type signatures quite a lot!
4
5
5
6
```rust,editable
6
7
use std::iter;
7
8
use std::vec::IntoIter;
8
9
9
-
// This function combines two Vec<i32> and returns an iterator over it.
10
+
// This function combines two `Vec<i32>` and returns an iterator over it.
10
11
// Look how complicated its return type is!
11
12
fn combine_vecs_explicit_return_type<'a>(
12
13
v: Vec<i32>,
@@ -25,7 +26,10 @@ fn combine_vecs<'a>(
25
26
}
26
27
```
27
28
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
+
More importantly, some Rust types can't be written out. For example, every
30
+
closure has its own unnamed concrete type. Before `impl Trait` syntax, you had
31
+
to allocate on the heap in order to return a closure. But now you can do it all
32
+
statically, like this:
29
33
30
34
```rust,editable
31
35
// Returns a function that adds `y` to its input
@@ -40,7 +44,10 @@ fn main() {
40
44
}
41
45
```
42
46
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:
47
+
You can also use `impl Trait` to return an iterator that uses `map` or `filter`
48
+
closures! This makes using `map` and `filter` easier. Because closure types don't
49
+
have names, you can't write out an explicit return type if your function returns
50
+
iterators with closures. But with `impl Trait` you can do this easily:
0 commit comments