-
Notifications
You must be signed in to change notification settings - Fork 13k
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
array::from_fn
behavior is unclear from documentation
#102609
Comments
I'm not sure if the Creates an array of type (Maybe "returned value from" instead of "result of"?) |
They're both the same function (std is a re-export of the core one), so https://doc.rust-lang.org/nightly/std/array/fn.from_fn.html is a sufficient link. I agree the "Arguments" section is pretty unusual (I don't remember seeing it on anything else), and I'm not sure that "callback" is a usual word here. The "Examples" section does, I think, really help clarify what it's going to do. But both the prose and the examples could due with some expansion. For example, this should probably get a guarantee of the order in which the items are generated, since it takes Oh, and I might as well cc #100462, which is open with some other suggested improvements. |
I don't know about how best to document the type inference in the other issue (though, to throw my 2¢ in, I'm not sure it needs documentation :), but you raise a good point about Creates an array of type Is "the end of the array" interchangeable with |
Consider writing out pseudocode as well, as that can be more helpful than prose for some people. For example, you could rename the function to [gen(0), gen(1), gen(2), …, gen(N-2), gen(N-1)] That's pretty accurate, and the But your note about You could also describe it by analogy to other things. For example, this is in a way the eager version of |
@rustbot claim |
…n, r=scottmcm improve array_from_fn documenation Improves array::from_fn documentation Fixes rust-lang#102609 There were also unresolved comments from [this PR rust-lang#100462](rust-lang#100462), which have been added to my PR
Location
https://doc.rust-lang.org/std/array/fn.from_fn.html and https://doc.rust-lang.org/core/array/fn.from_fn.html
Summary
The documentation for
from_fn
statesIt is not clear what "current array index" refers to, since the function description doesn't necessarily indicate that there is iteration happening to create the resulting array.
Additionally, the first line summary doesn't actually reflect the behavior of the function at all. It mentions "the cb call" -- as if there is only one call to
cb
.I think would be more clear if the documentation somehow mentioned that the
i
-th element in the output array is the result of callingcb(i)
.The function
try_from_fn
is similarly unclear. Anyone have thoughts on this?The text was updated successfully, but these errors were encountered: