I tried running the below code on rustc 1.72.0 with cargo run :
fn main() {
let ea = (1..).filter(|x| *x < 10).take(10);
// Initialize a counter
let mut count = 0;
println!("Counter has been initialized.");
// Iterate through the elements and count them
for _ in ea {
count += 1;
println!("Current index: {}", count);
}
}
If I'm not mistaken, the expected behavior should be that the program exits normally after printing 1-9, since .take() is supposed to stop early and return all elements if the number of iterator elements is less than .take()'s specified parameter (https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.take).
However, what happened was that my program got frozen with items 1-9 printed out, and I had to press Ctrl+C to exit.
I believe it is also worth noting that cargo check found no issues with the code:

Additionally, creating the iterator with let ea = (1..10); instead caused the same program to exit normally after printing values 1-9.
Is this expected behavior from Rust? If it is, may I know what is causing it, and whether it will be changed in the future?


It does not freeze, it just overflows. Since the iterator only has 9 elements
< 10, it counts all the way toi32::MAXthen overflows back to 0. In debug builds, this will panic. In release builds, it will wrap around and give you 0 ad the tenth element.