Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. enum Anything
  2. {
  3. Something,
  4. Nothing,
  5. }
  6.  
  7. type Search = fn(&Life) -> Status;
  8.  
  9. enum Status
  10. {
  11. Searching(Search),
  12. Answer(Anything),
  13. }
  14.  
  15. struct Life;
  16.  
  17. impl Life
  18. {
  19. fn understanding(&self) -> Status
  20. {
  21. // This is a simulation
  22.  
  23. // Status::Answer(Anything::Nothing)
  24. // Status::Answer(Anything::Something)
  25. Status::Searching(Life::enlightenment)
  26. }
  27.  
  28. fn enlightenment(&self) -> Status
  29. {
  30. Status::Answer(Anything::Nothing)
  31. }
  32. }
  33.  
  34. fn search_origin(life: &Life) -> Status
  35. {
  36. life.understanding()
  37. }
  38.  
  39. fn main()
  40. {
  41. let life = Life;
  42. let mut most_important_thing_in_life: Status;
  43.  
  44. most_important_thing_in_life = Status::Searching(search_origin);
  45.  
  46. while let Status::Searching(search) = most_important_thing_in_life
  47. {
  48. println!("{:?}", most_important_thing_in_life);
  49. most_important_thing_in_life = search(&life);
  50. }
  51.  
  52. println!("{:?}", most_important_thing_in_life);
  53. }
  54.  
  55. use std::fmt::{
  56. self,
  57. Debug,
  58. Formatter,
  59. };
  60.  
  61. impl Debug for Anything
  62. {
  63. fn fmt(&self, formatter: &mut Formatter) -> fmt::Result
  64. {
  65. use Anything::*;
  66.  
  67. match self
  68. {
  69. Something => write!(formatter, "Something"),
  70. Nothing => write!(formatter, "Nothing"),
  71. }
  72. }
  73. }
  74.  
  75. impl Debug for Status
  76. {
  77. fn fmt(&self, formatter: &mut Formatter) -> fmt::Result
  78. {
  79. use Status::*;
  80.  
  81. match self
  82. {
  83. Searching(_) => write!(formatter, "Searching"),
  84. Answer(answer) => write!(formatter, "Answer({:?})", answer),
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement