Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on the Rust Crate lru

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [2]:
:timing
:sccache 1
:dep lru = "0.10.0"
Out[2]:
Timing: true
sccache: true
Out[2]:
Took 2406ms
In [3]:
use lru::LruCache;
use std::num::NonZeroUsize;
Out[3]:
Took 63ms
In [5]:
let mut cache: LruCache<u64, &str> = LruCache::new(NonZeroUsize::new(2).unwrap());
Out[5]:
Took 260ms
In [6]:
cache.put(1, "a")
Out[6]:
None
Out[6]:
Took 334ms
In [7]:
cache.put(2, "b")
Out[7]:
None
Out[7]:
Took 258ms
In [8]:
cache
Out[8]:
LruCache { len: 2, cap: 2 }
Out[8]:
Took 236ms
In [9]:
cache.put(2, "beta")
Out[9]:
Some("b")
Out[9]:
Took 326ms
In [11]:
cache.put(3, "hello")
Out[11]:
None
Out[11]:
Took 323ms
In [12]:
cache
Out[12]:
LruCache { len: 2, cap: 2 }
Out[12]:
Took 246ms
In [13]:
cache.get(&1)
Out[13]:
None
Out[13]:
Took 216ms
In [14]:
cache.get(&2)
Out[14]:
Some("beta")
Out[14]:
Took 211ms
In [15]:
cache.get(&3)
Out[15]:
Some("hello")
Out[15]:
Took 218ms
In [ ]:

Comments