Skip to content

Commit 139d48e

Browse files
committed
Make impl Trait code examples valid Rust
1 parent 244f953 commit 139d48e

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

text/1522-conservative-impl-trait.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ With the placeholder syntax used in discussions so far,
2121
abstract return types would be used roughly like this:
2222

2323
```rust
24-
fn foo(n: u32) -> impl Iterator<Item=u32> {
24+
fn foo(n: u32) -> impl Iterator<Item = u32> {
2525
(0..n).map(|x| x * 100)
2626
}
2727
// ^ behaves as if it had return type Map<Range<u32>, Closure>
@@ -30,7 +30,6 @@ fn foo(n: u32) -> impl Iterator<Item=u32> {
3030
for x in foo(10) {
3131
// x = 0, 100, 200, ...
3232
}
33-
3433
```
3534

3635
# Background
@@ -61,10 +60,10 @@ its motivation and some other parts of its text below.
6160
6261
In today's Rust, you can write a function signature like
6362

64-
````rust
65-
fn consume_iter_static<I: Iterator<u8>>(iter: I)
66-
fn consume_iter_dynamic(iter: Box<Iterator<u8>>)
67-
````
63+
```rust
64+
fn consume_iter_static<I: Iterator<Item = u8>>(iter: I)
65+
fn consume_iter_dynamic(iter: Box<Iterator<Item = u8>>)
66+
```
6867

6968
In both cases, the function does not depend on the exact type of the argument.
7069
The type is held "abstract", and is assumed only to satisfy a trait bound.
@@ -78,15 +77,15 @@ The type is held "abstract", and is assumed only to satisfy a trait bound.
7877

7978
On the other hand, while you can write
8079

81-
````rust
82-
fn produce_iter_dynamic() -> Box<Iterator<u8>>
83-
````
80+
```rust
81+
fn produce_iter_dynamic() -> Box<Iterator<Item = u8>>
82+
```
8483

8584
you _cannot_ write something like
8685

87-
````rust
88-
fn produce_iter_static() -> Iterator<u8>
89-
````
86+
```rust
87+
fn produce_iter_static() -> Iterator<Item = u8>
88+
```
9089

9190
That is, in today's Rust, abstract return types can only be written using trait
9291
objects, which can be a significant performance penalty. This RFC proposes
@@ -105,15 +104,15 @@ Here are some problems that unboxed abstract types solve or mitigate:
105104
type, when the API should really only promise a trait bound. For example, a
106105
function returning `Rev<Splits<'a, u8>>` is revealing exactly how the iterator
107106
is constructed, when the function should only promise that it returns _some_
108-
type implementing `Iterator<u8>`. Using newtypes/structs with private fields
107+
type implementing `Iterator<Item = u8>`. Using newtypes/structs with private fields
109108
helps, but is extra work. Unboxed abstract types make it as easy to promise only
110109
a trait bound as it is to return a concrete type.
111110

112111
* _Complex types_. Use of iterators in particular can lead to huge types:
113112

114-
````rust
115-
Chain<Map<'a, (int, u8), u16, Enumerate<Filter<'a, u8, vec::MoveItems<u8>>>>, SkipWhile<'a, u16, Map<'a, &u16, u16, slice::Items<u16>>>>
116-
````
113+
```rust
114+
Chain<Map<'a, (i32, u8), u16, Enumerate<Filter<'a, u8, vec::MoveItems<u8>>>>, SkipWhile<'a, u16, Map<'a, &u16, u16, slice::Items<u16>>>>
115+
```
117116

118117
Even when using newtypes to hide the details, the type still has to be written
119118
out, which can be very painful. Unboxed abstract types only require writing the

0 commit comments

Comments
 (0)