Skip to content

Array initialization with repeated enum variant requires the whole enum type to be Copy #30754

@benw

Description

@benw

I want to initialize an array of Option<Foo> with [None; 5]. The None variant is constant and has no associated data, so there seems to be no reason that it couldn't be copied into all the entries in the array:

struct Foo;

fn main() {
    let mut array: [Option<Foo>; 5] = [None; 5];
}

Sadly this errors because Foo is not Copy, so Option<Foo> is not Copy.

<anon>:4:35: 4:44 error: the trait `core::marker::Copy` is not implemented for the type `Foo` [E0277]
<anon>:4     let array: [Option<Foo>; 5] = [None; 5];
                                           ^~~~~~~~~

I gather array initialization treats None as an expression in this case? Could it instead notice that None is a constant, and copy it to all the elements in the array?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions