diff --git a/src/bin/mutex_cache.rs b/src/bin/mutex_cache.rs new file mode 100644 index 0000000..de9beda --- /dev/null +++ b/src/bin/mutex_cache.rs @@ -0,0 +1,29 @@ +use tokio::sync::Mutex; + +struct Cache(Mutex>); + +impl Cache { + async fn get(&self, index: usize) -> Option + where + T: Clone, + { + self.0.lock().await.get(index).cloned() + } +} + +#[tokio::main] +async fn main() { + let cache = Cache(Mutex::new(vec![ + "Rust ".to_string(), + "is ".to_string(), + "awesome!".to_string(), + ])); + + for i in 0..3 { + if let Some(item) = cache.get(i).await { + print!("{}", item); + } + } +} + +// Rust is awesome!