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
## Description
This implements an Iterator trait in std-lib, and adds iter() to Vec.
This also adds parsing and desugaring of for loops.
```
for pattern in iterator {
code_block
}
```
is desugared into:
```
let mut iterable = iterator;
while true {
let value_opt = iterable.next();
if value_opt.is_none() {
break;
}
let value = value_opt.unwrap();
code_block
}
```
This also adds for loops documentation to the control flow docs.
We still have to fix this issues:
- #5567
- #5568
- #5570
- #5571Closes#145
## Checklist
- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
Copy file name to clipboardexpand all lines: docs/book/src/basics/control_flow.md
+14-2
Original file line number
Diff line number
Diff line change
@@ -62,7 +62,7 @@ Some examples of how you can use a `match` expression:
62
62
63
63
### `while`
64
64
65
-
Loops in Sway are currently limited to `while`loops. This is what they look like:
65
+
This is what a `while`loop looks like:
66
66
67
67
```sway
68
68
while counter < 10 {
@@ -72,9 +72,21 @@ while counter < 10 {
72
72
73
73
You need the `while` keyword, some condition (`value < 10` in this case) which will be evaluated each iteration, and a block of code inside the curly braces (`{...}`) to execute each iteration.
74
74
75
+
### `for`
76
+
77
+
This is what a `for` loop that computes the sum of a vector of numbers looks like:
78
+
79
+
```sway
80
+
for element in vector.iter() {
81
+
sum += element;
82
+
}
83
+
```
84
+
85
+
You need the `for` keyword, some pattern that contains variable names such as `element` in this case, the `ìn` keyword followed by an iterator, and a block of code inside the curly braces (`{...}`) to execute each iteration. `vector.iter()` in the example above returns an iterator for the `vector`. In each iteration, the value of `element` is updated with the next value in the iterator until the end of the vector is reached and the `for` loop iteration ends.
86
+
75
87
### `break` and `continue`
76
88
77
-
`break` and `continue` keywords are available to use inside the body of a `while` loop. The purpose of the `break` statement is to break out of a loop early:
89
+
`break` and `continue` keywords are available to use inside the body of a `while`or `for`loop. The purpose of the `break` statement is to break out of a loop early:
0 commit comments