Skip to content

Implement Mul<usize> and MulAssign<usize> for String #41350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use core::fmt;
use core::hash;
use core::iter::{FromIterator, FusedIterator};
use core::mem;
use core::ops::{self, Add, AddAssign, Index, IndexMut};
use core::ops::{self, Add, AddAssign, Index, IndexMut, Mul, MulAssign};
use core::ptr;
use core::str as core_str;
use core::str::pattern::Pattern;
Expand Down Expand Up @@ -1710,6 +1710,48 @@ impl<'a> AddAssign<&'a str> for String {
}
}

/// Implements the `*` operator for multiplying `String` content.
///
/// ```
/// let one = String::from("one");
/// assert_eq!(one * 3, "oneoneone");
/// ```
#[stable(feature = "stringmultiply", since = "1.18.0")]
impl Mul<usize> for String {
type Output = String;

#[inline]
fn mul(self, rhs: usize) -> String {
let size = self.len() * rhs;
let mut out = String::with_capacity(size);
for _ in 0..rhs {
out.push_str(self.as_str());
}
out
}
}

/// Implements the `*=` operator for multiplyng `String` content.
///
/// ```
/// let mut one = String::from("one");
/// one *= 3;
/// assert_eq!(one, "oneoneone");
/// ```
#[stable(feature = "stringmultiply", since = "1.18.0")]
impl MulAssign<usize> for String {
#[inline]
fn mul_assign(&mut self, rhs: usize) {
let content = self.clone();
let size = content.len() * rhs;
self.clear();
self.reserve(size);
for _ in 0..rhs {
self.push_str(content.as_str());
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::Range<usize>> for String {
type Output = str;
Expand Down
18 changes: 18 additions & 0 deletions src/libcollections/tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ fn test_add_assign() {
assert_eq!(s.as_str(), "abcประเทศไทย中华Việt Nam");
}

#[test]
fn test_mul() {
let s = String::from("abcประเทศไทย中华Việt Nam") * 0;
assert_eq!(s, "");
let s = String::from("abcประเทศไทย中华Việt Nam") * 2;
assert_eq!(s, "abcประเทศไทย中华Việt Namabcประเทศไทย中华Việt Nam");
}

#[test]
fn test_mul_assign() {
let mut s = String::from("abcประเทศไทย中华Việt Nam");
s *= 0;
assert_eq!(s, "");
s = String::from("abcประเทศไทย中华Việt Nam");
s *= 2;
assert_eq!(s, "abcประเทศไทย中华Việt Namabcประเทศไทย中华Việt Nam");
}

#[test]
fn test_push() {
let mut data = String::from("ประเทศไทย中");
Expand Down