Reputation: 538
I have some code :
impl<K, V> Database<K, V>
where
K: Eq + Hash,
V: Clone,
Where I have Database
defined like :
pub struct Database<K, V>
where
K: Eq + Hash,
Note the absence of trait bounds on V
. My question is what happens if I construct an instance of this object such that V: !Clone
? Would my methods that depend on V: Clone
simple panic? Or will the rust compiler catch it?
Upvotes: 3
Views: 109
Reputation: 58705
You can read this:
impl<K, V> Database<K, V>
where
K: Eq + Hash,
V: Clone,
{
// methods
}
As:
Implement these methods for
Database<K, V>
wheneverK
implementsEq + Hash
andV
implementsClone
.
If those constraints are not met then those methods will not exist, and you'll see a compile error if you try to call them.
These constraints are different from the ones on the type itself. Those just affect if you can construct an instance of the type. It's actually quite common to omit all constraints on the type itself and only have them on the impl
blocks.
Upvotes: 4