Skip to content

Commit f7f95c8

Browse files
committed
std: Bring back half of Add on String
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.
1 parent 05ca9f7 commit f7f95c8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libcollections/string.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,14 @@ impl<'a, S: Str> Equiv<S> for String {
352352
}
353353
}
354354

355+
impl<S: Str> Add<S, String> for String {
356+
fn add(&self, other: &S) -> String {
357+
let mut s = self.to_string();
358+
s.push_str(other.as_slice());
359+
return s;
360+
}
361+
}
362+
355363
#[cfg(test)]
356364
mod tests {
357365
use std::prelude::*;
@@ -469,4 +477,13 @@ mod tests {
469477
assert_eq!(s.len(), 0);
470478
assert_eq!(s.as_slice(), "");
471479
}
480+
481+
#[test]
482+
fn test_str_add() {
483+
let a = String::from_str("12345");
484+
let b = a + "2";
485+
let b = b + String::from_str("2");
486+
assert_eq!(b.len(), 7);
487+
assert_eq!(b.as_slice(), "1234522");
488+
}
472489
}

0 commit comments

Comments
 (0)