let mut count = 0;
for i in 2..100 {
for n in 2..(i + 1) {
if n == i {
println!("{}",n);
count += 1;
}
if i % n == 0 && n < i {
break;
}
}
}
println!("{}", count);
This is a logic for calculating prime numbers and I don't know how to break, I see break handled in the try_fold() method but I don't seem to see it in the other methods, is it ok to use try_fold() here? Or is there a better way? Suppose I need to use break and continue several times, u even break 'label and continue 'label, is it not appropriate to use Iterator, instead I should use for loops?
What is wrong with using loops here in the first place? If you need to have the functionality of break and continue (which only work inside of loop bodies, not inside of callbacks you pass to the various iterator methods, as you've already stated in your question), why make this program harder to comprehend by using iterator methods instead of loop expressions?
While I do agree with @jofas about the for loops looking cleaner, it is still possible to do what you asked with iterators.
let mut count = 0;
// Iterate over i
(2..100)
.flat_map(|i| {
// Flatten with iterator over n
// See link for explanation
(2..i + 1).map(move |n| (i, n)).map_while(|(i, n)| {
// "break" once condition is met. We don't check for i == n yet
if i % n == 0 && n < i {
None
} else {
Some((i, n))
}
})
})
// Remove all items where n != i, yield n
.filter_map(|(i, n)| if n == i { Some(n) } else { None })
// Print each prime number and increment count
.for_each(|x| {
println!("{}", x);
count += 1;
});
println!("{}", count);
Unlike your method, this one checks if n == iafter it checks for i % n == 0 && n < i. It also uses flat_map to combine multiple for loops into one iterator. (Please see this post.)