Skip to content

Commit 6a07036

Browse files
authored
Remove incorrect note from string_add_assign docs
The docs claim that `String::push_str` is better than `String::add` because `String::add` allocates a new string and drops the old one, but this is not true. In fact, `add` reuses the existing string and grows it only if its capacity is exceeded, exactly like `push_str`. Their performance is identical since `add` is just a wrapper for `push_str`: ``` fn add(mut self, other: &str) -> String { self.push_str(other); self } ``` https://github.com/rust-lang/rust/blob/35bf1ae25799a4e62131159f052e0a3cbd27c960/src/liballoc/string.rs#L1922-L1925
1 parent e9c3d3d commit 6a07036

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

clippy_lints/src/strings.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use crate::utils::{get_parent_expr, is_allowed, match_type, paths, span_lint, sp
99
/// `let`!).
1010
///
1111
/// **Why is this bad?** It's not really bad, but some people think that the
12-
/// `.push_str(_)` method is more readable. Also creates a new heap allocation and throws
13-
/// away the old one.
12+
/// `.push_str(_)` method is more readable.
1413
///
1514
/// **Known problems:** None.
1615
///

0 commit comments

Comments
 (0)