【Rust】无关联类型

环境

  • Rust 1.56.1
  • VSCode 1.61.2

概念

参考:https://doc.rust-lang.org/stable/rust-by-example/generics/assoc_items/the_problem.html

示例

在使用泛型的过程中,有时候可能会觉得不方便,以下是一个例子。

main.rs

struct Container(i32, i32);

trait Contains<A, B> {
    fn contains(&self, _: &A, _: &B) -> bool;
    fn first(&self) -> i32;
    fn last(&self) -> i32;
}

impl Contains<i32, i32> for Container {
    fn contains(&self, n1: &i32, n2: &i32) -> bool {
        (&self.0 == n1) && (&self.1 == n2)
    }

    fn first(&self) -> i32 {
        self.0
    }

    fn last(&self) -> i32 {
        self.1
    }
}

// 问题在这里,需要重新申明 A 和 B,虽然并没有使用到
fn difference<A, B, C>(container: &C) -> i32
where
    C: Contains<A, B>,
{
    container.last() - container.first()
}

fn main() {
    let number_1 = 3;
    let number_2 = 10;

    let container = Container(number_1, number_2);

    println!(
        "Does container contain {} and {}: {}",
        &number_1,
        &number_2,
        container.contains(&number_1, &number_2)
    );

    println!("The difference is: {}", difference(&container));
}

总结

了解了 Rust 中没有关联类型时,直接使用泛型有可能产生的一点小问题。

附录

原文地址:https://www.cnblogs.com/jiangbo44/p/15724680.html