]> git.lizzy.rs Git - rust.git/blob - src/doc/style/style/naming/iterators.md
Merge branch 'syntax' of https://github.com/aochagavia/rust into mulit-decor
[rust.git] / src / doc / style / style / naming / iterators.md
1 % Iterators
2
3 #### Method names [RFC #199]
4
5 > The guidelines below were approved by [RFC #199](https://github.com/rust-lang/rfcs/pull/199).
6
7 For a container with elements of type `U`, iterator methods should be named:
8
9 ```rust
10 fn iter(&self) -> T           // where T implements Iterator<&U>
11 fn iter_mut(&mut self) -> T   // where T implements Iterator<&mut U>
12 fn into_iter(self) -> T       // where T implements Iterator<U>
13 ```
14
15 The default iterator variant yields shared references `&U`.
16
17 #### Type names [RFC #344]
18
19 > The guidelines below were approved by [RFC #344](https://github.com/rust-lang/rfcs/pull/344).
20
21 The name of an iterator type should be the same as the method that
22 produces the iterator.
23
24 For example:
25
26 * `iter` should yield an `Iter`
27 * `iter_mut` should yield an `IterMut`
28 * `into_iter` should yield an `IntoIter`
29 * `keys` should yield `Keys`
30
31 These type names make the most sense when prefixed with their owning module,
32 e.g. `vec::IntoIter`.