简体   繁体   English

Rust:泛型参数能否具有引用其他参数的界限?

[英]Rust: Can a generic parameter have bounds referencing other parameters?

I want to create a function that takes two arrays of comparable types and compares them lexicographically, like so我想创建一个 function ,它需要两个类似类型的 arrays 并按字典顺序比较它们,就像这样

use std::cmp::Eq;

fn compare_arrays<T : Eq<U>, U>(a1: &[T], a2: &[U]) -> bool {
    //Comparison code here
    return false
}

However, the Rust compiler does not accept T: Eq<U> .但是,Rust 编译器不接受T: Eq<U> Is there some other syntax to do this, so that I can indicate comparability of T and U in the function signature?是否有其他语法可以做到这一点,以便我可以在 function 签名中指出TU的可比性?

Yes, type constraints can use other type parameters.是的,类型约束可以使用其他类型参数。 The error you're seeing is because Eq is not generic .您看到的错误是因为Eq不是通用的。

error[E0107]: wrong number of type arguments: expected 0, found 1
 --> src/lib.rs:3:26
  |
3 | fn compare_arrays<T : Eq<U>, U>(a1: &[T], a2: &[U]) -> bool {
  |                          ^ unexpected type argument

Use PartialEq instead.请改用PartialEq

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

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