Moebius
Moebius

Reputation: 6518

Generic type implementing more than one trait

Here is my code :

struct Node<T: PartialEq & PartialOrd>
{
    left: Box<Option<Node<T>>>,
    right: Box<Option<Node<T>>>,
    value: Option<T>,
}

I want to force the T generic type to implement both the PartialEq and the PartialOrd traits. I can't found the synthax to do this (the & char not being the one I look for). Thanks for helping me.

Upvotes: 1

Views: 193

Answers (1)

Chris Morgan
Chris Morgan

Reputation: 90752

The syntax is +:

struct Node<T: PartialEq + PartialOrd> {
    left: Option<Box<Node<T>>>,
    right: Option<Box<Node<T>>>,
    value: Option<T>,
}

I would also recommend changing Box<Option<U>> to Option<Box<U>>. This can be represented more efficiently (None doesn’t require an allocation, and is in fact represented as a null pointer).

Upvotes: 5

Related Questions