Intermediate Rust
Q98 / 100

What is the difference between String::new() and String::with_capacity(n)?

Correct! Well done.

Incorrect.

The correct answer is B) String::new() allocates no heap memory initially; with_capacity(n) pre-allocates n bytes, avoiding reallocations when building strings incrementally

B

Correct Answer

String::new() allocates no heap memory initially; with_capacity(n) pre-allocates n bytes, avoiding reallocations when building strings incrementally

Explanation

Pre-allocating with String::with_capacity(100) avoids multiple reallocations when pushing chars or appending substrings in a loop.

Progress
98/100