Closed
Description
The following code:
fn main() {
let strs: [String; 5] = [String::new(); 5];
}
gives the following error:
error[E0277]: the trait bound `std::string::String: std::marker::Copy` is not satisfied
--> src/main.rs:2:29
|
2 | let strs: [String; 5] = [String::new(); 5];
| ^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::string::String`
|
= note: the `Copy` trait is required because the repeated element will be copied
However, String::new()
is a const fn
, so this code can be made to compile by creating a new const
item:
fn main() {
const EMPTY: String = String::new();
let strs: [String; 5] = [EMPTY; 5];
}
We should suggest this to user when a const
function is being called in the array initializer.
Assuming that rust-lang/rfcs#2920 is accepted, we could eventually suggest an inline const
block.
On Nightly, we could suggest an inline const
block.
Metadata
Metadata
Assignees
Labels
Area: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Messages for errors, warnings, and lintsArea: Suggestions generated by the compiler applied by `cargo fix`Category: An issue proposing an enhancement or a PR with one.Diagnostics: Confusing error or lint; hard to understand for new users.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Relevant to the compiler team, which will review and decide on the PR/issue.