Shouldn't for loops return values the same way blocks do?
So we can do
{ 50 + 10} + 7
but why can't we do the same thing with for loops?
for i in 0..10 {
if i == LUCKY_NUMBER {
break i; // assuming this is how it would return
}
} + 60
The intent is not to overcome any problem i'm facing or anything, just a discussion. But this is what sparked the question:
I was trying to find the index of some element, so i used the for loop to analyse the elements 'til some condition was met, which would mutate a variable outside the for loop.
// My solution. Not the best example and i didn't do this exactly of course.
let mut magic_index: usize = 0;
for (index, element) in array.iter().enumerate() {
if SOME_CONDITION {
magic_index = index;
}
}
Here's how this could look like, although it's completely incorrect:
let mut magic_index: usize = for (index, element) in array.iter().enumerate() {
if SOME_CONDITION {
break index;
}
};