-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Closed
Description
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 commentedon Dec 27, 2014
Q: Why did you remove
Assign
trait for=
operator from the RFC?eddyb commentedon Dec 27, 2014
Overloading
=
would violate Rust's doctrine of "everything moves without side effects".mtahmed commentedon Jan 3, 2015
(noob) Q: Does rust provide any way of doing something like this right now?:
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 commentedon Jan 3, 2015
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 commentedon Mar 8, 2015
Implementation
RFC
+=
rust#28160nagisa commentedon Sep 2, 2015
Should default trait implementations like this be provided?
👍 for this either way.
alexcrichton commentedon Feb 11, 2016
This is done!
Merge pull request rust-lang#393 from Alonski/patch-3