std-method

Vec::truncate

Drop elements beyond the requested length. If the requested length is at least the current length, do nothing. Capacity is unchanged. Expected stdout (not executed): [1, 2] Common mistake: Using truncate to add values or to force memory release.

Formal Syntax & Structure

rust syntaxrust
pub fn truncate(&mut self, len: usize)

Working Example

Example Snippetrust
fn main() {    let mut log = vec![1, 2, 3, 4];    log.truncate(2);    log.truncate(10);    println!("{log:?}");}
Rust Reference