Rust关联类型与默认泛型类型参数

栏目: 编程语言 · Rust · 发布时间: 7年前

内容简介:我们阅读Rust程序的时候,有时候会出现如下的代码:下面是上面代码的注解:Iterator trait 有一个关联类型这里的

关联类型(associated types)

我们阅读Rust程序的时候,有时候会出现如下的代码:

trait Iterator {
    type Item; 
    fn next(&mut self) -> Option<Self::Item>;
}

下面是上面代码的注解:Iterator trait 有一个关联类型 ItemItem 是一个占位类型,同时 next 方法会返回 Option<Self::Item> 类型的值。这个 trait 的实现者会指定 Item 的具体类型。

这里的 type 用法就是关联类型。

关联类型(associated types)是一个将类型占位符与 trait 相关联的方式,这样 trait 的方法签名中就可以使用这些占位符类型。trait 的实现者会针对特定的实现在这个类型的位置指定相应的具体类型。如此可以定义一个使用多种类型的 trait,直到实现此 trait 时都无需知道这些类型具体是什么。

使用关联类型的代码示例如下:

pub trait Watch {
    type Item;
    fn inner(&self) -> Option<Self::Item>;
}

struct A {
    data: i32,
}

impl Watch for A {
    type Item = i32;
    fn inner(&self) -> Option<Self::Item> {
        Some(self.data)
    }
}

struct B {
    data: String,
}

impl Watch for B {
    type Item = String;
    fn inner(&self) -> Option<Self::Item> {
        Some(self.data.clone())
    }
}

fn main() {
    let a = A{data: 10};
    let b = B{data: String::from("B")};
    assert_eq!(Some(10), a.inner());
    assert_eq!(Some(String::from("B")), b.inner());
}

默认泛型类型参数

我们还会碰到如下类型的代码:

#[lang = "add"]
pub trait Add<RHS = Self> {
    type Output;

    #[must_use]
    fn add(self, rhs: RHS) -> Self::Output;
}

这里 Add<RHS = Self> 是默认泛型类型参数,表示如果不显示指定泛型类型,就默认泛型类型为 Self

当使用泛型类型参数时,可以为泛型指定一个默认的具体类型。如果默认类型就足够的话,这消除了为具体类型实现 trait 的需要。为泛型类型指定默认类型的语法是在声明泛型类型时使用

使用默认泛型类型参数的示例代码如下:

pub trait Watch<Inner=String> {
    type Item;
    fn inner(&self) -> Option<Self::Item>;
    fn info(&self) -> Inner;
}

struct A {
    data: i32,
}

impl Watch<i32> for A {
    type Item = i32;
    fn inner(&self) -> Option<Self::Item> {
        Some(self.data)
    }
    fn info(&self) -> i32 {
        println!("A inner is {}", self.data);
        self.data
    }
}

struct B {
    data: String,
}

impl Watch for B {
    type Item = String;
    fn inner(&self) -> Option<Self::Item> {
        Some(self.data.clone())
    }
    fn info(&self) -> String {
        println!("B inner is {}", self.data);
        self.data.clone()
    }
}

fn main() {
    let a = A{data: 10};
    let b = B{data: String::from("B")};
    assert_eq!(10, a.info());
    assert_eq!(Some(String::from("B")), b.inner());
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构与算法

数据结构与算法

张铭、王腾蛟、赵海燕 / 高等教育出版社 / 2008-6 / 34.00元

《数据结构与算法》是普通高等教育“十一五”国家级规划教材,也是北京市精品课程主讲教材。《数据结构与算法》按照IEEE/ACM CC20025和教育部教指委关于“计算机科学与技术专业规范”(CCC2005)的要求编写,力求使学生较全面地理解数据结构的概念、掌握各种数据结构与算法的实现方式,同时比较不同数据结构和算法的特点,重点强调实践教学和学生动手能力的培养。 《数据结构与算法》的内容涉及基本......一起来看看 《数据结构与算法》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具