1个不稳定版本
0.1.0 | 2023年9月9日 |
---|
#104 in #dictionary
29KB
535 行
maparr
(基于数组的映射帮助) 📙
基于const数组构建静态Map
的Rust宏。
开始使用
首先定义你的映射,然后你可以根据需要使用它。
use maparr::maparr;
maparr!(
Continents;
ASIA,
AFRICA,
AMERICA_NORTH,
AMERICA_SOUTH,
ANTARCTICA,
EUROPE,
AUSTRALIA,
);
const CONTINENT_SQUARE_MILES: Continents<usize> = maparr!(
Continents;
ASIA = 17_212_000,
AFRICA = 11_608_000,
AMERICA_NORTH = 9_365_000,
AMERICA_SOUTH = 6_880_000,
ANTARCTICA = 5_100_000,
EUROPE = 3_837_000,
AUSTRALIA = 2_968_000,
);
fn main() {
for (sq_miles, continent) in CONTINENT_SQUARE_MILES.into_iter().zip(Continents::names()) {
println!("{continent:15} = {sq_miles:10} (sq mi)");
}
}
你可以在stdout
中期望得到以下输出。
ASIA = 17212000 (sq mi)
AFRICA = 11608000 (sq mi)
AMERICA_NORTH = 9365000 (sq mi)
AMERICA_SOUTH = 6880000 (sq mi)
ANTARCTICA = 5100000 (sq mi)
EUROPE = 3837000 (sq mi)
AUSTRALIA = 2968000 (sq mi)
你可以修改构建的映射(如果允许,即使在const
上下文中)。
use maparr::maparr;
maparr!(
Continents<usize>;
ASIA,
AFRICA,
AMERICA_NORTH,
AMERICA_SOUTH,
ANTARCTICA,
EUROPE,
AUSTRALIA,
);
fn main() {
let mut continents = maparr!(
Continents;
ASIA = 17_212_000,
AFRICA = 11_608_000,
AMERICA_NORTH = 9_365_000,
AMERICA_SOUTH = 6_880_000,
ANTARCTICA = 5_100_000,
EUROPE = 3_837_000,
AUSTRALIA = 2_968_000,
);
continents.set(Continents::ASIA, 17_212_001);
assert_eq!(continents[Continents::ASIA], 17_212_001);
continents = continents.map(|value| value * 2);
assert_eq!(continents[Continents::ASIA], 17_212_001 * 2);
}