Skip to content

Commit d07f40a

Browse files
aturonalexcrichton
authored andcommitted
Added alternative discussion for for
1 parent 8384dce commit d07f40a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

active/0000-collections-conventions.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,25 @@ could potentially be dropped. However, there are a few downsides:
15601560

15611561
## For `IntoIterator`
15621562

1563+
### Handling of `for` loops
1564+
1565+
The fact that `for x in v` moves elements from `v`, while `for x in v.iter()`
1566+
yields references, may be a bit surprising. On the other hand, moving is the
1567+
default almost everywhere in Rust, and with the proposed approach you get to use `&` and
1568+
`&mut` to easily select other forms of iteration.
1569+
1570+
Unfortunately, it's a bit tricky to make for use by-ref iterators instead. The
1571+
problem is that an iterator is `IntoIterator`, but it is not `Iterable` (or
1572+
whatever we call the by-reference trait). Why? Because `IntoIterator` gives you
1573+
an iterator that can be used only *once*, while `Iterable` allows you to ask for
1574+
iterators repeatedly.
1575+
1576+
If `for` demanded an `Iterable`, then `for x in v.iter()` and `for x in v.iter_mut()`
1577+
would cease to work -- we'd have to find some other approach. It might be
1578+
doable, but it's not obvious how to do it.
1579+
1580+
### Input versus output type parameters
1581+
15631582
An important aspect of the `IntoIterator` design is that the element type is an
15641583
associated type, *not* an input type.
15651584

0 commit comments

Comments
 (0)