From 66509f2f3e7714ad5a96b150117d2d5d1427b7b5 Mon Sep 17 00:00:00 2001 From: Igor Petruk Date: Mon, 23 Jan 2023 12:04:42 +0000 Subject: [PATCH] Update for-expressions.md (#238) If we are introducing `for` loops, I think it is beneficial to show a very basic `for i=0; i < 10; i+=2` too. --- src/control-flow/for-expressions.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/control-flow/for-expressions.md b/src/control-flow/for-expressions.md index eb60704d..6b1cd17d 100644 --- a/src/control-flow/for-expressions.md +++ b/src/control-flow/for-expressions.md @@ -10,7 +10,19 @@ fn main() { for x in v { println!("x: {x}"); } + + for i in (0..10).step_by(2) { + println!("i: {i}"); + } } ``` You can use `break` and `continue` here as usual. + +
+ +* Index iteration is not a special syntax in Rust for just that case. +* `(0..10)` is a range that implements an `Iterator` trait. +* `step_by` is a method that returns another `Iterator` that skips every other element. + +