rust
37 lines · 6 steps
A generic memoizer in Rust
A reusable struct that caches the results of any pure function, keyed by its input.
Explained by
highlit
1use std::collections::HashMap;
2
3pub struct Memoizer<K, V, F> {
4 cache: HashMap<K, V>,
5 compute: F,
6}
7
8impl<K, V, F> Memoizer<K, V, F>
9where
10 K: std::hash::Hash + Eq + Clone,
11 V: Clone,
12 F: Fn(&K) -> V,
13{
14 pub fn new(compute: F) -> Self {
15 Self {
16 cache: HashMap::new(),
17 compute,
18 }
19 }
20
21 pub fn get(&mut self, key: K) -> V {
22 if let Some(value) = self.cache.get(&key) {
23 return value.clone();
24 }
25 let value = (self.compute)(&key);
26 self.cache.insert(key, value.clone());
27 value
28 }
29
30 pub fn len(&self) -> usize {
31 self.cache.len()
32 }
33
34 pub fn invalidate(&mut self, key: &K) -> Option<V> {
35 self.cache.remove(key)
36 }
37}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Storing a closure in a generic field lets one struct memoize any compatible function.
- 2Trait bounds like Hash, Eq, and Clone express exactly what the cache needs from its types.
- 3Checking the cache before computing turns repeated work into a single hash lookup.
Related explainers
rust
use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::thread;
Aggregating metrics across threads in Rust
concurrency
shared-state
mutex
Intermediate
7 steps
ruby
require "csv" class SalesReport def initialize(path)
Aggregating CSV sales data in Ruby
data-aggregation
memoization
group_by
Intermediate
6 steps
rust
use std::sync::{mpsc, Arc, Mutex}; use std::thread; use std::time::Duration;
Building a thread pool in Rust
concurrency
channels
thread-pool
Advanced
9 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
rust
use std::time::Instant; use tracing::info; pub struct Timer {
A scope-guard timer with Drop in Rust
raii
drop-guard
timing
Intermediate
7 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-generic-memoizer-in-rust-explained-rust-7c22/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.