I've been working in quite a number of statically-typed programming languages (C++, Haskell, ...), but am relatively new to Rust.
I often end up writing code like this:
struct LeafNode<K: Ord + Default + Clone, V: Default + Clone> {
keys: Vec<K>,
values: Vec<V>,
}
impl <K: Ord + Default + Clone, V: Default + Clone> LeafNode<K, V> {
// ...
}
There is a lot of repetition here in the type constraints.
This is compounded further when LeafNode
is used inside something else (say, when building a tree of nodes).
Whenever one of these constraints changes during implementation, there are many places where the code needs to be altered as they are named in many of the different struct
definitions and impl
blocks.
Is there a way to create some kind of 'type constraint alias' from K: Ord + Default + Clone
and V: Default + Clone
?
If it exists, it probably has some name that I am not aware of, which makes searching for this technique very difficult. Hence this question.