Trait len_trait::len::LenMut
[−]
[src]
pub trait LenMut: Default + Clear { fn truncate(&mut self, len: usize); fn split_off(&mut self, index: usize) -> Self; }
A trait for modifying the length of a collection.
These methods must take at most a linear amount of time and space with respect to the number of elements which are moved or dropped.
Required Methods
fn truncate(&mut self, len: usize)
Truncates the collection to be no greater than len
long, dropping elements as needed.
If the collection is less than len
long, do nothing.
Panics
Panics if len
is not valid according to the collection. For example, the implementation
for String
will panic if len
does not lie on a character boundary.
Examples
use len_trait::LenMut; fn check_truncate<C: LenMut>(mut collection: C) { let old_len = collection.len(); collection.truncate(5); if old_len >= 5 { assert_eq!(collection.len(), 5); } else { assert_eq!(collection.len(), old_len); } } check_truncate("Hello, world!".to_string()); check_truncate(vec![1, 2, 3]);
fn split_off(&mut self, index: usize) -> Self
Splits off the collection at the given index, returning the data past the index.
Panics
Panics if index > len
.
Examples
use len_trait::LenMut; fn check_split_off<C: LenMut>(mut collection: C) { let old_len = collection.len(); let split = collection.split_off(5); assert_eq!(collection.len(), 5); assert_eq!(split.len(), old_len - 5); } check_split_off("Hello, world!".to_string()); check_split_off(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]);