A string buffer stores a growable string’s character data in heap memory alongside its current length and capacity. Mutating the buffer avoids copying the entire string until new data exceeds its capacity. section 2.4.1
The buffer’s two size measures determine when allocation is needed:
- Length counts the valid elements currently stored. section 2.4.1
- Capacity counts how many elements fit before growth requires reallocation. section 2.4.1
- When capacity is exceeded, the buffer obtains larger storage and copies its data; growth strategy is not guaranteed. section 2.4.1
Building ten million dots
A mutable Rust String can receive one dot per loop iteration. String::with_capacity(10_000_000) reserves space in advance, reducing allocation opportunities while preserving the resulting string.
