5 releases

0.2.3 Dec 19, 2022
0.2.2 Jun 16, 2020
0.2.1 Dec 26, 2019
0.1.4 Mar 10, 2017
0.1.2 Oct 26, 2015

#5 in #constructs

Download history 28/week @ 2023-12-22 117/week @ 2023-12-29 323/week @ 2024-01-05 58/week @ 2024-01-12 25/week @ 2024-01-19 7/week @ 2024-01-26 6/week @ 2024-02-02 14/week @ 2024-02-09 29/week @ 2024-02-16 58/week @ 2024-02-23 65/week @ 2024-03-01 66/week @ 2024-03-08 50/week @ 2024-03-15 41/week @ 2024-03-22 85/week @ 2024-03-29 59/week @ 2024-04-05

247 downloads per month
Used in 3 crates

MIT/Apache

10KB
77 lines

1 << iota

github crates.io docs.rs build status

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));
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps