| Crates.io | const-anonymous-functions |
| lib.rs | const-anonymous-functions |
| version | 1.1.0 |
| created_at | 2023-08-06 10:38:03.293558+00 |
| updated_at | 2023-08-07 12:36:33.879014+00 |
| description | Simple macro to create const anonymous functions |
| homepage | |
| repository | https://github.com/Odilf/const_anonymous_functions |
| max_upload_size | |
| id | 937069 |
| size | 3,440 |
Small crate that provides a macro to create anonymous functions that can be used in const contexts.
It doesn't provide any new functionality, only syntax sugar.
Add it to your project
cargo add const-anonymous-functions
use const_anonymous_functions::caf;
const RESULT: i32 = caf!(|a: i32, b: i32| -> i32 { a + b })(1, 2);
assert_eq!(RESULT, 1 + 2);
There are a couple caveats to using this crate:
|a: i32| a + 1 is not allowed (notice how it would be wrong either way since the return type here is (), so it doesn't return anything). |a: i32| -> i32 { a + 1 } is the correct way to write it.This macro is actually super basic. It just takes the closure syntax and transforms it into a const function. Then it returns the function.
So caf!(|a: i32, b: i32| -> i32 { a + b }) literally becomes:
{
const fn __annon_caf__(a: i32, b: i32) -> i32 {
a + b
}
__annon_caf__
}
# ;
That's it.