-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
std: Bring back half of Add on String #14482
Conversation
This has been a highly requested feature since the move away from |
Copying my comment from #14481 for posterity: I'm not keen on the Add impl for string. It seems to easily encourage inefficient/overallocating code since it doesn't reuse the allocation of its argument, meaning s = s + a is horrible (especially in a loop). (And I relatively often see code approximately equivalent to that.) I would be very interested in waiting a little longer to see if not having it is just too unbearable. (Also this addition on strings isn't even commutative, i.e. it flies in the face of mathematical convention for the + operator. :P ) |
Travis failed btw. |
I agree. This seems too much like Python. |
I think people expect something like this (even when imperformant), since many other languages provide it: http://rosettacode.org/wiki/String_concatenation |
How about we implement the That way one can do That way people can have some sugar, without us encouraging inefficient reallocations. |
How about adding new versions of add that take parameters by value instead of by reference (for a total of 4 versions), and then having the compiler automatically pass by value when the value is no longer used? The more general One could also automatically rewrite |
@bill-myers That's rather magical behavior. Adding a later use of a variable should not silently change the semantics of earlier code. Note here that it's not just allocation semantics; the actual behavior could be subject to change as well, as this is just invoking user-defined code. |
Theoretically we could actually just have impl<'a,'b> Add<&'b BigInt, BigInt> for &'a BigInt {
fn add(self, other: &'b BigInt) -> BigInt { ... }
} However, (with current-Rust) this would mean that |
fn add(&self, other: &S) -> String { | ||
let mut s = self.to_string(); | ||
s.push_str(other.as_slice()); | ||
return s; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: return s;
-> s
:)
This adds an implementation of Add for String where the rhs is <S: Str>. The other half of adding strings is where the lhs is <S: Str>, but coherence and the libcore separation currently prevent that.
fix: Fix relative path creation using the wrong path accessor This should hopefully fix the other errors with paths people seem to be getting
This adds an implementation of Add for String where the rhs is
<S: Str>
. Theother half of adding strings is where the lhs is
<S: Str>
, but coherence andthe libcore separation currently prevent that.