Skip to content

Instantly share code, notes, and snippets.

@n1xx1
Created December 17, 2017 00:17
Show Gist options
  • Save n1xx1/e9c221a2fc6bafb8638c1dbd42b7c651 to your computer and use it in GitHub Desktop.
Save n1xx1/e9c221a2fc6bafb8638c1dbd42b7c651 to your computer and use it in GitHub Desktop.
macro_rules! linq {
(from ($from:expr) $fromident:ident select ($($select:expr), +) where ($where:expr)) => {{
let mut vec = Vec::new();
for $fromident in &($from) {
if $where {
vec.push(( $( $select ), * ));
}
}
vec
}};
}
struct V {
val: i32,
dval: i32,
sel: bool,
}
fn m_v(a: i32, b: bool) -> V {
V{val: a, dval: a*2, sel: b}
}
fn main() {
let v = vec![m_v(1, true), m_v(2, true), m_v(3, false), m_v(4, true), m_v(5, false), m_v(6, false)];
let v1 = linq!(from (v) m select (m.val, m.dval) where (m.sel == true));
for res in &v1 {
println!("{:?}", res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment