#[macro_export]
macro_rules! capture {
($( $v:expr ),*) => {
$(let _ = &$v;)*
};
}
#[cfg(test)]
mod tests {
use super::*;
struct TestStruct {
a: String,
b: String,
}
impl TestStruct {
fn new() -> Self {
Self {
a: String::from("a"),
b: String::from("b"),
}
}
}
#[test]
fn single() {
let a = TestStruct::new();
let result = || {
capture!(a);
format!("Value: {}", a.a)
};
assert_eq!(result(), "Value: a");
}
#[test]
fn multiple() {
let a = TestStruct::new();
let b = TestStruct::new();
let result = || {
capture!(a, b);
format!("Value: {} {}", a.a, b.b)
};
assert_eq!(result(), "Value: a b");
}
}