Skip to content

Suggest array::from_fn in array initialization #119530

Closed
@est31

Description

@est31

For this snippet (playground):

fn foo() -> String { String::new() }

fn main() {
    let string_arr = [foo(); 64];
}

rustc gives:

error[E0277]: the trait bound `String: Copy` is not satisfied
 --> src/main.rs:3:23
  |
3 |     let string_arr = [foo(); 64];
  |                       ^^^^^ the trait `Copy` is not implemented for `String`
  |
  = note: the `Copy` trait is required because this value will be copied for each element of the array

Since #113912, for const-able values, rustc suggests the creation of a constant binding. Here this is not possible because foo is not const fn. However, we can suggest array::from_fn instead.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsE-help-wantedCall for participation: Help is requested to fix this issue.T-compilerRelevant to the compiler 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