6 releases

0.6.2 Nov 16, 2019
0.6.1 Oct 24, 2019
0.5.2 May 2, 2019
0.5.1 Apr 2, 2019
0.5.0 Jan 29, 2019

#32 in Rust patterns

Download history 235/week @ 2019-12-02 690/week @ 2019-12-09 912/week @ 2019-12-16 745/week @ 2019-12-23 798/week @ 2019-12-30 1290/week @ 2020-01-06 1120/week @ 2020-01-13 1603/week @ 2020-01-20 1658/week @ 2020-01-27 1468/week @ 2020-02-03 1439/week @ 2020-02-10 2339/week @ 2020-02-17 2028/week @ 2020-02-24 2160/week @ 2020-03-02 2571/week @ 2020-03-09 2009/week @ 2020-03-16

1,485 downloads per month
Used in 29 crates (10 directly)


442 lines

LICENSE LICENSE Documentation Crates.io Version


enumflags2 defines a BitFlags<T> type, which is a Set<T> for enums without associated data.


In your Cargo.toml:

enumflags2 = "^0.6"


  • Uses enums to represent individual flags—a set of flags is a separate type from a single flag.
  • Detects incorrect BitFlags at compile time.
  • Has a similar API compared to the popular bitflags crate.
  • Does not expose the generated types explicity. The user interacts exclusively with struct BitFlags<Enum>;.
  • The debug formatter prints the binary flag value as well as the flag enums: BitFlags(0b1111, [A, B, C, D]).
  • Optional support for serialization with the serde feature flag.


use enumflags2::BitFlags;

#[derive(BitFlags, Copy, Clone, Debug, PartialEq)]
enum Test {
    A = 0b0001,
    B = 0b0010,
    C = 0b0100,
    D = 0b1000,

let a_b = Test::A | Test::B; // BitFlags<Test>
let a_c = Test::A | Test::C;
let b_c_d = Test::C | Test::B | Test::D;

// BitFlags<Test>(0b11, [A, B])
println!("{:?}", a_b);

// BitFlags<Test>(0b1, [A])
println!("{:?}", a_b & a_c);

// Iterate over the flags like a normal set!
assert_eq!(a_b.iter().collect::<Vec<_>>(), &[Test::A, Test::B]);

assert!(b_c_d.contains(Test::B | Test::C));

assert!(!(a_b.intersects(Test::C | Test::D)));


By default, the BitFlags are usize-sized. If you want them to be smaller, specify a repr on your enum as in the example above.

Migrating from 0.5

The minimum rustc version has been bumped to 1.34.0, because of syn 1.0. The version policy from now on will be "what's available on Debian stable", because Debian is famously slow with new software versions.

You should no longer depend on enumflags2_derive directly. Use the reexport from the enumflags2 crate. semver guarantees will be violated if you depend on the derive crate directly.

The derive macro has been renamed to BitFlags, to make it clearer what the derive does.

The nostd feature flag has been removed. The crate now only depends on libcore by default. Enable the std flag to get an implementation of std::error::Error on error types.

Flags more than one bit set have been found to have inconsistent semantics. They are now rejected at compile-time. The same applies to flags without any bit set. If you were relying on this in your code, please open an issue and explain your usecase.

BitFlags::from_bits returns a Result instead of an Option. This might necessitate some minor changes in your code.

BitFlags::not has been removed. Use the ! operator instead.


~19K SLoC