rust_utils/
iter.rs

1#[cfg(feature = "alloc")]
2use alloc::vec::Vec;
3
4pub fn map_collect<C, T, I, F>(iterable: I, f: F) -> C
5where
6    I: IntoIterator,
7    F: FnMut(I::Item) -> T,
8    C: FromIterator<T>,
9{
10    iterable.into_iter().map(f).collect()
11}
12
13#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
14#[cfg(feature = "alloc")]
15pub fn map_collect_vec<T, I, F>(iterable: I, f: F) -> Vec<T>
16where
17    I: IntoIterator,
18    F: FnMut(I::Item) -> T,
19{
20    map_collect(iterable, f)
21}
22
23pub fn filter_map_collect<C, T, I, F>(iterable: I, f: F) -> C
24where
25    I: IntoIterator,
26    F: FnMut(I::Item) -> Option<T>,
27    C: FromIterator<T>,
28{
29    iterable.into_iter().filter_map(f).collect()
30}
31
32#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
33#[cfg(feature = "alloc")]
34pub fn filter_map_collect_vec<T, I, F>(iterable: I, f: F) -> Vec<T>
35where
36    I: IntoIterator,
37    F: FnMut(I::Item) -> Option<T>,
38{
39    filter_map_collect(iterable, f)
40}