std-method

Vec::clear

Remove and drop every element while keeping the allocation for future use. The length becomes zero. Useful when a temporary buffer is reused. Expected stdout (not executed): 0 true Common mistake: Expecting clear to deallocate or securely erase all allocated bytes.

Formal Syntax & Structure

rust syntaxrust
pub fn clear(&mut self)

Working Example

Example Snippetrust
fn main() {    let mut buffer = vec![1, 2, 3];    let before = buffer.capacity();    buffer.clear();    println!("{} {}", buffer.len(), buffer.capacity() == before);}
Rust Reference