有没有办法为多个特征创建一个类型别名?

Dan*_*mon 21 rust

我有一个通用函数,打印至少两个项目:

use std::fmt::Display;

fn print_min<T: PartialOrd + Display>(a: &T, b: &T) {
    println!("min = {}", if a < b { a } else { b });
}
Run Code Online (Sandbox Code Playgroud)

这适用于实现PartialOrdDisplay特征的任何东西:

print_min(&45, &46);
// min = 45
print_min(&"a", &"b");
// min = a
Run Code Online (Sandbox Code Playgroud)

必须放入PartialOrd + Display函数定义是一种丑陋的,特别是如果我想拥有一大堆对此进行操作的函数(例如,实现二进制搜索树),或者我的边界变得更复杂.我的第一个倾向是尝试编写一个类型别名:

type PartialDisplay = PartialOrd + Display;
Run Code Online (Sandbox Code Playgroud)

但这给了我一些相当奇怪的编译器错误:

error[E0393]: the type parameter `Rhs` must be explicitly specified
 --> src/main.rs:7:23
  |
7 | type PartialDisplay = PartialOrd + Display;
  |                       ^^^^^^^^^^ missing reference to `Rhs`
  |
  = note: because of the default `Self` reference, type parameters must be specified on object types

error[E0225]: only auto traits can be used as additional traits in a trait object
 --> src/main.rs:7:36
  |
7 | type PartialDisplay = PartialOrd + Display;
  |                                    ^^^^^^^ non-auto additional trait
Run Code Online (Sandbox Code Playgroud)

我猜我的语法是错的还是这还不可能.我喜欢类似的东西

type PartialDisplay = ???
fn print_min<T: PartialDisplay> { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

snf*_*snf 25

PartialOrd并且Display是特征.它已经讨论了如何实现一个别名,但它决定是没有必要的.

相反,您可以创建一个具有您想要的特征作为超级特征的新特征,并提供一揽子实施:

use std::fmt::Display;

trait PartialDisplay: PartialOrd + Display {}
impl<T: PartialOrd + Display> PartialDisplay for T {}

fn print_min<T: PartialDisplay>(a: &T, b: &T) {
    println!("min = {}", if a < b { a } else { b });
}

fn main() {
    print_min(&45, &46);
    print_min(&"aa", &"bb");
}
Run Code Online (Sandbox Code Playgroud)


She*_*ter 9

RFC 1733引入了特征别名的概念.当它的实现,你就可以说:

#![feature(trait_alias)]

use std::fmt::Display;

trait PartialDisplay<Rhs = Self> = PartialOrd<Rhs> + Display;

fn print_min<T: PartialDisplay>(a: &T, b: &T) {
    println!("min = {}", if a < b { a } else { b });
}

fn main() {
    print_min(&45, &46);
    print_min(&"a", &"b");
}
Run Code Online (Sandbox Code Playgroud)


小智 5

在等待特征别名稳定时,您可以使用特征集板条箱。下面是它的使用示例:

trait-set! {
    pub trait HashKey = Hash + Eq + Clone
}
Run Code Online (Sandbox Code Playgroud)