/// 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
///
#[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.