]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/format_push_string.txt
Auto merge of #101307 - jyn514:simplify-storage, r=cjgillot
[rust.git] / src / tools / clippy / src / docs / format_push_string.txt
1 ### What it does
2 Detects cases where the result of a `format!` call is
3 appended to an existing `String`.
4
5 ### Why is this bad?
6 Introduces an extra, avoidable heap allocation.
7
8 ### Known problems
9 `format!` returns a `String` but `write!` returns a `Result`.
10 Thus you are forced to ignore the `Err` variant to achieve the same API.
11
12 While using `write!` in the suggested way should never fail, this isn't necessarily clear to the programmer.
13
14 ### Example
15 ```
16 let mut s = String::new();
17 s += &format!("0x{:X}", 1024);
18 s.push_str(&format!("0x{:X}", 1024));
19 ```
20 Use instead:
21 ```
22 use std::fmt::Write as _; // import without risk of name clashing
23
24 let mut s = String::new();
25 let _ = write!(s, "0x{:X}", 1024);
26 ```