1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#![feature(core)] /// Generate a new iterable witn a list comprehension. This macro tries to follow the syntax of /// Python's list comprehensions. This is a very flexable macro that allows the generation of any /// iterable that implements `std::iter::FromIterator`. The resulting type will be determined by /// the type of the variable that you are attempting to assign to. You can create a `Vec`: /// /// ```ignore /// let x: Vec<i32> = gen![i*30 => i in [1, 2, 3, 4, 5]]; /// ``` /// /// You can generate a `HashSet`: /// /// ```ignore /// let x: HashSet<i32> = gen![i*30 => i in [1, 2, 3, 4, 5]]; /// ``` /// /// You can even use conditionals to generate stuff: /// /// ```ignore /// let x: HashSet<i32> = gen![i => i in [1, 2, 3, 4, 5], x % 2 == 0]; /// assert_eq!(x, vec![2, 4]); /// ``` /// /// Comparisson to Python's list comprehension /// === /// /// Python /// --- /// ```python /// x = [i*4 for i in range(1, 5)] /// ``` /// /// Rust with gen! macro /// --- /// ```ignore /// let x: Vec<i32> = gen!(x*4 => x in [1, 2, 3, 4]); /// ``` #[macro_export] #[macro_use] macro_rules! gen { [$e:expr => $variable:ident in $iterable:expr] => ( $iterable.iter().cloned().map(|$variable| $e).collect() ); [$e:expr => $variable:ident in $iterable:expr, $condition:expr] => ( $iterable.iter().cloned().filter(|$variable| $condition).map(|$variable| $e).collect() ); }