6

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.

Qqwy
  • 5,214
  • 5
  • 42
  • 83
  • 2
    well, you do not need to constrain the structs, just with the impl would be enough (usually), that would save you a 50% of constrains declaration :) – Netwave Dec 16 '21 at 12:19
  • @Netwave Interesting! When do and when don't you need to constrain the structs themselves? – Qqwy Dec 16 '21 at 12:34
  • 1
    @Qqwy See [this question](https://stackoverflow.com/questions/49229332/should-trait-bounds-be-duplicated-in-struct-and-impl) for more details about when to use bounds on structs. – Filipe Rodrigues Dec 16 '21 at 15:01

2 Answers2

8

You can accomplish this by making your own trait that takes the other traits as a bound, then add a blanket implementation for it:

trait MyConstraint: Ord + Default + Clone {}
impl <T: Ord + Default + Clone> MyConstraint for T {}

struct LeafNode<K: MyConstraint> {
    keys: Vec<K>
}

impl<K: MyConstraint> LeafNode<K> {
    fn keys(&self) -> &[K] {
        &self.keys
    }
}
Aplet123
  • 33,825
  • 1
  • 29
  • 55
3

As a complement answer.

You can use trait alias (currently unstable):

#![feature(trait_alias)]

trait MyConstraint = Ord + Default + Clone;

struct LeafNode<K: MyConstraint> {
    keys: Vec<K>
}

impl<K: MyConstraint> LeafNode<K> {
    fn keys(&self) -> &[K] {
        &self.keys
    }
}

Playground

Netwave
  • 40,134
  • 6
  • 50
  • 93