beginnerFlow Control
For Loop with Range
Iterate over ranges with for loop.
For Loop with Range
Iterate over ranges with for loop.
Difficulty
Beginner
Code
rust
fn main() {
for i in 0..5 {
println!("{}", i);
}
println!("---");
for i in (0..5).rev() {
println!("{}", i);
}
}Explanation
This example demonstrates how to use for loop with range in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.
Key Concepts
- Rust's strong type system catches errors at compile time
- Ownership and borrowing rules ensure memory safety
- Pattern matching makes code expressive and exhaustive
Related Topics
Browse more examples in the flow-control category to build a complete understanding of this topic.