Advertisement
Guest User

Untitled

a guest
Aug 14th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. use std::fmt::{Display, Formatter};
  2.  
  3. pub struct Example {
  4. pub flags: u64,
  5. }
  6.  
  7. impl Display for Example {
  8. // I would like to print out the flags with the str repr
  9. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  10. // READ 0x1
  11. // WRITE 0x2
  12. // EXEC 0x4
  13. let mut flag_repr = String::new();
  14.  
  15. if self.flags & 1 == 1 {
  16. flag_repr.push('R');
  17. } else {
  18. flag_repr.push('-');
  19. }
  20. if self.flags & 2 == 2 {
  21. flag_repr.push('W');
  22. } else {
  23. flag_repr.push('-');
  24. }
  25. if self.flags & 4 == 4 {
  26. flag_repr.push('X');
  27. } else {
  28. flag_repr.push('-');
  29. }
  30.  
  31. write!(f, "{}", flag_repr)
  32. }
  33. }
  34.  
  35. fn main() {
  36. for x in 0..8 {
  37. println!("{}", Example{flags:x});
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement