LRU cache implementation for Rust
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Jerome Froelich dac07e3edc
Some checks failed
CI / ci (1.85.0) (push) Has been cancelled
CI / ci (beta) (push) Has been cancelled
CI / ci (nightly) (push) Has been cancelled
CI / ci (stable) (push) Has been cancelled
Merge pull request #234 from jeromefroe/jerome/prepare-0-18-0-release
Prepare 0.18.0 release
2026-04-27 07:15:57 -04:00
.github/workflows Update MSRV to 1.85.0 2026-04-14 23:36:27 +01:00
src Fix unconstrained lifetime in LruCache::get_or_insert_mut_ref 2026-04-27 01:20:45 +02:00
.gitignore Add resize method 2018-01-15 11:12:09 -05:00
Cargo.toml Prepare 0.18.0 release 2026-04-27 07:09:38 -04:00
CHANGELOG.md Prepare 0.18.0 release 2026-04-27 07:09:38 -04:00
LICENSE Initial commit 2016-12-11 21:21:13 -05:00
README.md Update MSRV to 1.85.0 2026-04-14 23:36:27 +01:00

LRU Cache

Build Badge crates.io Badge docs.rs Badge License Badge

Documentation

An implementation of a LRU cache. The cache supports put, get, get_mut 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.

The MSRV for this crate is 1.85.0.

Example

Below is a simple example of how to instantiate and use a LRU cache.

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!(cache.get(&"pear").is_none());

    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);
    assert!(cache.get(&"apple").is_none());

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

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