我通常能够使用特征作为 Box<> 之类的类型参数:
trait CtxVal {}
type CtxNodes = HashMap<String, Box<CtxVal>>;
但是当特征有它自己的泛型类型参数时,比如PartialEq ,我被困住了。
type CtxNodes = HashMap<String, Box<PartialEq>>;
我收到错误:
main.rs:6:37: 6:46 error: the type parameter `Rhs` must be explicitly specified in an
object type because its default value `Self` references the type `Self`
main.rs:6 type CtxNodes = HashMap<String, Box<PartialEq>>;
^~~~~~~~~
如果我要提供 PartialEq
的类型,那会是什么?
Box<PartialEq<???>>
最佳答案
您需要指定您希望对象能够比较的内容等于:
fn foo(value: Box<PartialEq<u8>>) -> bool {
*value == 42
}
fn bar(value: Box<PartialEq<&str>>) -> bool {
*value == "the answer"
}
关于rust - 如何对具有泛型类型参数的特征进行装箱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29947744/