Trait len_trait::len::Len [] [src]

pub trait Len: Empty {
    fn len(&self) -> usize;
}

A trait for describing the length of a collection.

The amount of data stored in a collection, i.e. the amount of space it requires in memory, is directly proportional to its length. For this reason, str and other types measure their lengths in code values (e.g. u8), not code points (e.g. char).

Obtaining the length of the collection must take a constant amount of time and space.

Required Methods

Returns the length of the collection.

Examples

use len_trait::Len;

fn print_len<C: ?Sized + Len>(collection: &C) {
    println!("{} units long", collection.len());
}

print_len("中文");         // 6 units long
print_len(&[1, 2, 3][..]); // 3 units long

Implementors