Crates.io | anon |
lib.rs | anon |
version | 0.1.0 |
source | src |
created_at | 2016-08-20 06:57:33.955158 |
updated_at | 2016-08-20 06:57:33.955158 |
description | Anonymous struct macro. |
homepage | https://github.com/ktakeda/anon |
repository | https://github.com/ktakeda/anon |
max_upload_size | |
id | 6035 |
size | 2,167 |
anon!{} is a Rust macro which create anonymous structs, like C# anonymous types.
Add this to your Cargo.toml
:
[dependencies]
anon = { git = "https://github.com/ktakeda/anon" }
and this to your crate root:
#[macro_use(anon)]
extern crate anon;
#[macro_use(anon)]
extern crate anon;
#[allow(non_camel_case_types)]
fn main() {
let a = anon! { a: 10, b: 20 };
assert_eq!(10, a.a);
assert_eq!(20, a.b);
}
Anon can be used in closure:
#[macro_use(anon)]
extern crate anon;
#[allow(non_camel_case_types)]
fn main() {
let y: Vec<_> = vec![1,2].into_iter().map(|x| {
anon! { a: x, b: x*x } // create anonymous struct
}).collect();
assert_eq!(1, y[0].a);
assert_eq!(2, y[1].a);
assert_eq!(1, y[0].b);
assert_eq!(4, y[1].b);
}
MIT.