Functions and Control Flow
Learn how to define functions, use control flow statements (if, else, while, for), and handle different code execution paths.
Rust: For Loops
Control Flow: For Loops
In Rust, for
loops are a fundamental control flow construct used to iterate over a sequence of values or execute a block of code repeatedly. They provide a structured way to repeat operations for each element within a collection (like an array, vector, or slice) or a numerical range.
Iterating over Collections
Rust's for
loop is particularly well-suited for working with collections. It automatically handles iterating through each item, making your code cleaner and more readable.
Example: Iterating over a Vector
fn main() {
let my_vector = vec![10, 20, 30, 40, 50];
for element in &my_vector { // Important: Use & to borrow, not take ownership
println!("The value is: {}", element);
}
}
Explanation:
let my_vector = vec![10, 20, 30, 40, 50];
creates a vector (a dynamically sized array) containing the numbers 10 through 50.for element in &my_vector
starts thefor
loop. The&
is crucial here. It borrows the vector, meaning the loop doesn't take ownership of it. If you didn't include&
, the loop would *move* the vector, and you couldn't usemy_vector
after the loop.println!("The value is: {}", element);
prints the value of the currentelement
in each iteration.
Example: Iterating over a Slice
fn main() {
let array = [1, 2, 3, 4, 5];
let slice = &array[1..4]; // Creates a slice from index 1 (inclusive) to 4 (exclusive)
for number in slice {
println!("Slice element: {}", number);
}
}
Explanation:
- The code first defines an array
array
of integers. - Then, it creates a slice named
slice
that points to a portion of the original array, specifically elements at indices 1, 2, and 3. The range1..4
means "from index 1 up to (but not including) index 4." - The
for
loop iterates over each element of the slice and prints its value.
Iterating over Ranges
Rust also provides a convenient way to iterate over numerical ranges using the ..
and ..=
operators.
Example: Using ..
(exclusive range)
fn main() {
for i in 1..5 { // 1 to 4 (5 is exclusive)
println!("The number is: {}", i);
}
}
Explanation:
for i in 1..5
creates a range from 1 up to (but not including) 5. So, the loop will iterate withi
taking the values 1, 2, 3, and 4.println!("The number is: {}", i);
prints the current value ofi
in each iteration.
Example: Using ..=
(inclusive range)
fn main() {
for i in 1..=5 { // 1 to 5 (5 is inclusive)
println!("The number is: {}", i);
}
}
Explanation:
for i in 1..=5
creates a range from 1 up to and including 5. So, the loop will iterate withi
taking the values 1, 2, 3, 4, and 5.println!("The number is: {}", i);
prints the current value ofi
in each iteration.
Summary
for
loops in Rust offer a clean and efficient way to iterate over collections and ranges. Remember to borrow collections using &
to avoid ownership issues. The ..
and ..=
range operators provide flexibility in controlling the iteration boundaries.