#pattern #let #pattern-match #match #chain #if

iflet

Provides a macro to chain multiple patterns in an if let rather than nesting them

1 unstable release

Uses old Rust 2015

0.1.0 Jun 4, 2017

#9 in #if


Used in corollary

Apache-2.0

9KB
253 lines

crates.io Build Status

iflet - a Rust macro to avoid nested if let

if let is an extremely nice concept in Rust. You can use it in place of a pattern match for readability, like in this extremely contrived example below:

fn div(num: i32, denom: i32) -> Option<i32> {
    if denom == 0 {
        return Some(num / denom);
    
    
    None
}

fn main() {
    if let Some(x) = div(6, 2) {
        assert_eq!(x, 3);
    }
}

However, you can't use if let to match multiple clauses or with additional if guards (like in match patterns). iflet provides a macro that lets you do just that:

#[macro_use]
extern crate iflet;
#[macro_use]
extern crate serde_json;

use serde_json::Value::{Object, Array};

fn main() {
    let value = json!({
        "numbers": [ 1, 2, 4, 9, 16, 25 ]
    });

    if_chain!([let Object(ref map) => value,
               let Some(&Array(ref vec)) if !vec.is_empty() => map.get("numbers")] {
        println!("there are {} numbers stored in the object", vec.len());
    } else {
        println!("there are no numbers stored in the object");
    });
}

No runtime deps