std-method

String::reserve

Ensure capacity for at least len plus additional bytes. The allocation strategy may provide more room. Expected stdout (not executed): 2 true Common mistake: Treating additional as the final total capacity.

Formal Syntax & Structure

rust syntaxrust
fn reserve(&mut self, additional: usize)

Working Example

Example Snippetrust
fn main() {    let mut label = String::from("ID");    label.reserve(20);    println!("{} {}", label.len(), label.capacity() >= 22);}
Rust Reference