std-method

String::clear

Remove all contents while retaining allocated capacity for later reuse. Expected stdout (not executed): 0 true Common mistake: Assuming clear releases the allocation.

Formal Syntax & Structure

rust syntaxrust
fn clear(&mut self)

Working Example

Example Snippetrust
fn main() {    let mut line = String::from("draft");    let before = line.capacity();    line.clear();    println!("{} {}", line.len(), line.capacity() == before);}
Rust Reference