Skip to content

impl Add<char> and AddAssign<char> for String #66490

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
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
2 changes: 1 addition & 1 deletion src/doc/book
Submodule book updated 1 files
+1 −1 src/ch10-00-generics.md
2 changes: 1 addition & 1 deletion src/doc/nomicon
2 changes: 1 addition & 1 deletion src/doc/rust-by-example
2 changes: 1 addition & 1 deletion src/doc/rustc-guide
107 changes: 107 additions & 0 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,91 @@ impl Add<&str> for String {
}
}

// This had to be added to avoid breakage after adding `impl Add<char> for String`
#[allow(missing_docs)]
#[stable(feature = "extra_add_string_and_dbl_ref_str", since = "1.41.0")]
impl Add<&&str> for String {
type Output = String;

#[inline]
fn add(mut self, other: &&str) -> String {
self.push_str(other);
self
}
}

// This had to be added to avoid breakage after adding `impl Add<char> for String`
#[allow(missing_docs)]
#[stable(feature = "extra_add_string_and_ref_string", since = "1.41.0")]
impl Add<&String> for String {
type Output = String;

#[inline]
fn add(mut self, other: &String) -> String {
self.push_str(other);
self
}
}

// This had to be added to avoid breakage after adding `impl Add<char> for String`
#[allow(missing_docs)]
#[stable(feature = "extra_add_string_and_dbl_ref_string", since = "1.41.0")]
impl Add<&&String> for String {
type Output = String;

#[inline]
fn add(mut self, other: &&String) -> String {
self.push_str(other);
self
}
}

/// Implements the `+` operator for concatenating a string and a char together.
///
/// This consumes the `String` on the left-hand side and re-uses its buffer (growing it if
/// necessary). This is done to avoid allocating a new `String` and copying the entire contents on
/// every operation, which would lead to `O(n^2)` running time when building an `n`-byte string by
/// repeated concatenation.
///
/// # Examples
///
/// Concatenating a `String` with a `char` takes the `String` by value and copies the `char`:
///
/// ```
/// let a = String::from("hello world! ");
/// let b = '👋';
/// let c = a + b;
/// // `a` is moved and can no longer be used here.
/// ```
///
/// If you want to keep using the initial `String`, you can clone it and append to the
/// clone instead:
///
/// ```
/// let a = String::from("hello world! ");
/// let b = '👋';
/// let c = a.clone() + b;
/// // `a` is still valid here.
/// ```
///
/// Concatenating `char` to a `&str` slice can be done by converting the `&str` to a `String`:
///
/// ```
/// let a = "hello world! ";
/// let b = '👋';
/// let c = a.to_string() + b;
/// ```
#[stable(feature = "add_string_and_char", since = "1.41.0")]
impl Add<char> for String {
type Output = String;

#[inline]
fn add(mut self, other: char) -> String {
self.push(other);
self
}
}

/// Implements the `+=` operator for appending to a `String`.
///
/// This has the same behavior as the [`push_str`][String::push_str] method.
Expand All @@ -1993,6 +2078,28 @@ impl AddAssign<&str> for String {
}
}

/// Implements the `+=` operator for appending to a `String`.
///
/// This has the same behavior as the [`push_str`][String::push_str] method.
#[stable(feature = "string_add_assign_string", since = "1.41.0")]
impl AddAssign<&String> for String {
#[inline]
fn add_assign(&mut self, other: &String) {
self.push_str(other);
}
}

/// Implements the `+=` operator for appending a `char` to a `String`.
///
/// This has the same behavior as the [`push`][String::push] method.
#[stable(feature = "string_add_assign_char", since = "1.41.0")]
impl AddAssign<char> for String {
#[inline]
fn add_assign(&mut self, other: char) {
self.push(other);
}
}

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

