Crates.io | letr |
lib.rs | letr |
version | 0.2.1 |
source | src |
created_at | 2023-02-10 18:48:04.354414 |
updated_at | 2023-02-11 09:23:37.430381 |
description | The macro for the lazy. |
homepage | https://github.com/swarnimarun/letr |
repository | https://github.com/swarnimarun/letr |
max_upload_size | |
id | 782032 |
size | 8,709 |
letr
The lazy way out.
fn simple() {
enum Items {
RevItem(Option<i32>),
ForItem(Option<Option<Option<Option<i32>>>>),
}
fn foo(x: Items) -> i32 {
letr! {
Items::RevItem(ret) = x, else -1;
Some(ret) = ret, else -2;
};
ret
}
assert_eq!(foo(Items::RevItem(Some(0))), 0);
assert_eq!(foo(Items::ForItem(Some(Some(Some(Some(0)))))), -1);
assert_eq!(foo(Items::RevItem(None)), -2);
fn bar(x: Items) -> Option<i32> {
letr! {
Items::ForItem(ret) = x, else Some(-1);
Some(ret) = ret, else Some(-2);
Some(ret) = ret, else Some(-3);
Some(ret) = ret, else Some(-4);
ret = ret?; // returns none by default
};
Some(ret)
}
assert_eq!(bar(Items::ForItem(Some(Some(Some(Some(0)))))), Some(0));
assert_eq!(bar(Items::RevItem(Some(0))), Some(-1));
assert_eq!(bar(Items::ForItem(None)), Some(-2));
assert_eq!(bar(Items::ForItem(Some(None))), Some(-3));
assert_eq!(bar(Items::ForItem(Some(Some(None)))), Some(-4));
assert_eq!(bar(Items::ForItem(Some(Some(Some(None))))), None);
}
A simpler alternative to if_chain crate, but using let-else semantics. Generally works better with rust-analyzer.