konst/iter/collect_const.rs
1/**
2Collects an iterator constant into an array
3
4This macro requires the `"rust_1_56"` feature.
5
6# Iterator methods
7
8This macro supports emulating iterator methods by expanding to equivalent code.
9
10The supported iterator methods are documented in the [`iterator_dsl`] module,
11because they are also supported by other `konst::iter` macros.
12
13# Syntax
14
15The syntax of this macro is:
16
17```text
18collect_const!(
19 $Item:ty => $into_iterator:expr
20 $(, $iterator_method:ident ($($method_args:tt)*) )*
21 $(,)?
22)
23```
24Where `$Item` is the type of the elements that'll be collected into an array.
25
26Where `$into_iterator` is any type that can be converted into a const iterator, with
27[`konst::iter::into_iter`](crate::iter::into_iter).
28
29Where `$iterator_method` is any of the supported methods described in
30the [`iterator_dsl`] module.
31
32# Examples
33
34### Iterating over a range
35
36```rust
37use konst::iter;
38
39const ARR: [usize; 8] = iter::collect_const!(usize =>
40 10..,
41 filter(|n| *n % 2 == 0),
42 skip(5),
43 take(8),
44);
45
46assert_eq!(ARR, [20, 22, 24, 26, 28, 30, 32, 34]);
47```
48
49### Iterating over an array
50
51```rust
52use konst::iter;
53
54const ARR: [u8; 6] = iter::collect_const!(u8 =>
55 // the `&` is required here,
56 // because by-value iteration over arrays is not supported.
57 &[10, 20, 30],
58 flat_map(|&n| {
59 // To allow returning references to arrays, the macro extends
60 // the lifetime of borrows to temporaries in return position.
61 // The lifetime of the array is extended to the entire iterator chain.
62 &[n - 1, n + 1]
63 }),
64 copied()
65);
66
67assert_eq!(ARR, [9, 11, 19, 21, 29, 31]);
68```
69
70
71
72[`iterator_dsl`]: crate::iter::iterator_dsl
73*/
74#[cfg(feature = "rust_1_56")]
75#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_56")))]
76pub use konst_macro_rules::iter_collect_const as collect_const;