parity 0.1.0

Provides is_even and is_odd methods for primitive numeric types
Documentation
  • Coverage
  • 100%
    4 out of 4 items documented0 out of 3 items with examples
  • Size
  • Source code size: 6.46 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.44 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • richardscollin/parity
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • richardscollin

Parity

Provides an interface to check the evenness or oddness of a value.

Normally, you would just use the modulus operator to check if a number is even or odd. Some languages like ruby allow calling a method name even? or odd?. This crate provides is_even and is_odd methods on all primitive numeric types. Now you can write code like this:

// importing the trait is required to use the method on primitives
use parity::Parity;

for i in 0..100 {
  if i.is_even() {
    println("{i}");
  }
}

// or even like this
let x : Vec<_> = (0..100).map(u32::is_even).collect();

The implementation of the trait is very simple:

pub trait Parity {
    fn is_even(&self) -> bool;
    fn is_odd(&self) -> bool;
}
impl Parity for i32 {
    #[inline]
    fn is_even(&self) -> bool {
        *self & 1 == 0
    }
    #[inline]
    fn is_odd(&self) -> bool {
        *self & 1 != 0
    }
}
// implemented for all numeric primitive types
// ...

The inline attribute is used to allow optimization across crate boundaries. This means there is no extra cost associated with using these methods compared to directly using the modulus or bitwise and operator.