Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. extern crate bytes; // 0.4.12
  2. use bytes::BufMut;
  3.  
  4. #[derive(Debug)]
  5. struct Message {
  6. id: u32,
  7. body: Body,
  8. }
  9.  
  10. #[derive(Debug)]
  11. enum Body {
  12. SendMessage { to: String, message: String },
  13. ReceiveMessage { from: String, message: String },
  14. }
  15.  
  16. fn main() {
  17. let m = Message {
  18. id: 1,
  19. body: Body::SendMessage {
  20. to: "#rust".to_string(),
  21. message: "help".to_string(),
  22. },
  23. };
  24.  
  25. println!("{:#?}", m);
  26.  
  27. // This seems super awkward, is there a way of not listing each attribute?
  28. match m.body {
  29. Body::SendMessage{message, to} => println!("{} please {}", to, message),
  30. _ => ()
  31. };
  32.  
  33. // Is there some way I can get this behaviour? I don't really want to
  34. // implement IntoBuf for every variant if I can help it?
  35. let b = bytes::BytesMut::new();
  36. b.put(m);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement