#container #len #length #is-empty #is-not-empty

have_len

Is the container empty ? (is_empty() and is_not_empty())

1 unstable release

0.1.0 Oct 29, 2024

#1361 in Rust patterns

MIT/Apache

6KB

Define is_empty(&self) and is_not_empty(&self) for container that implement HaveLen, such as vector, array, string...

use have_len::*;

assert_eq!([1, 2, 3].is_empty(), false);
assert_eq!([1, 2, 3].is_not_empty(), true);

let empty_array : [i32; 0] = []; 
assert_eq!(empty_array.is_empty(), true);
assert_eq!(empty_array.is_not_empty(), false);


assert_eq!("".is_empty(), true);
assert_eq!("hello".is_empty(), false);

assert_eq!("".is_not_empty(), false);
assert_eq!("hello".is_not_empty(), true);

assert_eq!("".is_empty(), !("".is_not_empty()));
pub trait HaveLen
{
    fn len(&self) -> usize;

    fn is_empty(&self) -> bool { self.len() == 0 }
    fn is_not_empty(&self) -> bool { !self.is_empty() }
}

lib.rs:

Define is_empty(&self) and is_not_empty(&self) for container that implement HaveLen, such as vector, array, string...

use have_len::*;

assert_eq!([1, 2, 3].is_empty(), false);
assert_eq!([1, 2, 3].is_not_empty(), true);

let empty_array : [i32; 0] = []; 
assert_eq!(empty_array.is_empty(), true);
assert_eq!(empty_array.is_not_empty(), false);

assert_eq!("".is_empty(), true);
assert_eq!("hello".is_empty(), false);

assert_eq!("".is_not_empty(), false);
assert_eq!("hello".is_not_empty(), true);

assert_eq!("".is_empty(), !("".is_not_empty()));

No runtime deps