Skip to content

Commit 193dd6d

Browse files
Update for.md
I recently discovered the issue for myself, that you can write the following code: for i in 10..1{ println!("Hello World"); } This code does nothing, but you dont get a compiler warning and nothing and this isnt described in the rust handbook either. I dont think I can be the only beginner stumbling over this, so theres a possible solution. I dont know if the way I wrote it fits in with the writing style of the book, but just adjust it as needed :)
1 parent b31e3b8 commit 193dd6d

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/flow_control/for.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ fn main() {
4646
}
4747
```
4848

49+
Just remember that even though you can compile the code when a>b, the loop gets
50+
never executed.
51+
```rust,editable
52+
for i in 10..1{
53+
println!("fizzbuzz");
54+
}
55+
```
56+
If you want to count down, you need to use .rev() instead
57+
```rust,editable
58+
for i in (1..10).rev(){
59+
println!("fizzbuzz");
60+
}
61+
```
4962
## for and iterators
5063

5164
The `for in` construct is able to interact with an `Iterator` in several ways.

0 commit comments

Comments
 (0)