Skip to content

array::from_fn behavior is unclear from documentation #102609

Closed
@jgao222

Description

@jgao222

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 states

Creates an array [T; N] where each array element T is returned by the cb call.

Arguments

  • cb: Callback where the passed argument is the current array index.

It 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 calling cb(i).

The function try_from_fn is similarly unclear. Anyone have thoughts on this?

Metadata

Metadata

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libsRelevant to the library team, which will review and decide on the PR/issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions