1pub mod laughable_lifetimes {
2 use std::time::Duration;
3
4 pub fn set_joke_lifetime(joke: &str, lifetime: Duration) {
5 println!("Setting the lifetime of joke '{}' to {} seconds.", joke, lifetime.as_secs());
6 }
7}
8
9pub mod comedic_concurrency {
10 use std::sync::{Arc, Mutex};
11 use std::thread;
12
13 pub fn concurrent_laughter(joke: &str, num_threads: u32) {
14 let shared_joke = Arc::new(Mutex::new(joke.to_owned()));
15
16 let mut handles = vec![];
17
18 for i in 0..num_threads {
19 let cloned_joke = Arc::clone(&shared_joke);
20 let handle = thread::spawn(move || {
21 let joke = cloned_joke.lock().unwrap();
22 println!("Thread {}: Laughing at '{}'", i + 1, joke);
23 });
24 handles.push(handle);
25 }
26
27 for handle in handles {
28 handle.join().unwrap();
29 }
30 }
31}
32
33pub mod punchline_pointers {
34 pub fn deliver_punchline(joke: &str, punchline: &str) {
35 let punchline_ptr = punchline.as_ptr();
36 let len = punchline.len();
37 unsafe {
38 let raw_punchline = std::slice::from_raw_parts(punchline_ptr, len);
39 let punchline_str = std::str::from_utf8_unchecked(raw_punchline);
40 println!("Joke: {}\nPunchline: {}", joke, punchline_str);
41 }
42 }
43}
44
45pub fn chuckle_norris() -> String {
46 let joke = "Why do Rust programs never have buffer overflows?";
47 let punchline = "Because Chuck Norris guards the memory!";
48 format!("{} {}", joke, punchline)
49}
50
51