about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/libcollections/string.rs12
-rw-r--r--src/libcollectionstest/string.rs2
2 files changed, 9 insertions, 5 deletions
diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs
index 2226116585f..306fad2328b 100644
--- a/src/libcollections/string.rs
+++ b/src/libcollections/string.rs
@@ -992,10 +992,12 @@ impl String {
 
     /// Shortens this `String` to the specified length.
     ///
+    /// If `new_len` is greater than the string's current length, this has no
+    /// effect.
+    ///
     /// # Panics
     ///
-    /// Panics if `new_len` > current length, or if `new_len` does not lie on a
-    /// [`char`] boundary.
+    /// Panics if `new_len` does not lie on a [`char`] boundary.
     ///
     /// [`char`]: ../../std/primitive.char.html
     ///
@@ -1013,8 +1015,10 @@ impl String {
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn truncate(&mut self, new_len: usize) {
-        assert!(self.is_char_boundary(new_len));
-        self.vec.truncate(new_len)
+        if new_len <= self.len() {
+            assert!(self.is_char_boundary(new_len));
+            self.vec.truncate(new_len)
+        }
     }
 
     /// Removes the last character from the string buffer and returns it.
diff --git a/src/libcollectionstest/string.rs b/src/libcollectionstest/string.rs
index d8e01f3800c..d71529023f4 100644
--- a/src/libcollectionstest/string.rs
+++ b/src/libcollectionstest/string.rs
@@ -248,10 +248,10 @@ fn test_str_truncate() {
 }
 
 #[test]
-#[should_panic]
 fn test_str_truncate_invalid_len() {
     let mut s = String::from("12345");
     s.truncate(6);
+    assert_eq!(s, "12345");
 }
 
 #[test]