#[test]
Expand Down Expand Up @@ -304,9 +306,10 @@ fn test_str_clear() {
fn test_str_add() {
let a = String::from("12345");
let b = a + "2";
let b = b + "2";
assert_eq!(b.len(), 7);
assert_eq!(b, "1234522");
let b = b + "2 ";
let b = b + '👋';
assert_eq!(b.len(), 12);
assert_eq!(b, "1234522 👋");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ impl<'a> Parser<'a> {
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix })
= next_token.kind {
if self.token.span.hi() == next_token.span.lo() {
let s = String::from("0.") + &symbol.as_str();
let s = String::from("0.") + &*symbol.as_str();
let kind = TokenKind::lit(token::Float, Symbol::intern(&s), suffix);
return Some(Token::new(kind, self.token.span.to(next_token.span)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/llvm-project
2 changes: 1 addition & 1 deletion src/tools/cargo
Submodule cargo updated 43 files
+0 −1 .gitignore
+2 −72 CHANGELOG.md
+3 −3 Cargo.toml
+6 −7 ci/azure-install-rust.yml
+3 −3 crates/cargo-test-support/src/paths.rs
+1 −1 crates/crates-io/Cargo.toml
+3 −16 src/bin/cargo/main.rs
+18 −3 src/cargo/core/compiler/compilation.rs
+2 −24 src/cargo/ops/cargo_doc.rs
+4 −8 src/cargo/ops/cargo_new.rs
+1 −1 src/cargo/ops/fix.rs
+37 −27 src/cargo/util/config/mod.rs
+19 −19 src/cargo/util/cpu.rs
+1 −3 src/doc/man/cargo-doc.adoc
+1 −3 src/doc/man/cargo-rustdoc.adoc
+1 −3 src/doc/man/generated/cargo-doc.html
+1 −3 src/doc/man/generated/cargo-rustdoc.html
+0 −1 src/doc/src/SUMMARY.md
+0 −505 src/doc/src/reference/build-script-examples.md
+480 −306 src/doc/src/reference/build-scripts.md
+0 −7 src/doc/src/reference/config.md
+2 −31 src/doc/src/reference/environment-variables.md
+1 −6 src/doc/src/reference/manifest.md
+2 −2 src/doc/src/reference/publishing.md
+15 −15 src/doc/src/reference/specifying-dependencies.md
+3 −5 src/etc/man/cargo-doc.1
+3 −3 src/etc/man/cargo-help.1
+3 −3 src/etc/man/cargo-init.1
+3 −3 src/etc/man/cargo-install.1
+3 −3 src/etc/man/cargo-login.1
+3 −3 src/etc/man/cargo-new.1
+3 −3 src/etc/man/cargo-owner.1
+3 −5 src/etc/man/cargo-rustdoc.1
+3 −3 src/etc/man/cargo-search.1
+3 −3 src/etc/man/cargo-uninstall.1
+3 −3 src/etc/man/cargo-version.1
+3 −3 src/etc/man/cargo-yank.1
+2 −2 src/etc/man/cargo.1
+5 −0 tests/testsuite/build_script.rs
+0 −43 tests/testsuite/cargo_command.rs
+2 −29 tests/testsuite/init.rs
+5 −0 tests/testsuite/rustc_info_cache.rs
+6 −1 tests/testsuite/rustdoc.rs
2 changes: 1 addition & 1 deletion src/tools/clippy
2 changes: 1 addition & 1 deletion src/tools/miri
Submodule miri updated 43 files
+2 −1 .appveyor.yml
+2 −1 .travis.yml
+0 −32 Cargo.lock
+1 −1 Cargo.toml
+1 −1 rust-version
+1 −3 src/bin/cargo-miri.rs
+10 −0 src/bin/miri-rustc-tests.rs
+10 −0 src/bin/miri.rs
+29 −17 src/eval.rs
+1 −1 src/intptrcast.rs
+7 −16 src/machine.rs
+35 −21 src/operator.rs
+1 −1 src/shims/dlsym.rs
+1 −1 src/shims/env.rs
+33 −33 src/shims/foreign_items.rs
+86 −91 src/shims/fs.rs
+34 −26 src/shims/intrinsics.rs
+2 −2 src/shims/mod.rs
+10 −10 src/stacked_borrows.rs
+17 −20 test-cargo-miri/Cargo.lock
+1 −1 tests/compile-fail/exact_div2.rs
+1 −1 tests/compile-fail/exact_div3.rs
+0 −7 tests/compile-fail/ptr_offset_0_plus_0.rs
+0 −6 tests/compile-fail/stacked_borrows/issue-miri-1050-1.rs
+0 −7 tests/compile-fail/stacked_borrows/issue-miri-1050-2.rs
+0 −2 tests/compile-fail/validity/invalid_wide_raw.rs
+3 −2 tests/compiletest.rs
+31 −0 tests/run-pass/box_box_trait.rs
+0 −0 tests/run-pass/call_drop_on_array_elements.rs
+0 −0 tests/run-pass/call_drop_on_fat_ptr_array_elements.rs
+0 −0 tests/run-pass/call_drop_on_zst_array_elements.rs
+0 −0 tests/run-pass/call_drop_through_owned_slice.rs
+0 −0 tests/run-pass/call_drop_through_trait_object.rs
+0 −0 tests/run-pass/call_drop_through_trait_object_rc.rs
+2 −14 tests/run-pass/closures.rs
+0 −145 tests/run-pass/dyn-traits.rs
+0 −12 tests/run-pass/floats.rs
+1 −1 tests/run-pass/iter.rs
+0 −0 tests/run-pass/miri-issue-133.rs
+0 −29 tests/run-pass/ptr_offset_from.rs
+0 −29 tests/run-pass/stacked-borrows/stacked-borrows.rs
+0 −7 tests/run-pass/stacked-borrows/stacked-borrows.stderr
+30 −0 tests/run-pass/traits.rs