Start learning free

LESSON 116 OF 414 · FOR LOOPS - LOOPING OVER ARRAYS

Buggy Code: Fixing a For Loop Over a Vec in Rust

Practice Read the full explanation

Inside the code editor we've tried to write a for loop that greets every student in the students vector.

So when we ran the code, we expected it to print:

Welcome, Andy
Welcome, Sally
Welcome, John

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


Assignment:

Your task is to fix our loop such that no errors will be produced and it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    RUST · FOR LOOPS - LOOPING OVER ARRAYS

    Buggy Code For Loops 1 in Rust, explained

    Problem overview

    The program should greet every student in the students vector. The expected output is:

    Welcome, Andy
    Welcome, Sally
    Welcome, John
    

    The code in the editor doesn't compile, so nothing is printed at all. Your job is to find what is wrong and fix it so the program produces those three lines. Here is the starting code:

    let students = vec!["Andy", "Sally", "John"];
    for name in &students {
    }
    println!("Welcome, {}", student);

    What goes wrong

    There are two separate mistakes, and the compiler reports only one of them at first.

    Mistake 1: the variable does not exist

    The loop calls its variable name, but the println! uses student. No variable called student exists anywhere in this code, so the compiler stops with this error:

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

    It also suggests a similarly named variable, students. Taking that suggestion would be wrong: students is the whole vector, not one student. The right fix is to make the name used in the body match the name chosen by the loop.

    Mistake 2: the print is outside the loop

    The body of a loop is whatever sits between its braces. In the starter code the braces are empty, and the println! comes after the closing brace. So even if the variable existed, the print would run once, after the loop ended, instead of once per student.

    The two mistakes are linked. The loop variable only exists inside the body. A line after the closing brace can't use it, which is why moving the print into the body is part of the fix, not an extra step.

    The fix, step by step

    1. Move the println! line between the braces, so it runs on every pass.
    2. Make the names agree. You can rename the loop variable to student, or change the body to print name.

    Moving the print inside while keeping the loop variable named name still fails with the same cannot find value `student` error, because the body still says student. Both changes are needed.

    The reference solution renames the loop variable:

    let students = vec!["Andy", "Sally", "John"];
    for student in &students {
        println!("Welcome, {}", student);
    }

    Printing name in the body, with the loop variable left as name, is equally correct. A name that reads as "one student" inside a loop over students is the clearer choice, which is why the reference picks it.

    Language details worth knowing

    • &students borrows the vector, so the loop doesn't consume it. Each loop variable is then a reference to an element, and println! prints it like the text itself.
    • In println!("Welcome, {}", student), the {} is replaced by the argument after the string. The text is printed with a new line at the end.
    • An unused loop variable is not an error. The starter code's name is never used, which the compiler may flag as a warning, but the build only fails because of student.

    Common mistakes

    • Fixing only one of the two problems and getting the same error again. After moving the print, check the name.
    • Replacing student with students to silence the compiler. The vector can't be printed with {}, so this trades one error for another.
    • Putting the print on the line right after the loop's closing brace, where it either fails to compile or runs only once.

    Testing

    After the fix, the program prints Welcome, Andy, Welcome, Sally and Welcome, John on separate lines. A quick check is to add a fourth name to the vector and confirm it appears with no other change. That is the point of the loop: the code that greets one student is the code that greets all of them.

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