Crates.io | iota-rs |
lib.rs | iota-rs |
version | 0.1.2 |
source | src |
created_at | 2015-11-15 01:38:52.997258 |
updated_at | 2016-12-15 10:38:53.02315 |
description | A rust macro for other Go refuges who miss const blocks and iota. |
homepage | |
repository | https://github.com/Popog/iota-rs |
max_upload_size | |
id | 3422 |
size | 14,073 |
A rust macro for other Go refuges who miss const blocks and iota.
Go:
const (
Sunday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Partyday
numberOfDays // this constant is not exported
)
Rust:
#![feature(plugin)]
#![plugin(iota)]
consts!{
pub SUNDAY: i32 = iota!();
pub MONDAY;
pub TUESDAY;
pub WEDNESDAY;
pub THURSDAY;
pub FRIDAY;
pub PARTYDAY;
NUMBER_OF_DAYS; // this constant is not exported
}
Like in Go, you can do multiple constants on a single line, and use underscore to skip values.
Go:
const (
bit0, mask0 = 1 << iota, 1<<iota - 1 // bit0 == 1, mask0 == 0
bit1, mask1 // bit1 == 2, mask1 == 1
_, _ // skips iota == 2
Bit3, Mask3 // bit3 == 8, mask3 == 7
)
Rust:
#![feature(plugin)]
#![plugin(iota)]
consts!{
(BIT0, MASK0): (i32, i32) = (1 << iota!(), 1<<iota!() - 1); // bit0 == 1, mask0 == 0
(BIT1, MASK1); // bit1 == 2, mask1 == 1
(_, _); // skips iota == 2
(pub BIT3, pub MASK3); // bit3 == 8, mask3 == 7
}
Rust sans-plugin:
// bit0 == 1, mask0 == 0
const BIT0: i32 = 1 << 0;
const MASK0: i32 = 1<<0 - 1;
// bit1 == 2, mask1 == 1
const BIT1: i32 = 1 << 1;
const MASK1: i32 = 1<<1 - 1;
// bit3 == 8, mask3 == 7
pub const BIT3: i32 = 1 << 3;
pub const MASK3: i32 = 1<<1 - 3;
As you can see, compared to the base langauge, this plugin makes it easy to create a large number of similar numerical constants without a bunch of error-prone copy-pasting or having to define your own short-term use macros.
For those coming from Go, you may have noticed some differences: