| Crates.io | iflet |
| lib.rs | iflet |
| version | 0.1.0 |
| created_at | 2017-06-04 22:40:06.77314+00 |
| updated_at | 2017-06-04 22:40:06.77314+00 |
| description | Provides a macro to chain multiple patterns in an `if let` rather than nesting them |
| homepage | |
| repository | https://github.com/saghm/iflet |
| max_upload_size | |
| id | 17754 |
| size | 20,795 |
if letif 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");
});
}