-
Notifications
You must be signed in to change notification settings - Fork 13.3k
array::from_fn
behavior unclear from documentation
#139061
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
Comments
matthiaskrgr
added a commit
to matthiaskrgr/rust
that referenced
this issue
Apr 9, 2025
Promise `array::from_fn` is generated in order of increasing indices Fixes rust-lang#139061 I agree this needs to be documented because of the `FnMut`, either with a guarantee or to explicitly disclaim one. I'm pretty sure this will be non-controversial (like the other "well sure you *could* do it in a different order, but why?" things were), but I couldn't find any previous libs-api decision on it so it's seemingly a new promise that will need FCP. Basically, yes, it would be plausible to fill in the reverse order, but there's no obvious way we could ever know that that might even be a good idea, so forward seems like an easy thing to promise. We could always add a `from_fn_rev` or something later if there's ever a strong enough need, but it seems unlikely. Let's just do the obvious thing so it matches what `[gen(0), gen(1), …, gen(N-1)]` does.
rust-timer
added a commit
to rust-lang-ci/rust
that referenced
this issue
Apr 9, 2025
Rollup merge of rust-lang#139099 - scottmcm:from_fn_docs, r=Amanieu Promise `array::from_fn` is generated in order of increasing indices Fixes rust-lang#139061 I agree this needs to be documented because of the `FnMut`, either with a guarantee or to explicitly disclaim one. I'm pretty sure this will be non-controversial (like the other "well sure you *could* do it in a different order, but why?" things were), but I couldn't find any previous libs-api decision on it so it's seemingly a new promise that will need FCP. Basically, yes, it would be plausible to fill in the reverse order, but there's no obvious way we could ever know that that might even be a good idea, so forward seems like an easy thing to promise. We could always add a `from_fn_rev` or something later if there's ever a strong enough need, but it seems unlikely. Let's just do the obvious thing so it matches what `[gen(0), gen(1), …, gen(N-1)]` does.
github-actions bot
pushed a commit
to model-checking/verify-rust-std
that referenced
this issue
Apr 19, 2025
Promise `array::from_fn` is generated in order of increasing indices Fixes rust-lang#139061 I agree this needs to be documented because of the `FnMut`, either with a guarantee or to explicitly disclaim one. I'm pretty sure this will be non-controversial (like the other "well sure you *could* do it in a different order, but why?" things were), but I couldn't find any previous libs-api decision on it so it's seemingly a new promise that will need FCP. Basically, yes, it would be plausible to fill in the reverse order, but there's no obvious way we could ever know that that might even be a good idea, so forward seems like an easy thing to promise. We could always add a `from_fn_rev` or something later if there's ever a strong enough need, but it seems unlikely. Let's just do the obvious thing so it matches what `[gen(0), gen(1), …, gen(N-1)]` does.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Location
https://doc.rust-lang.org/nightly/std/array/fn.from_fn.html
Summary
Currently the documentation surrounding
array::from_fn
does not guarantee the order in which the array items are generated. Given that it accepts anFnMut
this seems dangerous. For example, code such as:currently compiles as expected but would fail if
array::from_fn
constructed the array out of order.The documentation should be updated to either confirm that the code above is a valid use of
array::from_fn
or to warn users that elements may be generated out of order.This was previously mentioned in the discussion around #102609 but didn't end up getting fixed.
The text was updated successfully, but these errors were encountered: