Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt::{Display, Formatter};
- pub struct Example {
- pub flags: u64,
- }
- impl Display for Example {
- // I would like to print out the flags with the str repr
- fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- // READ 0x1
- // WRITE 0x2
- // EXEC 0x4
- let mut flag_repr = String::new();
- if self.flags & 1 == 1 {
- flag_repr.push('R');
- } else {
- flag_repr.push('-');
- }
- if self.flags & 2 == 2 {
- flag_repr.push('W');
- } else {
- flag_repr.push('-');
- }
- if self.flags & 4 == 4 {
- flag_repr.push('X');
- } else {
- flag_repr.push('-');
- }
- write!(f, "{}", flag_repr)
- }
- }
- fn main() {
- for x in 0..8 {
- println!("{}", Example{flags:x});
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement