Skip to content
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

Add core::array::from_fn #83576

Closed
wants to merge 1 commit into from
Closed

Add core::array::from_fn #83576

wants to merge 1 commit into from

Conversation

CryZe
Copy link
Contributor

@CryZe CryZe commented Mar 27, 2021

This adds core::array::from_fn for initializing an array from a closure. The API is meant to closely resemble core::iter::from_fn with the difference that the array dictates the amount of elements produced as opposed to the function.

This should also cover a lot of cases where one might want to reach for collecting into an array, but without all the associated problems that this would have, such as providing too few or too many elements.

Example Usage

let array = std::array::from_fn(|index| 2 * index);
assert_eq!(array, [0, 2, 4, 6]);

@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 27, 2021
@the8472
Copy link
Member

the8472 commented Mar 27, 2021

Would it make sense to pass the index? Iterators can be infinite so it doesn't make sense there, but that doesn't apply to arrays.

@rust-log-analyzer

This comment has been minimized.

This adds `core::array::from_fn` for initializing an array from a
closure. The API is meant to closely resemble `core::iter::from_fn` with
the difference that the array dictates the amount of elements produced
as opposed to the function.

This should also cover a lot of cases where one might want to reach for
collecting into an array, but without all the associated problems that
this would have, such as providing too few or too many elements.

```rust
let array = std::array::from_fn(|index| 2 * index);
assert_eq!(array, [0, 2, 4, 6]);
```
@voidc
Copy link
Contributor

voidc commented Mar 27, 2021

I think this is very similar to #75644.

@Mark-Simulacrum
Copy link
Member

r? @joshtriplett but I imagine we may want to (temporarily at least) close in favor of #75644 as noted by @voidc

@joshtriplett
Copy link
Member

joshtriplett commented Apr 5, 2021

I'd suggest that we discuss on #75644 which PR should get merged.

I do favor the naming from this PR, but I don't have an opinion on which PR should be merged other than naming.

@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 23, 2021
@bors
Copy link
Contributor

bors commented Apr 24, 2021

☔ The latest upstream changes (presumably #84525) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon
Copy link
Member

Ping from triage - @CryZe
can you please resolve the merge conflicts?

@rustbot label: -S-waiting-on-review +S-waiting-on-author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 9, 2021
@CryZe
Copy link
Contributor Author

CryZe commented May 9, 2021

I'll close this in favor of #75644 which also implements the try_from_fn version of this.

@CryZe CryZe closed this May 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.