keyword
Rust for: iterate over values
Use `for` to visit successive values of an iterable such as a range or collection. It avoids maintaining a manual counter for many tasks. Expected output: 20 Why: The body adds each array element to the total. Common mistake: Iterating by value can move non-Copy elements; choose borrowed iteration when ownership must be retained.
Formal Syntax & Structure
rust syntaxrust
for pattern in iterable { statements }Working Example
Example Snippetrust
fn main() { let mut total = 0; for price in [4, 6, 10] { total += price; } println!("{total}");}