std-method

String::with_capacity

Request room for at least the specified number of bytes, not Unicode scalar values. The length remains zero. Expected stdout (not executed): 0 true Common mistake: Assuming preallocated bytes are initialized text.

Formal Syntax & Structure

rust syntaxrust
fn with_capacity(capacity: usize) -> String

Working Example

Example Snippetrust
fn main() {    let text = String::with_capacity(24);    println!("{} {}", text.len(), text.capacity() >= 24);}
Rust Reference