#bitmask #flags

no-std bitflags

A macro to generate structures which behave like bitflags

52 releases (33 stable)

new 2.11.0 Feb 14, 2026
2.10.0 Oct 19, 2025
2.9.4 Sep 2, 2025
2.9.1 May 15, 2025
0.1.1 Feb 9, 2015

#1 in Rust patterns

Download history 11207437/week @ 2025-10-25 11287191/week @ 2025-11-01 10957874/week @ 2025-11-08 11240044/week @ 2025-11-15 10143578/week @ 2025-11-22 11379080/week @ 2025-11-29 12600119/week @ 2025-12-06 12508646/week @ 2025-12-13 7577003/week @ 2025-12-20 7004747/week @ 2025-12-27 12347658/week @ 2026-01-03 14235778/week @ 2026-01-10 13934146/week @ 2026-01-17 14882764/week @ 2026-01-24 15806751/week @ 2026-01-31 14259956/week @ 2026-02-07

61,221,305 downloads per month
Used in 111,123 crates (3,926 directly)

MIT/Apache

160KB
4K SLoC

bitflags

Rust Latest version Documentation License

bitflags generates flags enums with well-defined semantics and ergonomic end-user APIs.

You can use bitflags to:

  • provide more user-friendly bindings to C APIs where flags may or may not be fully known in advance.
  • generate efficient options types with string parsing and formatting support.

You can't use bitflags to:

  • guarantee only bits corresponding to defined flags will ever be set. bitflags allows access to the underlying bits type so arbitrary bits may be set.

  • define bitfields. bitflags only generates types where set bits denote the presence of some combination of flags.

  • Documentation

  • Specification

  • Release notes

Usage

Add this to your Cargo.toml:

[dependencies]
bitflags = "2.11.0"

and this to your source code:

use bitflags::bitflags;

Example

Generate a flags structure:

use bitflags::bitflags;

// The `bitflags!` macro generates `struct`s that manage a set of flags.
bitflags! {
    /// Represents a set of flags.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    struct Flags: u32 {
        /// The value `A`, at bit position `0`.
        const A = 0b00000001;
        /// The value `B`, at bit position `1`.
        const B = 0b00000010;
        /// The value `C`, at bit position `2`.
        const C = 0b00000100;

        /// The combination of `A`, `B`, and `C`.
        const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
    }
}

fn main() {
    let e1 = Flags::A | Flags::C;
    let e2 = Flags::B | Flags::C;
    assert_eq!((e1 | e2), Flags::ABC);   // union
    assert_eq!((e1 & e2), Flags::C);     // intersection
    assert_eq!((e1 - e2), Flags::A);     // set difference
    assert_eq!(!e2, Flags::A);           // set complement
}

Cargo features

The bitflags library defines a few Cargo features that you can opt-in to:

  • std: Implement the Error trait on error types used by bitflags.
  • serde: Support deriving serde traits on generated flags types.
  • arbitrary: Support deriving arbitrary traits on generated flags types.
  • bytemuck: Support deriving bytemuck traits on generated flags types.

Also see bitflags_derive for other flags-aware traits.

Rust Version Support

The minimum supported Rust version is documented in the Cargo.toml file. This may be bumped in minor releases as necessary.

Dependencies

~175KB