Skip to content

Performance: Suggest String::new() over "".to_string() #2972

Closed
@phansch

Description

@phansch

From rust-lang/rust#52805 (comment). String::new() is faster than ""::to_string()

Benchmark:

#[bench]
fn bench_string_new(b: &mut Bencher) {
    b.iter(|| String::new());
}

#[bench]
fn bench_to_string(b: &mut Bencher) {
    b.iter(|| "".to_string());
}
running 2 tests
test tests::bench_string_new ... bench:           0 ns/iter (+/- 0)
test tests::bench_to_string  ... bench:           8 ns/iter (+/- 0)

Metadata

Metadata

Labels

A-lintArea: New lintsL-pedanticLint: Belongs in the pedantic lint groupgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions