Crates.io | iota |
lib.rs | iota |
version | 0.2.3 |
source | src |
created_at | 2015-10-23 02:42:53.138898 |
updated_at | 2022-12-19 17:27:33.595981 |
description | Related constants: 1 << iota |
homepage | |
repository | https://github.com/dtolnay/iota |
max_upload_size | |
id | 3288 |
size | 23,156 |
The iota!
macro constructs a set of related constants.
[dependencies]
iota = "0.2"
use iota::iota;
iota! {
const A: u8 = 1 << iota;
, B
, C
, D
}
fn main() {
assert_eq!(A, 1);
assert_eq!(B, 2);
assert_eq!(C, 4);
assert_eq!(D, 8);
}
Within an iota!
block, the iota
variable is an untyped integer constant
whose value begins at 0 and increments by 1 for every constant declared in
the block.
use iota::iota;
iota! {
const A: u8 = 1 << iota;
, B
const C: i32 = -1; // iota is not used but still incremented
pub const D: u8 = iota * 2;
, E
, F
}
// `iota` begins again from 0 in this block
iota! {
const G: usize = 1 << (iota + 10);
, H
}
fn main() {
assert_eq!(A, 1 << 0);
assert_eq!(B, 1 << 1);
assert_eq!(C, -1);
assert_eq!(D, 3 * 2);
assert_eq!(E, 4 * 2);
assert_eq!(F, 5 * 2);
assert_eq!(G, 1 << (0 + 10));
assert_eq!(H, 1 << (1 + 10));
}