#bitset #bitmap #bit-manipulation #bit #iterator #collection #bits #iterate

no-std bit_collection

A trait for iterating over the individual bits of a collection

6 releases

Uses old Rust 2015

0.2.3 Sep 5, 2017
0.2.2 Aug 25, 2017
0.1.1 Aug 8, 2017

#9 in #bit-manipulation

Download history 7/week @ 2022-11-30 18/week @ 2022-12-07 7/week @ 2022-12-14 15/week @ 2022-12-21 13/week @ 2022-12-28 2/week @ 2023-01-04 11/week @ 2023-01-11 17/week @ 2023-01-18 18/week @ 2023-01-25 20/week @ 2023-02-01 21/week @ 2023-02-08 32/week @ 2023-02-15 5/week @ 2023-02-22 10/week @ 2023-03-01 10/week @ 2023-03-08 22/week @ 2023-03-15

57 downloads per month
Used in zdex


251 lines


A Rust library that allows for iterating over the bits of a collection.

Build Status



This project is released under either:

at your choosing.


Iterate over a collection of bits.


This crate is available on crates.io and can be used by adding the following to your project's Cargo.toml:

bit_collection = "0.2.3"

and this to your crate root:

extern crate bit_collection;
# fn main() {}

#[bit] Attribute

The #[bit] attribute is composed of three parts, two of which are optional in some cases. The components can be provided in any order.


The type used to represent individual bits. This part is required.

#[bit(Type, ...)]


A mask indicating the valid bits of the collection. This should be a constant expression.

If not provided, the mask is assumed to have all bits set (i.e. !0).

BitCollection::FULL returns this value.

Attention: Please read the section on safety to ensure that this is used in a correct and safe manner.

#[bit(..., mask = "0b11", ...)]


The suffix for retrieving the inner integer value of the bit type. It expands to $value.$retr. Because of this, the provided retriever must be visible where the derive is located.

If not provided, the bit type is assumed to be an enum that can be casted to an integer.

#[bit(..., retr = "inner", ...)]


The iterator for a given BitCollection. If BitIter isn't imported as-is, this option allows for specifying its module path.

extern crate bit_collection as bc;

#[bit(..., iter = "bc::BitIter", ...)]


In computer chess, one popular way of representing the occupants of a board is through a Bitboard type. In this type, each individual bit is a square on a chess board.

# include!("../templates/imports.rs");
#[derive(Copy, Clone)]
pub struct Square(u8);

/// A set of sixty-four `Square`s.
#[bit(Square, mask = "!0", retr = "0")]
pub struct Bitboard(u64);

# fn main() {}

We can also represent castle rights this way.

# include!("../templates/imports.rs");
#[derive(Copy, Clone)]
pub enum CastleRight {

/// A set of `CastleRight`s.
#[bit(CastleRight, mask = "0b1111")]
pub struct CastleRights {
    bits: u8

fn iterate_over(rights: CastleRights) {
    for right in rights {
        match right {
            CastleRight::WhiteKingside  => { /* ... */ },
            CastleRight::BlackKingside  => { /* ... */ },
            CastleRight::WhiteQueenside => { /* ... */ },
            CastleRight::BlackQueenside => { /* ... */ },

# fn main() {}


This crate makes certain assumptions that, if unmet, may have unsafe and unexpected results.

The mask option for #[bit] must have the correct bits set. It must not have bits set that correspond to invalid instances of the bit type.

Similarly, the bit type must be defined such that corresponding bit patterns from mask provide legitimate values. Ask yourself, do 1 << item and its reversal (undo) operations, pop_{lsb,msb}, make sense in terms of the provided mask?


~38K SLoC