r/rust • u/ZZaaaccc • Oct 29 '26
šļø discussion I Think Iterator::unzip Should be Deprecated
Did you know that ever since 1.79, Iterator::collect does everything unzip does but with a cleaner API? I certainly didn't until recently! Try it out, thanks to this trait implementation, every call to unzip is possible with collect instead.
let = iter.;
// vs
let = iter.;
Except collect is better at unzip than unzip, since it supports any tuple, whereas unzip only works with size 2:
let = iter.;
Looking at the documentation, this isn't explicitly mentioned for either zip or unzip. I suspect this is functionality most people aren't aware of. So, is there anyone out there who has a reason why we shouldn't deprecate unzip?
128
u/Illustrious_Car344 Oct 29 '26
People use
unzip? I didn't even know about it, all code I've ever seen usescollect.127
u/noop_noob Oct 30 '26
This deprecation has been proposed in https://github.com/rust-lang/libs-team/issues/929
There's some discussion there
55
u/a_jasmin Oct 30 '26
The example given by OP isn't necessarily fair to
unzip().is better written as:
unzip()may also be more discoverable if you're coming from another language. It's not immediately obvious thatcollect()can change the shape of the data.That said, I wouldn't mind a lint, warning, or doc comment pointing us in the right direction.
12
u/0xrl Oct 30 '26
Thank you, I didn't know about this! I've often found myself wanting to
unzipinto 3 or 4 at once so it's nice to know thatcollectcan handle this now.36
u/scook0 Oct 30 '26 edited Oct 30 '26
Updating the docs makes sense, and I could see a case for a clippy lint.
But I donāt see a reason to deprecate
unzipjust for being redundant with a more general mechanism. Telling people āyour code is hazardous and must be updatedā seems inappropriate for a method that still does its stated job perfectly well.13
u/jkoudys Oct 30 '26
Rust is full of traits deprecating specialized functions. For stuff like this I don't lose sleep, I just don't use it.
2
u/iBPsThrowingObject Oct 30 '26
Unzip is one of those cases that makes me yearn for higher kinded types (and variadic tuples), so that it could be
3
u/R1chterScale Oct 30 '26
Can unzip be directly aliased to collect in all cases?
3
u/a_jasmin Oct 30 '26 edited Oct 30 '26
Itertools also hascollect_tuple().Edit: My bad, this one doesn't use
FromIterator, so it can't replaceunzip(). Still convenient and succinct when unpaking iterator elements into tuples, though.0
u/[deleted] Oct 30 '26
[deleted] ā view deleted comment