1 unstable release
new 0.1.0 | Oct 29, 2024 |
---|
#1007 in Rust patterns
117 downloads per month
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()));