Skip to content

Remove incorrect note from string_add_assign docs #3076

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

Merged
merged 1 commit into from
Aug 29, 2018

Conversation

mbrubeck
Copy link
Contributor

The docs claim that String::push_str is better than String::add because 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

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
@flip1995 flip1995 closed this Aug 25, 2018
@flip1995 flip1995 reopened this Aug 25, 2018
@phansch phansch merged commit b87ab5c into rust-lang:master Aug 29, 2018
@phansch
Copy link
Member

phansch commented Aug 29, 2018

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants