I will rewrite my previous, and unclear, question. My problem is when I try to return a value from a for loop.
That's works fine:
fn main() {
counter(5, 8);
}
fn counter(start: u32, end: u32) {
for n in start..=end {
println!("{}", n);
}
}
And that doesn't work:
fn main() {
let result = counter(5, 8);
println!("{}", result);
}
fn counter(start: u32, end: u32) -> u32 {
for n in start..=end {
n
}
}
And I try a lot of things and I can't make it work. I hope someone can help me to understand why.
forloop, and you could just print the desired output instead of trying to return the string from within the loop.Vec<String>. Or would you like to print the strings in each iteration? In this case you should use something likeprintln!()to print them. In any case, you likely want to remove the infinitelooparound the for loop.return s?forloops don't have values, andlooponly has a value throughbreak.