Skip to content

Remove wrong &str + String and &[T] + Vec<T> implementations #19965

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

Merged
merged 1 commit into from
Dec 22, 2014

Conversation

japaric
Copy link
Member

@japaric japaric commented Dec 18, 2014

TL;DR I wrongly implemented these two ops, namely "prefix" + "suffix".to_string() gives back "suffixprefix". Let's remove them.

The correct implementation of these operations (lhs.clone().push_str(rhs.as_slice())) is really wasteful, because the lhs has to be cloned and the rhs gets moved/consumed just to be dropped (no buffer reuse). For this reason, I'd prefer to drop the implementation instead of fixing it. This leaves us with the fact that you'll be able to do String + &str but not &str + String, which may be unexpected.

r? @aturon
Closes #19952

@aturon
Copy link
Member

aturon commented Dec 18, 2014

I'm not sure I agree that the potential inefficiency of a particular operation is a good reason to lose the symmetry of addition. People doing string manipulation where this kind of thing matters should probably be working more explicitly in the first place; these operators are mainly for convenience.

Also, it's feasible to reuse the buffer if you wanted -- basically a bit like the insert operation on Vec, but inserting a whole slice on the left. You could special-case this behavior depending on whether the existing capacity was sufficient, and otherwise allocate a fresh buffer and copy each bit in.

@alexcrichton
Copy link
Member

Ah yes I should have commented to that effect as well. I'm ok with removing these for now, but I'd like to add them back at some point. You can always reserve extra space, slide everything to the right in the owned container, and then clone the slice into the empty space for an "optimal" solution I believe.

@renato-zannon
Copy link
Contributor

Having a String#prepend method that works as @alexcrichton described sounds useful regardless of the symmetry of addition.

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Dec 21, 2014
TL;DR I wrongly implemented these two ops, namely `"prefix" + "suffix".to_string()` gives back `"suffixprefix"`. Let's remove them.

The correct implementation of these operations (`lhs.clone().push_str(rhs.as_slice())`) is really wasteful, because the lhs has to be cloned and the rhs gets moved/consumed just to be dropped (no buffer reuse). For this reason, I'd prefer to drop the implementation instead of fixing it. This leaves us with the fact that you'll be able to do `String + &str` but not `&str + String`, which may be unexpected.

r? @aturon
Closes rust-lang#19952
@bors bors merged commit 1a996f9 into rust-lang:master Dec 22, 2014
@japaric japaric deleted the remove-wrong-add branch January 4, 2015 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

String + &str and Vec<T> + &[T] work in unexpected way
5 participants