【Rust】关联类型

环境

  • Rust 1.56.1
  • VSCode 1.61.2

概念

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

示例

main.rs

struct Container(i32, i32);

trait Contains {
    // 关联类型
    type A;
    type B;

    fn contains(&self, _: &Self::A, _: &Self::B) -> bool;
    fn first(&self) -> i32;
    fn last(&self) -> i32;
}

impl Contains for Container {
    // 指定类型
    type A = i32;
    type B = i32;

    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
    }
}

fn difference<C: Contains>(container: &C) -> i32 {
    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 中的关联类型,使用关联类型来实现了无关联类型中的一个例子。

附录

posted @ 2021-12-11 21:33  jiangbo4444  阅读(179)  评论(0编辑  收藏  举报