I have written a macro that implements Scala-like for comprehensions in Rust. It will turn this:
map_for!{
x <- 0..4;
y = 2*x;
z <- 0..1;
=> y+z
}
into this:
((0..4).map (move |x| { let y = 2 * x; (x, y) }))
.flat_map (move |params| {
let (x, y) = params;
(0..1).map (move |z| { y + z })
})
This works, but the compiler emits an "unused variable" warning because x
is not used inside the flat_map
. I can disable the warning by adding #[allow(unused_variables)]
before the let
statements in the macro, but then it removes all unused variable warnings so this:
map_for!{
x <- 0..4;
y = 2;
z <- 0..1;
=> y+z
}
will expand into:
((0..4).map (move |x| { let y = 2; (x, y) }))
.flat_map (move |params| {
#[allow(unused_variables)]
let (x, y) = params;
(0..1).map (move |z| { y + z })
})
and will not generate a warning either even though x
really is not used.
Is there a way to make it so that the first example will not generate a warning, but the second one will?
The full code of the macro with warnings is available, as is the full code with warnings suppressed.