Start learning free

LESSON 121 OF 414 · FOR LOOPS - LOOPING OVER ARRAYS

Buggy Code: Ordering Statements Around a For Loop in Rust

Practice Read the full explanation

Inside the code editor we've tried to write a program that should print:

What a nice day!
Hey, Andy
How are you?
Hey, Mike
How are you?
Hey, Mary
How are you?
Let's start the class!

but it seems like we made some mistakes because when we run our code, it produces a different output.


Assignment:

Your task is to fix our code such that it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    RUST · FOR LOOPS - LOOPING OVER ARRAYS

    Buggy Code For Loops Execution Flow 3 in Rust, explained

    Problem overview

    The program should print one opening line, then a greeting and a question for each student, then one closing line:

    What a nice day!
    Hey, Andy
    How are you?
    Hey, Mike
    How are you?
    Hey, Mary
    How are you?
    Let's start the class!
    

    This is the starting code, and it doesn't compile:

    let students = vec!["Andy", "Mike", "Mary"];
    
    println!("Let's start the class!");
    for student in &students {
        println!("What a nice day!");
    }
    println!("How are you?");
    println!("Hey, {}", student);

    This lesson has several mistakes at once. The way to handle that is to compare the code to the expected output line by line and sort each statement into one of three places: before the loop, inside it, or after it.

    Sorting the statements

    Look at the expected output and ask how often each message appears:

    • What a nice day! appears once, first. It goes before the loop.
    • Hey, with a name appears once per student. It goes inside the loop, because it uses the loop variable.
    • How are you? appears once per student, right after each greeting. It goes inside the loop too, after the greeting.
    • Let's start the class! appears once, last. It goes after the loop.

    Now compare with the starter code. The closing line is before the loop, so it would print first. The opening line is inside the loop, so it would print three times. The question is after the loop, so it would print once. The greeting is after the loop as well, where the loop variable is gone. Every statement is in the wrong place.

    The compile error

    The compiler complains about student, and the message is:

    error[E0425]: cannot find value `student` in this scope

    The loop variable exists only between the loop's braces. After the closing brace the name student is no longer defined, so the last line of the starter code fails. Moving that line into the body fixes the error and also fixes its position in the output. Once the compile error is gone, the remaining mistakes show up in the output, as wrong order and wrong counts.

    The fix, step by step

    1. Put println!("What a nice day!"); before the for.
    2. Inside the braces, print the greeting with println!("Hey, {}", student);.
    3. Directly after it, still inside the braces, print How are you?.
    4. After the closing brace, print Let's start the class!.

    The reference solution is:

    let students = vec!["Andy", "Mike", "Mary"];
    println!("What a nice day!");
    for student in &students {
        println!("Hey, {}", student);
        println!("How are you?");
    }
    println!("Let's start the class!");

    Tracing it: the opening line prints once. Then the body runs for Andy, Mike and Mary, printing two lines each, six in total. After the loop, the closing line prints once. That is eight lines, in the order the task wants.

    Details and common mistakes

    • The order of statements inside the body matters. Swapping the greeting and the question inside the braces would print How are you? before each name.
    • The name student can't be used after the loop.
    • Fixing only the compile error by moving the greeting into the loop is not enough. The program then compiles but prints the lines in the wrong order, so check the output against the expected text.
    • Indentation does not group statements in Rust. The braces do.

    Testing

    Run the fixed program and compare it to the expected eight lines. Then change the vector to a single name, vec!["Andy"]. The output should be four lines: the opening, Hey, Andy, How are you?, and the closing. The opening and closing lines stay at one each however many students there are, while the greeting and the question grow with the vector.

    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free