Skip to content

Instantly share code, notes, and snippets.

@Mr-Byte
Last active August 13, 2017 18:00
Show Gist options
  • Save Mr-Byte/6015478 to your computer and use it in GitHub Desktop.
Save Mr-Byte/6015478 to your computer and use it in GitHub Desktop.
Proof of concept macro to implement LINQ syntax in rust.
macro_rules! query(
(from $v:ident in $c:ident $(where $mw:expr)* select $ms:expr) =>
($c.filter_mapped(|&$v| if(true $(&& $mw)*) { Some($ms) } else { None }))
)
fn main()
{
let nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let result1 = query!(from x in nums select x * 2);
let result2 = query!(from x in nums
where x%2 == 0
select x * 2);
let result3 = query!(from x in nums
where x%2 == 0
where x%4 == 0
select x * 2);
for result1.iter().advance |&num| {
println(fmt!("%d", num));
}
println("----");
for result2.iter().advance |&num| {
println(fmt!("%d", num));
}
println("----");
for result3.iter().advance |&num| {
println(fmt!("%d", num));
}
println("----");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment