Convert tokens into strings
The strinify!()
macro will convert a token into a string that be inserted, for example, into a hash.
use std::collections::HashMap;
#[derive(Debug)]
struct Foo {
x:isize,
y:isize
}
macro_rules! expand {
( $container:ident, $target:ident, $( $id:ident ),* ) => {
{
$(
$container.insert(stringify!($id), &$target.$id);
)*
}
};
}
fn main() {
let foo = Foo { x: 0, y: 1 };
let mut items:HashMap<&str, &isize> = HashMap::new();
expand!(items, foo, x, y);
println!("{:?}", items);
}