-
Open the Cargo.toml file that was generated earlier for you.
- Under [dependencies], add the following lines:
bitflags = "1.0"
- If you want, you can go to bitflags' crates.io page (https://crates.io/crates/bitflags) to check for the newest version and use that one instead.
-
In the bin folder, create a file called bit_fields.rs.
-
Add the following code and run it with cargo run --bin bit_fields:
1 #[macro_use]
2 extern crate bitflags;
3
4 bitflags! {
5 struct Spices: u32 {
6 const SALT = 0b0000_0001;
7 const PEPPER = 0b0000_0010;
8 const CHILI = 0b0000_0100;
9 const SAFFRON = 0b0000_1000;
10 const ALL = Self::SALT.bits
11 | Self::PEPPER.bits
12 | Self::CHILI.bits
13 | Self::SAFFRON.bits;
14 }
15 }
16
17 impl Spices {
18 // Implementing a "clear" method can be useful
19 pub fn clear(&mut self) -> &mut Self...