Skip to content

Commit c5e19ca

Browse files
committed
impl Add<char> and AddAssign<char> for String + rebase with master
1 parent 0f0c640 commit c5e19ca

File tree

12 files changed

+118
-11
lines changed

12 files changed

+118
-11
lines changed

src/doc/book

src/doc/rustc-guide

src/liballoc/string.rs

+107
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,91 @@ impl Add<&str> for String {
19821982
}
19831983
}
19841984

1985+
// This had to be added to avoid breakage after adding `impl Add<char> for String`
1986+
#[allow(missing_docs)]
1987+
#[stable(feature = "extra_add_string_and_dbl_ref_str", since = "1.41.0")]
1988+
impl Add<&&str> for String {
1989+
type Output = String;
1990+
1991+
#[inline]
1992+
fn add(mut self, other: &&str) -> String {
1993+
self.push_str(other);
1994+
self
1995+
}
1996+
}
1997+
1998+
// This had to be added to avoid breakage after adding `impl Add<char> for String`
1999+
#[allow(missing_docs)]
2000+
#[stable(feature = "extra_add_string_and_ref_string", since = "1.41.0")]
2001+
impl Add<&String> for String {
2002+
type Output = String;
2003+
2004+
#[inline]
2005+
fn add(mut self, other: &String) -> String {
2006+
self.push_str(other);
2007+
self
2008+
}
2009+
}
2010+
2011+
// This had to be added to avoid breakage after adding `impl Add<char> for String`
2012+
#[allow(missing_docs)]
2013+
#[stable(feature = "extra_add_string_and_dbl_ref_string", since = "1.41.0")]
2014+
impl Add<&&String> for String {
2015+
type Output = String;
2016+
2017+
#[inline]
2018+
fn add(mut self, other: &&String) -> String {
2019+
self.push_str(other);
2020+
self
2021+
}
2022+
}
2023+
2024+
/// Implements the `+` operator for concatenating a string and a char together.
2025+
///
2026+
/// This consumes the `String` on the left-hand side and re-uses its buffer (growing it if
2027+
/// necessary). This is done to avoid allocating a new `String` and copying the entire contents on
2028+
/// every operation, which would lead to `O(n^2)` running time when building an `n`-byte string by
2029+
/// repeated concatenation.
2030+
///
2031+
/// # Examples
2032+
///
2033+
/// Concatenating a `String` with a `char` takes the `String` by value and copies the `char`:
2034+
///
2035+
/// ```
2036+
/// let a = String::from("hello world! ");
2037+
/// let b = '👋';
2038+
/// let c = a + b;
2039+
/// // `a` is moved and can no longer be used here.
2040+
/// ```
2041+
///
2042+
/// If you want to keep using the initial `String`, you can clone it and append to the
2043+
/// clone instead:
2044+
///
2045+
/// ```
2046+
/// let a = String::from("hello world! ");
2047+
/// let b = '👋';
2048+
/// let c = a.clone() + b;
2049+
/// // `a` is still valid here.
2050+
/// ```
2051+
///
2052+
/// Concatenating `char` to a `&str` slice can be done by converting the `&str` to a `String`:
2053+
///
2054+
/// ```
2055+
/// let a = "hello world! ";
2056+
/// let b = '👋';
2057+
/// let c = a.to_string() + b;
2058+
/// ```
2059+
#[stable(feature = "add_string_and_char", since = "1.41.0")]
2060+
impl Add<char> for String {
2061+
type Output = String;
2062+
2063+
#[inline]
2064+
fn add(mut self, other: char) -> String {
2065+
self.push(other);
2066+
self
2067+
}
2068+
}
2069+
19852070
/// Implements the `+=` operator for appending to a `String`.
19862071
///
19872072
/// This has the same behavior as the [`push_str`][String::push_str] method.
@@ -1993,6 +2078,28 @@ impl AddAssign<&str> for String {
19932078
}
19942079
}
19952080

2081+
/// Implements the `+=` operator for appending to a `String`.
2082+
///
2083+
/// This has the same behavior as the [`push_str`][String::push_str] method.
2084+
#[stable(feature = "string_add_assign_string", since = "1.41.0")]
2085+
impl AddAssign<&String> for String {
2086+
#[inline]
2087+
fn add_assign(&mut self, other: &String) {
2088+
self.push_str(other);
2089+
}
2090+
}
2091+
2092+
/// Implements the `+=` operator for appending a `char` to a `String`.
2093+
///
2094+
/// This has the same behavior as the [`push`][String::push] method.
2095+
#[stable(feature = "string_add_assign_char", since = "1.41.0")]
2096+
impl AddAssign<char> for String {
2097+
#[inline]
2098+
fn add_assign(&mut self, other: char) {
2099+
self.push(other);
2100+
}
2101+
}
2102+
19962103
#[stable(feature = "rust1", since = "1.0.0")]
19972104
impl ops::Index<ops::Range<usize>> for String {
19982105
type Output = str;

src/librustc_parse/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ impl<'a> Parser<'a> {
11101110
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix })
11111111
= next_token.kind {
11121112
if self.token.span.hi() == next_token.span.lo() {
1113-
let s = String::from("0.") + &symbol.as_str();
1113+
let s = String::from("0.") + &*symbol.as_str();
11141114
let kind = TokenKind::lit(token::Float, Symbol::intern(&s), suffix);
11151115
return Some(Token::new(kind, self.token.span.to(next_token.span)));
11161116
}

src/tools/clippy

0 commit comments

Comments
 (0)