Skip to main content

Command Palette

Search for a command to run...

How Many Ways to Apply For loop in Rust?

Published
2 min read
How Many Ways to Apply For loop in Rust?
M

A self-motivated and enthusiastic web developer with a deep interest in JavaScript (React.js). To work in the Software industry with modern web technologies of different local & multinational Software/ IT agencies of Bangladesh and grow rapidly with increasing responsibilities.

In Rust, the for the loop is used to iterate through a collection of items, such as an array or a vector. The basic syntax for a for the loop is as follows:

for variable in collection {
    // code to execute
}

Here variable is a variable that will take on the value of each item in the collection, and collection is an expression that evaluates to the collection you want to iterate over.

For example, let's consider an array of integers and iterate through it using a for loop

let numbers = [1, 2, 3, 4, 5];

for number in numbers {
    println!("The number is: {}", number);
}

You can also use the for loop to iterate through a range of numbers, using the range function. For example:

for i in 0..10 {
    println!("The value of i is: {}", i);
}
//This will print the values from 0 to 9.

You can also use the .iter() method to iterate over an array and use .next() method to get the next item in the iteration.

let numbers = [1, 2, 3, 4, 5];

let mut iterator = numbers.iter();

loop {
    match iterator.next() {
        Some(number) => println!("The number is: {}", number),
        None => break,
    }
}

You can use for loop with different types of collections like arrays, vectors, strings, etc., and use it in different ways as per your requirement.


In Rust, you can use the step_by() method to iterate through a range of numbers with a specific step. The step_by() method takes a single argument, which is the number of steps to take between each iteration.

Here is an example of using a for loop with a range of numbers, but jumping by 2 each time:

for i in (0..10).step_by(2) {
    println!("The value of i is: {}", i);
}

This will print the values 0, 2, 4, 6, and 8.

You can also use this method in a for the loop that iterates through an array or vector.

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

for i in (0..numbers.len()).step_by(2) {
    println!("The value at index {} is: {}", i, numbers[i]);
}

This will print the values at every 2nd index of the array

More from this blog

DSA মানে শুধু LeetCode না — DSA মানে Production System slow না করা

অনেকেই বলে — “ভাই, Web Development-এ DSA লাগে না” আসলে Junior Developer থাকলে এমনটাই মনে হয়।আমি নিজেও Junior থাকতে তাই ভাবতাম। আমার আবার একটা বাজে স্বভাব আছে 😅👉 কোন কিছুর বাস্তব প্রয়োজন না বুঝলে সেটা আমার মাথায় ঢুকে না। Junior থাকতে DSA শিখেছি...

Dec 24, 20253 min read
DSA মানে শুধু LeetCode না — DSA মানে Production System slow না করা

🦀 Rust-এ মেমরি ম্যানেজমেন্ট: GC ছাড়া In-depth.

Rust-এ Garbage Collector (GC) নেই, কিন্তু মেমরি নিরাপত্তা (memory safety) আর performance Rust-এর সবচেয়ে শক্তিশালী দিক।তাই, Go-এর মতো runtime-based GC না থাকা সত্ত্বেও Rust compile-time এ মেমরি ম্যানেজ করে Ownership System, Borrow Checker, আর Lifetime...

Oct 5, 202523 min read
🦀 Rust-এ মেমরি ম্যানেজমেন্ট: GC ছাড়া In-depth.

Morshedul Munna

45 posts

As a Software Developer, I am like an architect who designs and builds digital Products. I use my knowledge and expertise to create modern applications that are both efficient and elegant.