Skip to content

proposal: Assign trait #4329

@thestinger

Description

@thestinger
Contributor

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.

Activity

pcwalton

pcwalton commented on Jun 12, 2013

@pcwalton
Contributor

I don't think that this is backwards incompatible.

thestinger

thestinger commented on Jun 12, 2013

@thestinger
ContributorAuthor

@pcwalton: agreed, this shouldn't break anything, it's just an extra method/trait

graydon

graydon commented on Aug 1, 2013

@graydon
Contributor

just a bug, removing milestone/nomination.

sanxiyn

sanxiyn commented on Oct 24, 2013

@sanxiyn
Member

Tagging RFC.

thestinger

thestinger commented on Nov 2, 2013

@thestinger
ContributorAuthor

I think this would be better as a default method on Clone rather than a trait.

added a commit that references this issue on May 22, 2025
44d3c93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @graydon@sanxiyn@pcwalton@thestinger

        Issue actions

          proposal: Assign trait · Issue #4329 · rust-lang/rust