Menu
Coddy logo textTech

Enhanced For Loop

Part of the Fundamentals section of Coddy's Rust journey — lesson 66 of 75.

There are different ways to iterate over an array, here are some of them:
Consider the following array:

let numbers = [1, 2, 3, 4, 5];
  • Direct Iterator:

    for num in numbers.iter() {}
    // Values: 1, 2, 3, 4, 5
  • Index & Value:

    for (index, value) in numbers.iter().enumerate() { }
    // Values: (0,1), (1,2), (2,3), (3,4), (4,5)
  • Slices of specific size:

    for chunk in numbers.chunks(2) { }
    // Values: [1,2], [3,4], [5]

    Each chunk is a slice, so you can use slice methods on it — for example, chunk.len() returns how many elements are in that chunk. The last chunk may be smaller if the array doesn't divide evenly.

You can also chain .enumerate() onto .chunks() to get both an index and the chunk at the same time:

for (i, chunk) in numbers.chunks(2).enumerate() { }
// Values: (0,[1,2]), (1,[3,4]), (2,[5])

Here, .chunks(2) produces the slices first, then .enumerate() wraps each one with a counter — so i is the chunk index and chunk is the slice.

  • Iterate with mutation:

    for num in numbers.iter_mut() { 
    	*num *= 2;
    }
    // Array becomes: [2, 4, 6, 8, 10]
challenge icon

Challenge

Easy

Create a program that processes a shopping list with items and their prices, and provides different views of the data.

The program should:

  1. Print each item number and price (using enumerate). Format as Item N: $Price (e.g., Item 1: $2.75).
  2. Print the prices in pairs (using chunks) as "bundle deals". If a bundle has 2 items, print the sum: Bundle N: $Price1 + $Price2 = $Sum. If it has only 1 item, print: Bundle N: $Price.
  3. Apply a 10% discount to all prices (using iter_mut).
  4. Print the final discounted prices, each on a new line, formatted to 2 decimal places (e.g., $2.48).

Hint: You can chain iterators together — for example, prices.chunks(2).enumerate() gives you both the chunk index and the chunk slice at once. Each chunk is a slice, so you can call chunk.len() on it and index into it with chunk[0], chunk[1], etc.

Expected output format for prices:
Use ${:.2} to format prices to 2 decimal places.

Try it yourself

fn main() {
    let mut prices = [2.75, 1.50, 5.00, 3.5, 4.1, 2.25, 7.9];

    println!("Original Prices:");
    // TODO: Use enumerate() to print each item number and price
    // Hint: prices.iter().enumerate() gives (index, &price) pairs
    // Format prices to 2 decimal places using {:.2} — e.g., "Item 1: $2.75"


    println!("\nBundle Deals:");
    // TODO: Chain chunks(2) and enumerate() together: prices.chunks(2).enumerate()
    // This gives (index, chunk) pairs — each chunk is a slice, so chunk.len() tells you its size
    // If chunk.len() == 2, print: "Bundle 1: $2.75 + $1.50 = $4.25"
    // If chunk.len() == 1, print: "Bundle 4: $7.90"
    // Use {:.2} to format all prices to 2 decimal places


    // TODO: Use iter_mut() to apply a 10% discount to all prices
    // Hint: multiply each *price by 0.9


    println!("\nPrices after 10% discount:");
    // TODO: Print each discounted price formatted to 2 decimal places
    // Expected: "$2.48" etc.

}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals