1 unstable release
Uses new Rust 2024
0.1.0 | Mar 22, 2025 |
---|
#1047 in Rust patterns
132 downloads per month
23KB
480 lines
implicit-fn.rs
A macro that adds support for implicit closures to Rust.
This provides a concise alternative to regular closure syntax for when each parameter is used at most once and is not deeply nested.
This feature has been suggested before, but this macro mostly exists for fun.
Examples
Using implicit closures to concisely sum a list:
#[implicit_fn]
fn main() {
let n = [1, 2, 3].into_iter().fold(0, _ + _);
assert_eq!(n, 6);
}
Copying all the elements of an array:
#[implicit_fn]
fn main() {
let array: [&u32; 3] = [&1, &2, &3];
let array: [u32; 3] = array.map(*_);
assert_eq!(array, [1, 2, 3]);
}
Running a fallible function in an iterator:
#[implicit_fn]
fn main() -> Result<(), Box<dyn Error>> {
let names = fs::read_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/src"))?
.map(_?.file_name().into_string().map_err(|_| "file not UTF-8")?)
.collect::<Result<Vec<_>, Box<dyn Error>>>()?;
assert_eq!(names, ["lib.rs"]);
Ok(())
}
Running a match on an array of options:
#[implicit_fn]
fn main() {
let options = [Some(16), None, Some(2)];
let numbers = options.map(match _ {
Some(x) => x + 1,
None => 0,
});
assert_eq!(numbers, [17, 0, 3]);
}
Printing the elements of an iterator:
#[implicit_fn]
fn main() {
[1, 2, 3].into_iter().for_each(println!("{}", _));
}
License
MIT.
Dependencies
~180–600KB
~14K SLoC