std-method

String::capacity

Inspect how many bytes the String can hold without growing its allocation. Capacity is not the number of characters. Expected stdout (not executed): 8 true Common mistake: Expecting capacity to equal scalar count.

Formal Syntax & Structure

rust syntaxrust
fn capacity(&self) -> usize

Working Example

Example Snippetrust
fn main() {    let mut text = String::with_capacity(16);    text.push_str("سلام");    println!("{} {}", text.len(), text.capacity() >= 16);}
Rust Reference