Crate lru

source ·
Expand description

An implementation of a LRU cache. The cache supports get, get_mut, put, and pop operations, all of which are O(1). This crate was heavily influenced by the LRU Cache implementation in an earlier version of Rust’s std::collections crate.


extern crate lru;

use lru::LruCache;
use std::num::NonZeroUsize;

fn main() {
        let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
        cache.put("apple", 3);
        cache.put("banana", 2);

        assert_eq!(*cache.get(&"apple").unwrap(), 3);
        assert_eq!(*cache.get(&"banana").unwrap(), 2);

        assert_eq!(cache.put("banana", 4), Some(2));
        assert_eq!(cache.put("pear", 5), None);

        assert_eq!(*cache.get(&"pear").unwrap(), 5);
        assert_eq!(*cache.get(&"banana").unwrap(), 4);

            let v = cache.get_mut(&"banana").unwrap();
            *v = 6;

        assert_eq!(*cache.get(&"banana").unwrap(), 6);


  • An iterator that moves out of a LruCache.
  • An iterator over the entries of a LruCache.
  • An iterator over mutables entries of a LruCache.
  • An LRU Cache

Type Definitions