Skip to content

proposal: Assign trait #4329

Closed
Closed
@thestinger

Description

@thestinger

At the moment, a = b.clone() is the way to copy-assign to a value in generic code (at least once there's a default implementation for Copyable types, per #3313).

If a is uninitialized, that's perfectly optimal. When the value is already initialized, assignment can often be done with fewer allocations; a list or chunked list can be partially or fully reused, and even a vector/string can often be reused if it's big enough.

So, something like this (with a default impl for Cloneable types):

pub trait Assign<T>() {
    fn assign(&mut self, other: &T) -> self;
}

a.assign(&b);

There's no need for any operator overload, since a = b is just an implicit copy or a move.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions