use bitflags::bitflags;

bitflags! {
    pub struct FilePermissions: u32 {
        const READ = 0b00000001;
        const WRITE = 0b00000010;
        const EXECUTE = 0b00000100;
    }
}

fn main() {
    let mut perm = FilePermissions::READ | FilePermissions::WRITE;

    // 检查是否包含某个标记
    if perm.contains(FilePermissions::READ) {
        println!("Read permission is set.");
    }

    // 添加一个标记
    perm.insert(FilePermissions::EXECUTE);
    if perm.contains(FilePermissions::EXECUTE) {
        println!("Execute permission is set.");
    }

    // 移除一个标记
    perm.remove(FilePermissions::WRITE);
    if !perm.contains(FilePermissions::WRITE) {
        println!("Write permission is not set.");
    }

    // 打印当前所有标记
    println!("Current permissions: {:?}", perm);
}

putao
5 声望0 粉丝

推动世界向前发展,改善民生。