Skip to content

array::from_fn behavior unclear from documentation #139061

Closed
@SyxtonPrime

Description

@SyxtonPrime

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 an FnMut this seems dangerous. For example, code such as:

let mut iter = 0..5;
let arr = core::array::from_fn(|_| iter.next().unwrap());
assert_eq!(arr, [0, 1, 2, 3, 4]);

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.

Metadata

Metadata

Assignees

No one assigned

    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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions