Fricative Melon
Fricative Melon

Reputation: 389

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

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>. Is there some other syntax to do this, so that I can indicate comparability of T and U in the function signature?

Upvotes: 0

Views: 163

Answers (1)

kmdreko
kmdreko

Reputation: 59852

Yes, type constraints can use other type parameters. The error you're seeing is because Eq is not generic.

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.

Upvotes: 4

Related Questions