内容简介:我们阅读Rust程序的时候,有时候会出现如下的代码:下面是上面代码的注解:Iterator trait 有一个关联类型这里的
关联类型(associated types)
我们阅读Rust程序的时候,有时候会出现如下的代码:
trait Iterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
}
下面是上面代码的注解:Iterator trait 有一个关联类型 Item
。 Item
是一个占位类型,同时 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());
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- presto中不同类型的字段关联问题
- 如何将iPhone应用程序与_every_文件类型相关联?
- QuickDAO4.1.3 发布,优化全局类型转换和新增多字段关联
- Flink 维表关联系列之 Kafka 维表关联:广播方式
- Flink 维表关联系列之 Redis 维表关联:实时查询
- Flink 维表关联系列之 MySQL 维表关联:全量加载
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Writing Windows VxDs and Device Drivers, Second Edition
Karen Hazzah / CMP / 1996-01-12 / USD 54.95
Software developer and author Karen Hazzah expands her original treatise on device drivers in the second edition of "Writing Windows VxDs and Device Drivers." The book and companion disk include the a......一起来看看 《Writing Windows VxDs and Device Drivers, Second Edition》 这本书的介绍吧!
JSON 在线解析
在线 JSON 格式化工具
RGB CMYK 转换工具
RGB CMYK 互转工具