【问题标题】:Increasing the traits requirement for a type in Rust增加 Rust 中类型的特征要求
【发布时间】:2021-08-22 06:37:33
【问题描述】:

假设我有以下内容

pub struct A { type M : Eq + PartialOrd; } 

我怎样才能创建类似的东西

struct B { type N : Eq + PartialOrd + std::hash::Hash; } 

不一定要引用 Eq + PartialOrd 而是直接引用 M 并写类似的东西

type N = A::M + std::hash::Hash;

【问题讨论】:

  • 很遗憾,我认为你不能。你最好的选择可能是制作一个新的特质,并将所有要求都作为超级特质。

标签: rust traits


【解决方案1】:

它仍然不稳定,但这样做的方式是trait_alias:

#![feature(trait_alias)]

trait At = Eq + PartialOrd;
trait Bt = At + std::hash::Hash;

Playground

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 2020-08-17
    • 2019-02-28
    • 2020-12-31
    • 2022-07-31
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2023-01-12
    相关资源
    最近更新 更多