For destructuring an expression into multiple constants.
The primary feature of this crate is the [multiconst
] macro,
which destructuring an expression into multiple constants.
For more examples you can look in the docs for multiconst
This example demonstrates destructuring an array (whose length is inferred) into multiple constants.
```rust use multiconst::multiconst;
asserteq!(A, 0b11); asserteq!(B, 0b111); asserteq!(C, 0b1111); asserteq!(D, 0b11111);
multiconst!{ pub const [A, B, C, D]: [u64; ] = mersennesfrom(2); }
/// Generates all mersenne numbers (binary numbers that are all 1
bits)
/// from start
amount of 1s up to start + N - 1
.
const fn mersennesfrom
```
This example demonstrates how structs that impl FieldType
can be destructured.
This example uses the FieldType
derive macro
(which requires the "derive" feature)
to make it possible to destructure struct fields without
annotating their types.
```rust use multiconst::{FieldType, multiconst};
asserteq!(MIN, 3); asserteq!(MAX, 21);
multiconst!{ const MinMax{min: MIN, max: MAX}: MinMax = min_max(&[21, 13, 3, 8, 5]); }
struct MinMax { min: u32, max: u32, }
const fn min_max(elems: &[u32]) -> MinMax { let mut min = u32::MAX; let mut max = 0;
multiconst::for_range!{i in 0..elems.len() =>
let elem = elems[i];
if elem < min { min = elem; }
if elem > max { max = elem; }
}
MinMax{min, max}
}
```
All these crate features are opt-in:
"derive"
: enables the FieldType
derive macro.multiconst
is #![no_std]
, it can be used anywhere Rust can be used.
multiconst
requires Rust 1.51.0, requiring crate features to use newer language features.