fallthrough 0.1.3

Pattern match with fallthrough, in the style of C switch
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 18.75 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 248.17 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 8s Average build duration of successful builds.
  • all releases: 7s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • Jules-Bertholet/fallthrough
    2 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Jules-Bertholet

fallthrough

Build Status API reference Crates.io License

This crate provides a fallthrough macro, which allows performing a pattern match with fallthrough through the arms, in the style of C switch.

use fallthrough::fallthrough;

fn fall(scrutinee: u32) -> u32 {
    let mut ret: u32 = 0;

    fallthrough!(scrutinee,
        val @ (0 | 63..) => ret = val + 7,
        'one: 1 => ret += 8,
        'two: 2 => ret += 9,
        'three: 3 if true => { ret += 10; break 'end },
        'four: 4 => ret = 42,
        'five: 5 => { ret += 1; break 'seven },
        'six: 6 => ret = 3,
        'seven: _ => ret *= 2,
        'end
    );
    ret
}

fn main() {
    assert_eq!(fall(0), 34);
    assert_eq!(fall(1), 27);
    assert_eq!(fall(2), 19);
    assert_eq!(fall(3), 10);
    assert_eq!(fall(4), 86);
    assert_eq!(fall(5), 2);
    assert_eq!(fall(6), 6);
    assert_eq!(fall(7), 0);
    assert_eq!(fall(64), 98);
}