rust-itertools / itertools

Extra iterator adaptors, iterator methods, free functions, and macros.
https://docs.rs/itertools/
Apache License 2.0
2.76k stars 309 forks source link

Add implementation of MultiUnzip to Option #980

Open zekefast opened 3 months ago

zekefast commented 3 months ago

Would it be possible to implement MultiUnzip on Option to make something like that possible to implement?

struct A {
    a: u32,
    b: u32,
    c: u32,
}

let opt = Some(A { a: 1, b: 2, c: 3 });
let non: Option<A> = None;

let (a, b, c): (Option<_>, Option<_>, Option<_>) = opt
    .map(|value| (value.a, value.b, value.c))
    .multiunzip(); // a = Some(1); b = Some(2); c = Some(3)

let (a, b, c): (Option<_>, Option<_>, Option<_>) = non
    .map(|value| (value.a, value.b, value.c))
    .multiunzip(); // a = None; b = None; c = None
jswrenn commented 3 months ago

That's an elegant example! I'd have to think through this more (or just try it), but I think we could perhaps replace the IT: Iterator bound on the MultiZip impls with IT: IntoIterator.

zekefast commented 3 months ago

@jswrenn That would be great! I think there could be issues with Extend as well as it is not implemented for Option if remember it right.

jswrenn commented 3 months ago

Ah, oof that will be an issue, I think.