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
- Move the
println!line between the braces, so it runs on every pass. - Make the names agree. You can rename the loop variable to
student, or change the body to printname.
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
&studentsborrows the vector, so the loop doesn't consume it. Each loop variable is then a reference to an element, andprintln!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
nameis never used, which the compiler may flag as a warning, but the build only fails because ofstudent.
Common mistakes
- Fixing only one of the two problems and getting the same error again. After moving the print, check the name.
- Replacing
studentwithstudentsto 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.
