I found this question very helpful when trying to understand how to extend the Cacher function introduced in Chapter 13.
Im just confused about what is happening here:
T: Fn(K) -> V,
K: hash::Hash + Eq + Clone,
It makes sense to me that
T is a function that takes something of type
K and returns type
V. But what does it mean that
K is of type
hash::Hash + Eq + Clone and
V is of type
Copy. Thanks for any help.
Those are trait bounds that the types must satisfy.
Hash means that
K can be hashed (for a
Eq means that two instances of
K can be checked for equality, also needed for a
HashMap (so collisions can be detected)
Clone means you can duplicate
K by cloning it (which may be expensive or may be cheap)
Copy means that you can duplicate
V by a simple
memcpy, which will happen implicitly if needed when you pass the value around or assign it to a new variable
If it's unclear how these are used in any particular part of the code, please give an example. Or try running the code in the Playground, then remove a bound and see what the compiler error says. (If there isn't an error, the bound wasn't needed.)
T: Fn(K) -> V is also a trait bound.
T could be a closure, a function, a function pointer, etc.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.