Mercurial > lbo > hg > memoize
view examples/full_featured.rs @ 69:0ada62444b37
Merge pull request #10 from bijij/master
Add support for pattern binding to function arguments
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 01 Jan 2022 12:29:34 +0100 |
parents | 3ed9875b9139 |
children | b493c1f99877 |
line wrap: on
line source
use memoize::memoize; use std::time::{Instant, Duration}; use std::thread; #[derive(Debug, Clone)] struct ComplexStruct { s: String, b: bool, i: Instant, } #[memoize(TimeToLive: Duration::from_secs(2), Capacity: 123)] fn hello(key: String) -> ComplexStruct { println!("hello: {}", key); ComplexStruct { s: key, b: false, i: Instant::now(), } } fn main() { println!("result: {:?}", hello("ABC".to_string())); println!("result: {:?}", hello("DEF".to_string())); println!("result: {:?}", hello("ABC".to_string())); //Same as first thread::sleep(core::time::Duration::from_millis(2100)); println!("result: {:?}", hello("EFG".to_string())); println!("result: {:?}", hello("ABC".to_string())); //Refreshed println!("result: {:?}", hello("EFG".to_string())); //Same as first println!("result: {:?}", hello("ABC".to_string())); //Same as refreshed println!("result: {:?}", memoized_original_hello("ABC".to_string())); }