简体   繁体   English

具有多个特征的泛型类型

[英]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. 我想强制T泛型类型同时实现PartialEq和PartialOrd特性。 I can't found the synthax to do this (the & char not being the one I look for). 我找不到执行此操作的合成器(&char不是我要寻找的那个)。 Thanks for helping me. 谢谢你帮我

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>> . 我还建议将Box<Option<U>>更改为Option<Box<U>> This can be represented more efficiently ( None doesn't require an allocation, and is in fact represented as a null pointer). 这可以更有效地表示( None不需要分配,并且实际上表示为空指针)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM