Cache implementation in Rust I've started learning Rust recently using the Rust book.

In one of the chapters, the authors encourage the reader to experiment with closures, and try to create a generic cache implementation.

I've managed to get it working as below. However, it irks me that I need to add Clone trait on the input argument in the get() method. I was wondering if there was a better way of doing this?

Any other comments are also welcome (apart from the main() method which is a sample driver).

use std::collections::HashMap;
use std::thread;
use std::time::Duration;
use std::hash::Hash;
use std::clone::Clone;

struct Cache&lt;T, U, W&gt;
    where T: Fn(U) -&gt; W,
          U: Eq + Hash + Clone {
    calculator: T,
    values: HashMap&lt;U, W&gt;,
}

impl&lt;T, U, W&gt; Cache&lt;T, U, W&gt;
    where T: Fn(U) -&gt; W,
          U: Eq + Hash + Clone {
    fn new(calculator: T) -&gt; Cache&lt;T, U, W&gt; {
        Cache {
            calculator,
            values: HashMap::new(),
        }
    }

    /*
     * The ugliest part in this function is the constraint that we have to clone the input.
     * HashMap::entry() moves the key, which renders it unusable for subsequent calculations.
     * So we can either
     * - always perform the expensive computation
     * (OR)
     * - clone the input so that it can be re-used later
     *   
     * Another problem is that defining a closure in or_insert_with borrows the Cache::calculator, and that creates a problem
     * when we attempt to borrow it again by invoking self.calculator. This is why the following line won't work:
     * self.values.entry(input.clone()).or_insert_with(|| { (self.calculator)(input) })
     */  
    fn get(&amp;mut self, input: U) -&gt; &amp;W {
        let calc = &amp;self.calculator;
        self.values.entry(input.clone()).or_insert_with(|| { (calc)(input) })
    }   
}

fn main() {
    println!("Hello, world!");
    let mut c = Cache::new(|x: &amp;str| {
        println!("performing an expensive calculation with input: {}", x);
        thread::sleep(Duration::from_secs(2));
        format!("Original input: {}", x)
    });

    let x = c.get("10");
    println!("x: {}, cacher after getting {}", x, 10);

    let x = c.get("10");
    println!("x: {}, cacher after getting {}", x, 10);

    let x = c.get("20");
    println!("x: {}, cacher after getting {}", x, 20);

    let x = c.get("10");
    println!("x: {}, cacher after getting {}", x, 10);

    let x = c.get("10");
    println!("x: {}, cacher after getting {}", x, 10);

    let x = c.get("20");
    println!("x: {}, cacher after getting {}", x, 20);
}
You don't need to have the where clause on your struct. General, rust coders prefer to only restrict the impl blocks and not the structs.

You can avoid Clone, by unpacking the Entry enum, like so:

fn get(&amp;mut self, input: U) -&gt; &amp;W {
    use std::collections::hash_map::Entry;
    match self.values.entry(input) {
        Entry::Occupied(entry) =&gt; entry.into_mut(),
        Entry::Vacant(entry) =&gt; {
            let value = (self.calculator)(entry.key());
            entry.insert(value)
        }
    }
}   


This is, in fact, close to how or_insert_with is implemented.  

Note that this calls the function with &amp;U not U. Your calculator cannot take ownership of the key if you also plan to put into the hashmap. This is awkward for your code because it is using &amp;str as the keys. Hence the type passed to the calculator is &amp;&amp;str. We can resolve this by using the borrow trait. The Borrow traits knows that it can convert &amp;&amp;str to &amp;str, so we could implement it as follows.

// Leave the struct unconstrained
struct Cache&lt;T, U, W&gt; {
    calculator: T,
    values: HashMap&lt;U, W&gt;,
}

// The impl block only constrains for Eq + Hash of the keys.
impl&lt;T, U, W&gt; Cache&lt;T, U, W&gt;
    where 
          U: Eq + Hash
         {

    fn new(calculator: T) -&gt; Cache&lt;T, U, W&gt; {
        Cache {
            calculator,
            values: HashMap::new(),
        }
    }

    // We take a new type parameter, Y.
    // The calculation function takes &amp;Y as a parameter
    // It does not need to be sized because we only use references to it.  
    fn get&lt;Y:?Sized&gt;(&amp;mut self, input: U) -&gt; &amp;W 
        where 
             // Our calculator must take a reference to Y.
             T: Fn(&amp;Y) -&gt; W,
             // The borrow trait will let us convert a &amp;U to a &amp;Y
             U: std::borrow::Borrow&lt;Y&gt; {
        use std::collections::hash_map::Entry;
        match self.values.entry(input) {
            Entry::Occupied(entry) =&gt; entry.into_mut(),
            Entry::Vacant(entry) =&gt; {
                let value = (self.calculator)(entry.key().borrow());
                entry.insert(value)
            }
        }
    }   
}