IsEmpty

Trait IsEmpty 

Source
pub trait IsEmpty {
    // Required method
    fn is_empty(&self) -> bool;
}
Expand description

A trait defining an is_empty method for various types.

This trait provides a generic way to determine if a value of a given type can be considered “empty”. The definition of “empty” is context-specific and is implemented for common Rust types like collections, strings, booleans, and numeric types.

§Implementations:

  • String and &str: Returns true if the string contains no characters.
  • Vec<T>: Returns true if the vector contains no elements.
  • bool: Returns true if the boolean value is false.
  • Numeric types (integers and floats): Returns true if the value is 0 or 0.0.
  • Option<T>: Returns true if the Option is None or if Some(value) and value is is_empty().

§Usage

This trait is particularly useful for filtering or compacting collections where the concept of “emptiness” applies to the elements.

Required Methods§

Source

fn is_empty(&self) -> bool

Checks if the value is considered empty.

The specific definition of “empty” depends on the type implementing this trait.

§Returns

true if the value is empty, false otherwise.

Implementations on Foreign Types§

Source§

impl IsEmpty for &str

Implements IsEmpty for string slices (&str).

A &str is considered empty if its length is zero.

§Performance

This implementation directly calls the &str::is_empty() method, which is an efficient O(1) operation.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for bool

Implements IsEmpty for boolean values (bool).

A bool is considered empty if its value is false. This aligns with the common interpretation of false as “nothing” or “non-existent” in a logical context.

§Performance

This is a direct comparison, an efficient O(1) operation.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for f32

Implements IsEmpty for floating-point type $float_ty. A float is considered empty if its value is 0.0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for f64

Implements IsEmpty for floating-point type $float_ty. A float is considered empty if its value is 0.0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for i8

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for i16

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for i32

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for i64

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for i128

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for isize

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for u8

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for u16

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for u32

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for u64

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for u128

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for usize

Implements IsEmpty for integer type $int_ty. An integer is considered empty if its value is 0.

Source§

fn is_empty(&self) -> bool

Source§

impl IsEmpty for String

Implements IsEmpty for String.

A String is considered empty if its length is zero.

§Performance

This implementation directly calls the String::is_empty() method, which is an efficient O(1) operation.

Source§

fn is_empty(&self) -> bool

Source§

impl<T> IsEmpty for Vec<T>

Implements IsEmpty for dynamic vectors (Vec<T>).

A Vec<T> is considered empty if it contains no elements.

§Type Parameters

  • T: The type of elements within the vector.

§Performance

This implementation directly calls the Vec::is_empty() method, which is an efficient O(1) operation.

Source§

fn is_empty(&self) -> bool

Source§

impl<T: IsEmpty> IsEmpty for Option<T>

Implements IsEmpty for Option<T>.

An Option<T> is considered empty if it is None or if it is Some(value) and the value itself is is_empty(). This provides a recursive check for emptiness.

§Type Parameters

  • T: The type contained within the Option, which must also implement IsEmpty.

§Performance

The performance depends on the is_empty() implementation of the inner type T.

Source§

fn is_empty(&self) -> bool

Implementors§