Exerpad
🔥0
0 XP
Lv.1 Beginner
Log In

Fix the Iter

This code tries to print each item and then print the total count, but it has a borrow error! The vector gets moved into the loop and can't be used afterward.

Fix the code so it prints:

text
apple
banana
cherry
Count: 3

Hint: When looping through a vector, use & before the vector name to borrow it instead of moving it. That way you can still use it after the loop!

rust
fn main() {
let fruits = vec!["apple", "banana", "cherry"];
for fruit in fruits {
println!("{}", fruit);
}
println!("Count: {}", fruits.len());
}