about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2013-06-15 23:17:53 +1000
committerHuon Wilson <dbau.pp+github@gmail.com>2013-06-16 10:50:28 +1000
commit30973ccb9041a5035cdb24b382a0d5634ccc9e2e (patch)
tree3692360c9ae278a7b8f4ef15d20dc82f57ef305f /src/libstd
parentee25cf8d75671415e74ff3fe1a3c0ba42e35396a (diff)
downloadrust-30973ccb9041a5035cdb24b382a0d5634ccc9e2e.tar.gz
rust-30973ccb9041a5035cdb24b382a0d5634ccc9e2e.zip
std: allow any sort of string to be Added with +.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/str.rs24
1 files changed, 22 insertions, 2 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index 1ab07003b6e..21f747317f4 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -913,10 +913,12 @@ pub mod traits {
     use cmp::{TotalOrd, Ordering, Less, Equal, Greater, Eq, Ord, Equiv, TotalEq};
     use super::{Str, eq_slice};
 
-    impl<'self> Add<&'self str,~str> for ~str {
+    impl<'self> Add<&'self str,~str> for &'self str {
         #[inline(always)]
         fn add(&self, rhs: & &'self str) -> ~str {
-            self.append((*rhs))
+            let mut ret = self.to_owned();
+            ret.push_str(*rhs);
+            ret
         }
     }
 
@@ -3138,6 +3140,24 @@ mod tests {
     }
 
     #[test]
+    fn test_add() {
+        macro_rules! t (
+            ($s1:expr, $s2:expr, $e:expr) => {
+                assert_eq!($s1 + $s2, $e);
+                assert_eq!($s1.to_owned() + $s2, $e);
+                assert_eq!($s1.to_managed() + $s2, $e);
+            }
+        );
+
+        t!("foo",  "bar", ~"foobar");
+        t!("foo", @"bar", ~"foobar");
+        t!("foo", ~"bar", ~"foobar");
+        t!("ศไทย中",  "华Việt Nam", ~"ศไทย中华Việt Nam");
+        t!("ศไทย中", @"华Việt Nam", ~"ศไทย中华Việt Nam");
+        t!("ศไทย中", ~"华Việt Nam", ~"ศไทย中华Việt Nam");
+    }
+
+    #[test]
     fn test_iterator() {
         use iterator::*;
         let s = ~"ศไทย中华Việt Nam";