2

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.

4
  • Hi Aitor, welcome to StackOverflow! As a tip: you just need the inner for loop, and you could just print the desired output instead of trying to return the string from within the loop. Commented Apr 16, 2021 at 9:37
  • 2
    Welcome to StackOverflow! I don't quite understand what you are trying to achieve. Would you like to return a vector of strings from the function? In this case, you should use the return type Vec<String>. Or would you like to print the strings in each iteration? In this case you should use something like println!() to print them. In any case, you likely want to remove the infinite loop around the for loop. Commented Apr 16, 2021 at 9:37
  • thank you very much for the answers and the welcome :) What I need is to return a value from a for loop. I tried to simplify the code to ask the question but it was unclear, sorry. I will follow your advice (remove the infinite loop) and see if I can re-ask the question. Commented Apr 16, 2021 at 9:57
  • @AitorZaldua if you need to return the string, why not return s? for loops don't have values, and loop only has a value through break. Commented Apr 16, 2021 at 10:36

1 Answer 1

2

If you declare a function like this

fn counter(start: u32, end: u32) -> u32

then you tell the compiler that it is supposed to return a single u32 value, i.e. a single unsigned integer number.

Your for loop iterates over a bunch of numbers, but it can only return one of them. You could change the return type of your function to return a Vec<u32>, a vector of u32 which is a data structure that can store a whole bunch of u32 values:

fn counter(start: u32, end: u32) -> Vec<u32> {
    (start..=end).collect()
}

Alternatively, you could also tell the compiler that the result is an iterator over u32 values:

fn counter(start: u32, end: u32) -> impl Iterator<Item = u32> {
    start..=end
}

The iterator lazily produces the values from start to end, and you can iterate over it in the calling code, e.g.

for i in counter(1, 10) {
    println!("{}", i);
}

prints the numbers from one to ten.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Sven, that´s exactly what I needed to know,
@AitorZaldua if this answer solved your problem, consider accepting it to reward users for helping you out
Ok, thankyou, kmdreko, I didn´t know that I had to check the answer (sorry Sven)...done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.