Trait len_trait::len::Clear [] [src]

pub trait Clear: Len {
    fn clear(&mut self);
}

A trait for clearing collections.

A collection is cleared by dropping all of its data. After clear is called, the collection is guaranteed to be empty.

Clearing a collection must take at most a linear amount of time and space.

Required Methods

Clears the collection, dropping its elements and returning its length to zero.

Examples

use len_trait::Clear;

struct Value(u32);
impl Drop for Value {
    fn drop(&mut self) {
        println!("Dropped a {}", self.0)
    }
}

fn check_clear<C: Clear>(mut collection: C) {
    collection.clear();
    assert!(collection.is_empty());
}

check_clear(vec![Value(12)]); // Prints "Dropped a 12"
check_clear("Hello, world!".to_string());

Implementors