Skip to content

Proposal: Implement assignment operator overloads #393

Closed
@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.

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

    Issue actions