Skip to content

Proposal: Implement assignment operator overloads #393

@rust-highfive

Description

@rust-highfive

Issue by bjz
Sunday Apr 21, 2013 at 22:16 GMT

For earlier discussion, see rust-lang/rust#5992

This issue was labelled with: A-libs, A-traits, I-enhancement, P-low in the Rust repository


This would be very useful for core::num (see #4819), and also mathematics libraries.

// overloads `+=`
#[lang="add_assign"]
trait AddAssign<RHS> {
    fn add_assign(&mut self, &other: RHS);
}

// overloads `-=`
#[lang="sub_assign"]
trait SubAssign<RHS> {
    fn sub_assign(&mut self, &other: RHS);
}

// overloads `*=`
#[lang="mul_assign"]
trait MulAssign<RHS> {
    fn mul_assign(&mut self, &other: RHS);
}

// overloads `/=`
#[lang="quot_assign"]
trait QuotAssign<RHS> {
    fn quot_assign(&mut self, &other: RHS);
}

// overloads `%=`
#[lang="rem_assign"]
trait RemAssign<RHS> {
    fn rem_assign(&mut self, &other: RHS);
}

It would also be useful to be able to assign to values accessed via the index operator. This would return a mutable reference to the element. =, +=, -=, *=, /=, and %= would then be based off the overloads defined for that element type.

#[lang="index_assign"]
trait IndexAssign<Index,Element> {
    fn mut_index<'a>(&'a mut self, index: Index) -> &'a mut Element;
}

Edit: Removed Assign trait for = operator.

Activity

kkimdev

kkimdev commented on Dec 27, 2014

@kkimdev

Q: Why did you remove Assign trait for = operator from the RFC?

eddyb

eddyb commented on Dec 27, 2014

@eddyb
Member

Overloading = would violate Rust's doctrine of "everything moves without side effects".

mtahmed

mtahmed commented on Jan 3, 2015

@mtahmed

(noob) Q: Does rust provide any way of doing something like this right now?:

let my_big_num: BigNum = 42;

Basically, to be able to assign "constants" to objects and have it work?
If not, would Assign trait have been the answer?
If yes, could you please explain a bit more why the Assign trait is bad?

Kimundi

Kimundi commented on Jan 3, 2015

@Kimundi
Contributor

Assign would be about modifying an existing instance of the type, not about creating a fresh one.

And I think for that, you'd rather want to push for making number literals generic, so that that turns into something like let my_big_num: BigNum = literal_from_integer(42);.

japaric

japaric commented on Mar 8, 2015

@japaric
Contributor
nagisa

nagisa commented on Sep 2, 2015

@nagisa
Member

Should default trait implementations like this be provided?

impl<R, T> AddAssign<R> for T 
where T: Add<R> {
    fn add_assign(&mut self, &rhs: R) { *self = self + rhs; }
}

👍 for this either way.

alexcrichton

alexcrichton commented on Feb 11, 2016

@alexcrichton
Member

This is done!

added a commit that references this issue on Mar 5, 2019
e58ebb5
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @alexcrichton@eddyb@kkimdev@nagisa@mtahmed

        Issue actions

          Proposal: Implement assignment operator overloads · Issue #393 · rust-lang/rfcs