case_clause 0.1.3

case clause macro for rust
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented3 out of 3 items with examples
  • Size
  • Source code size: 9.87 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.16 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Guelakais

Cause Clause Macro

The purpose of the macro implemented here is to create an alternative to huge if else cascades.

Current State:

The Idea is based on haskell case clauses:

sign x |  x >  0        =   1
       |  x == 0        =   0
       |  x <  0        =  -1

This would normally look like this in Rust:

if x > 0 {
    1 
} else if x == 0 {
    0
} else if x < 0 {
    -1 
}

If you now want to display this using a match case, which is normally the environment in rust for pattern matching, it looks like this:

match x {
    if x > 0 => 1,
    if x == 0 => 0,
    if x < 0 => -1,
    _ => 0,
}

I found both solutions extremely clunky and therefore tiring to work with. That's why the macro from this crate works like this:

case!(
    x > 0 => 1,
    x == 0 => 0,
    x < 0  => -1,
    true => 0,
    )

To be fair, this is a first step towards creating a more elegant alternative to rust's match environment, which still works elegantly when processing boolean values.

Getting Started

If you're in a cargo project, you can add this to your Cargo.toml:

cargo add case_clause

Inside your rust project, you can call this macro like so:

import case_clause::case;

last words

Best regards, my fellow rustaceans! :)