Repository navigation
Tracking Issue for array_zip #80094
Copy link
Copy link
Closed
Labels
A-arrayArea: `[T; N]`Area: `[T; N]`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs issues that are tracked on the team's radar.T-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.This PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Description
Feature gate: #![feature(array_zip)]
This is a tracking issue for the zip method on arrays.
Just like Iterator::zip combines two iterators into one, [T; N]::zip() returns a new array where every element is a tuple where the first element comes from the first array, and the second element comes from the second array. In other words, it zips two arrays together, into a single one.
let x = [1, 2, 3];
let y = [4, 5, 6];
let z = x.zip(y);
assert_eq!(z, [(1, 4), (2, 5), (3, 6)]);Public API
pub fn zip<U>(self, rhs: [U; N]) -> [(T, U); N]Steps / History
- Implementation: Added [T; N]::zip() #79451
- Stabilization PR
Unresolved Questions
[...] This seems like a pretty niche feature to me personally, but I can understand that there are situations when you might want it. I'm not sure if the libs team has a standard for "fill in the missing methods" PRs like this.
overlap with potential std::iter::Zip trait [...]
Reacted by Jakub Koralewski, veprolet, Brandon H. Gomes, Snowapril, Alphaesia, Acshi Haggenmiller, Sadiinso, Felipe S. S. Schneider, Adrián Delgado, Zack and 16 more
Metadata
Metadata
Assignees
Labels
A-arrayArea: `[T; N]`Area: `[T; N]`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs issues that are tracked on the team's radar.T-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.This PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Activity
leonardo-m commentedon Dec 16, 2020
Could you show two use cases?
usbalbin commentedon Dec 16, 2020
What made me open the PR was for doing element wise operations on vector-like objects without the need for any heap allocations or unnecessary zeroing.
As for a second use case, well I will have to think about that one...
Maybe bit far-fetched but here goes:
(which I suppose is pretty much the same use case but with different names)
cauebs commentedon Dec 21, 2020
Wouldn't this become obsolete after #65798?
CryZe commentedon Dec 21, 2020
No because iterators don't encode the length in the type, so if you intend to go back to an array you'd have to unwrap.
leonardo-m commentedon Dec 21, 2020
Can't we give arrays a different iterator that encodes the length too? :-)
usbalbin commentedon Dec 22, 2020
Wouldn't that be problematic in combination with methods like
filterorskipetc?leonardo-m commentedon Dec 22, 2020
Yeah. We can't have a "filter" there. So only some iterators are possible. But "skip" is doable as long as the number of items to skip is given as a const generics value (and in my code the argument of "skip" is often a value known at compile-time).
usbalbin commentedon Jan 2, 2021
Would that be/work with #80470 or something completely different like new trait? Just asking since it seems like
arrayIntoItermight be getting stabilizedusbalbin commentedon Mar 1, 2021
@leonardo-m
I have started doing some experimenting here. Atleast to me this feels like a much more reusable and flexible solution than adding all of those of methods directly to
[T; N].MDoerner commentedon May 6, 2021
As somebody who has stumbled upon this unstable method while implementing some const generic vector operations, I would like to point out something regarding the presented use case. To actually efficiently lift traits like
Addas component-wise operations, one would rather need azip_withoperation combiningzipandmap, maybe with some other name more consistent with rust conventions. Using the unstablearray_mapfrom #75243 onarray_zipwould potentially write the array twice, at least given the current implementation.usbalbin commentedon May 6, 2021
@MDoerner any thoughts on something like iter_fixed?
Edit:
There is an example here for vector addition
linclelinkpart5 commentedon Jun 20, 2021
This method has ended up being incredibly useful in my fixed-array audio DSP library, mainly in frame addition/multiplication. It allows me to do an element-wise binary operation without needing to use iterators (non-const? pah!) or having to create an incrementally-updated output array. The latter is especially nice in cases where the output type is expensive to instantiate: having to create
Ninstances for a temporary output buffer that are immediately going to be overwritten is wasteful.Something I'd love to suggest would be a corresponding
unzipmethod, the inverse operation ofzip. I imagine its signature looking like something along the lines of:I'd be happy to work on an impl of
unzipif others think it would be a useful method. I'd personally find quite a few good spots to use this in that DSP library.28 remaining items
rfcbot commentedon Mar 28, 2023
🔔 This is now entering its final comment period, as per the review above. 🔔
rfcbot commentedon Apr 7, 2023
The final comment period, with a disposition to close, as per the review above, is now complete.
As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.
Rollup merge of rust-lang#112096 - workingjubilee:array-unzip, r=scot…
Rollup merge of #112096 - workingjubilee:array-unzip, r=scottmcm