Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. // Modules
  2. use chrono::prelude::*;
  3. use std::{thread, time};
  4.  
  5. pub fn wait(millis: u64) {
  6. // Variables immutable per default
  7. let duration = time::Duration::from_millis(millis);
  8.  
  9. thread::sleep(duration);
  10. }
  11.  
  12. // Functions
  13. pub fn is_at_web_and_wine() -> bool {
  14. let local: DateTime<Local> = Local::now();
  15.  
  16. // Pattern Matching
  17. match (
  18. local.day(),
  19. local.month(),
  20. local.year()
  21. ) {
  22. (15, 10, 2019) => true,
  23. _ => false,
  24. }
  25. }
  26.  
  27. // Structs
  28. struct GreeterBot {
  29. face_emoji: String,
  30. greeting: String,
  31. }
  32.  
  33. // Methods
  34. impl GreeterBot {
  35. // static methods
  36. pub fn new(face_emoji: &str, greeting: &str) -> Self {
  37. // implicit return
  38. GreeterBot {
  39. face_emoji: face_emoji.to_owned(),
  40. greeting: greeting.to_owned()
  41. }
  42. }
  43.  
  44. fn speak(&self, message: &str) {
  45. println!("{} : {}", self.face_emoji, message);
  46. }
  47.  
  48. pub fn ask_purpose(&self) {
  49. self.speak("What is my purpose?");
  50. }
  51.  
  52. pub fn cannot_compute(&self) {
  53. self.speak("Cannot greet those who are not here. Critical failure...oh no...");
  54. }
  55.  
  56. pub fn greet(&self) {
  57. println!("
  58. __________________________________________
  59. / \\
  60. | {} |
  61. \\__ ______________________________________/
  62. \\/
  63. {}‍
  64. /||\\
  65. /\\
  66. ", self.greeting, self.face_emoji);
  67. }
  68. }
  69.  
  70. pub fn make_greeting(addressee: &str) -> String {
  71. let local: DateTime<Local> = Local::now();
  72. let time_log = format!(
  73. "{:02}-{:02}-{:02} {:02}:{:02}:{:02}",
  74. local.day(),
  75. local.month(),
  76. local.year(),
  77. local.hour(),
  78. local.minute(),
  79. local.second()
  80. );
  81.  
  82. // implicit return
  83. format!("Hello, {}! {}", addressee, time_log)
  84. }
  85.  
  86. pub fn log_countdown() {
  87. [3, 2, 1].iter()
  88. .map(|step| match step {
  89. 3 => "3️⃣",
  90. 2 => "2️⃣",
  91. 1 => "1️⃣",
  92. _ => ""
  93. })
  94. .for_each(|step| {
  95. println!("{}", step);
  96. wait(1000);
  97. });
  98. }
  99.  
  100. pub fn main() {
  101. let kauderwelsch = vec!['e', 'n', 'i', 'W', '-', 'd', 'n', 'a', '-', 'b', 'e', 'W'];
  102. // Iterators
  103. let adressee = kauderwelsch.iter().rev().collect::<String>();
  104.  
  105. // Borrwing and Ownership
  106. let greeter_bot = GreeterBot::new("🤖", &make_greeting(&adressee));
  107.  
  108. greeter_bot.ask_purpose();
  109.  
  110. wait(1000);
  111.  
  112. println!("👨‍ : You greet!");
  113.  
  114.  
  115. // Normal boring flow control
  116. if is_at_web_and_wine() {
  117. wait(1000);
  118.  
  119. greeter_bot.speak("Ok, here it comes...");
  120.  
  121. wait(1000);
  122.  
  123. log_countdown();
  124.  
  125. greeter_bot.greet();
  126.  
  127. wait(1000);
  128.  
  129. println!("👨‍ : Good bot!");
  130. } else {
  131. wait(1000);
  132.  
  133. greeter_bot.cannot_compute();
  134.  
  135. wait(1000);
  136.  
  137. log_countdown();
  138.  
  139. greeter_bot.speak("Bumm 💥 🔥");
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement