Skip to content

iter::fuse not behaving as expected #51119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JEphron opened this issue May 28, 2018 · 4 comments
Closed

iter::fuse not behaving as expected #51119

JEphron opened this issue May 28, 2018 · 4 comments

Comments

@JEphron
Copy link

JEphron commented May 28, 2018

The docs led me to believe that fuse would yield items until it saw a None, and then yield only Nones. Maybe I'm misunderstanding, but

let v = vec!["0", "a", "1", "2"];
let it: Vec<Option<i32>> = v.iter().map(|x| x.parse().ok()).fuse().inspect(|x| println!("inspected {:?}",x)).collect();
println!("it:{:?}", it);

outputs

inspected Some(0)
inspected None
inspected Some(1)
inspected Some(2)
it:[Some(0), None, Some(1), Some(2)]  

whereas I'd expect to see something like it:[Some(0), None, None, None]

rustc 1.28.0-nightly (cb20f68 2018-05-21)

@ordovicia
Copy link
Contributor

ordovicia commented May 28, 2018

The map() yields Some(Some(0)), Some(None), Some(Some(1)), Some(Some(2)), followed by None.
The iterator is not fused by Some(None).

fn main() {
    let mut iter = ["0", "a", "1", "2"].iter().map(|x| x.parse::<i32>().ok());

    for _ in 0..5 {
        println!("{:?}", iter.next());
    }
    // => Some(Some(0)), Some(None), Some(Some(1)), Some(Some(2)), None
}

The fuse() method is used for iterator that may return Some after it returns None, claiming that it has nothing to yield.
E.g. Incoming network buffers.

@hellow554
Copy link
Contributor

Somewhat related to #50225 ?

@Manishearth
Copy link
Member

Yeah, this is behaving as expected. fuse is about iterators that may "start" again after finishing, not about iterators over an option.

@pacman82
Copy link
Contributor

This is how fused is supposed to behave. it.next() in this example is returning Option<Option<i32>>. With the outer Optionindicating if the iterator has reached its end, and the inner Option indicating if parsing has been successful. The inner Optionis part of the element type iterated over and it being None should not cause fuse to do anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants