]> git.lizzy.rs Git - rust.git/commitdiff
std: Bring back half of Add on String
authorAlex Crichton <alex@alexcrichton.com>
Wed, 28 May 2014 04:34:00 +0000 (21:34 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 25 Jun 2014 00:17:09 +0000 (17:17 -0700)
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.

src/libcollections/string.rs

index 76f53c9b257493216ef5ee9e715ac845fd0560de..6d1fc43a4f1fd2d1e4b44799bbaa132ea6fd7c42 100644 (file)
@@ -352,6 +352,14 @@ fn equiv(&self, other: &S) -> bool {
     }
 }
 
+impl<S: Str> Add<S, String> for String {
+    fn add(&self, other: &S) -> String {
+        let mut s = self.to_string();
+        s.push_str(other.as_slice());
+        return s;
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use std::prelude::*;
@@ -469,4 +477,13 @@ fn test_str_clear() {
         assert_eq!(s.len(), 0);
         assert_eq!(s.as_slice(), "");
     }
+
+    #[test]
+    fn test_str_add() {
+        let a = String::from_str("12345");
+        let b = a + "2";
+        let b = b + String::from_str("2");
+        assert_eq!(b.len(), 7);
+        assert_eq!(b.as_slice(), "1234522");
+    }
 }