Fundamentals 3 min read

How to Define and Manipulate Bit Flags in Rust with the bitflags Crate

This guide shows how to add the bitflags crate to a Rust project, define a Permissions flag struct using the bitflags! macro, and perform common operations such as checking, inserting, and removing flags, while explaining the automatically derived traits and useful helper methods.

BirdNest Tech Talk
BirdNest Tech Talk
BirdNest Tech Talk
How to Define and Manipulate Bit Flags in Rust with the bitflags Crate

Problem

You need to define and manipulate a set of bit flags in a Rust program, where each flag represents an independent boolean option.

Solution

Use the bitflags crate. Add bitflags = "2.6.0" to Cargo.toml, then invoke the bitflags! macro to declare a flag struct.

[dependencies]
bitflags = "2.6.0"

Define the flags:

use bitflags::bitflags;

bitflags! {
    struct Permissions: u32 {
        const READ    = 0b001;
        const WRITE   = 0b010;
        const EXECUTE = 0b100;
    }
}

Operate on them:

fn main() {
    let mut perms = Permissions::READ | Permissions::WRITE;

    assert!(perms.contains(Permissions::READ));
    assert!(!perms.contains(Permissions::EXECUTE));

    perms.insert(Permissions::EXECUTE);
    assert!(perms.contains(Permissions::EXECUTE));

    perms.remove(Permissions::WRITE);
    assert!(!perms.contains(Permissions::WRITE));
}

Discussion

The bitflags crate provides a convenient, type‑safe way to define and work with bit flags, useful in systems programming, file‑permission handling, and configuration options.

Structures generated by the bitflags! macro automatically implement traits such as Copy, Clone, PartialEq, Eq, PartialOrd, Ord, and Hash, making them easy to use in many contexts.

Common methods include: empty(): creates an empty flag set. all(): creates a set containing all flags. bits(): returns the underlying integer representation. from_bits(): creates a flag set from an integer. intersects(): checks for common flags.

Using bitflags makes code clearer and more type‑safe compared with raw integers and manual bitwise operations, reducing the chance of errors. Remember to assign each flag a distinct power‑of‑two value (1, 2, 4, 8, …) when defining them.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

RustbitwiseType SafetySystems Programmingflagsbitflags
BirdNest Tech Talk
Written by

BirdNest Tech Talk

Author of the rpcx microservice framework, original book author, and chair of Baidu's Go CMC committee.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.