el-macro

Crates.ioel-macro
lib.rsel-macro
version0.3.1
created_at2025-08-21 10:37:20.681112+00
updated_at2025-09-03 06:54:35.614622+00
descriptiona dumb macro collection for anti-idiomatic rust programming
homepage
repositoryhttps://github.com/dmitry-glvch/el-macro
max_upload_size
id1804565
size16,396
(dmitry-glvch)

documentation

README

el-macro

a dumb macro collection for anti-idiomatic rust programming

Basic usage

More comprehensive usage examples can be found on docs.rs.

bind!

Binds to the unwrapped value or evaluates the execution flow control expression. An optional error handler can be used, and support for custom types can be implemented.

bind!(x = Some(42), or return);
assert_eq!(x, 42);

bind!(mut x = Some(42), or return);
x += 3;
assert_eq!(x, 45);

let x = Some(42);
// shorthand for bind!(x = x, or return)
bind!(x, or return);
assert_eq!(x, 42);

let handle_error = |err: &str| eprintln!("{err}!");
// prints 'error!' and returns
bind!(x = None::<i32>.ok_or("error"), or handle_error, return);
unreachable!();

if_matches!

Maps pattern-bound variables to Some if the provided expression matches the pattern.

let a = Some(41);
let b = Some(43);
let avg = |x: i32, y: i32| (x + y) / 2;

let x = if_matches!((a, b), (Some(x), Some(y)) => avg(x, y));
assert!(x.is_some_and(|val| val == 42));

let x = if_matches!((a, None::<u8>), (Some(x), Some(_)) => a);
assert!(x.is_none());

Syntax similar to match guard is supported:

let vol = Some(100);
let bins = Some(0);
let per_bin = if_matches!((vol, bins), (Some(v), Some(b)) if b != 0 => v / b);
assert!(per_bin.is_none());

License

This project is licensed under the MIT License - see the LICENSE file for details.

Commit count: 16

cargo fmt