📕
innohub
  • KEEP LEARNING
  • WebInfo
    • JS 部分运算符操作
    • javascript 中的object array
    • 错误处理以及异常捕获
    • JavaScript Bases
    • LaoZi & Confucius
  • PyInfo
    • Python3 输入与输出
    • Python3OS
    • python 修饰器的使用
    • python3 与mysql数据库连接使用实例
    • Format-specifier
    • CGI初学踩坑
    • Django 项目测试
    • Assert-info
    • 使用ngnix和uWSGI配置Django
    • write|SVN​
    • Matplotlib 基本使用
    • 重读 Python 官方文档
    • Python3 Base
    • python3 多线程
    • Python3 函数解析
    • python3 str 对象的基本操作
    • protocl buffers
    • Iterator-Generator
    • Django base
    • Decorator 2
    • Export to wheel or egg-info
    • 三. 运算、数据及逻辑
  • GoInfo
    • Info
      • Select 语句使用
      • First class function
      • Work Pools
      • StructTag
      • Go struct
      • 常用函数
      • Strings操作
      • Golang Bases
      • 数组以及切片
      • 文件操作
      • Golang 中的指针类型
      • Golang Map 类型
      • Reflection
      • 函数闭包
      • 接口
      • Panic and Recover
      • Go中的错误处理
      • 并发
      • defer usage
      • Method in golang
      • Object-oriented Programming
      • Goalng 包管理机制
  • RustInfo
    • Info
      • 包、crate、模块
      • Rust 中的错误处理
      • 智能指针
      • 泛型 generics
      • 数据布局与内存对齐
      • Functions and methods
      • 无畏并发
      • Actix-web 基本使用
      • Got from guessing game
      • 结构体的使用
      • Rust 中的函数式语言功能
      • 集合类型
      • 枚举以及模式匹配
      • Slice 类型
      • 生命周期
      • 测试
      • Rust 中的所有权
      • trait
      • 不安全 Rust
      • Format_print
      • Rust 通用编程概念
      • Macro
  • OS
    • info
      • 内存屏障 -- Part 1
      • 内存屏障 -- Part 2
      • CPU 上下文切换
      • 文件读写与零拷贝
      • ELF 文件
  • MySql
    • info
      • MySql 架构与历史
      • 02-key
  • kubernetes
    • 第二章 k8s 基本概念
    • 第一章 Kubernetes -- 伟大舵手
  • Redis
    • info
      • Redis 总览
      • 02-underline-ds
  • Shell&CInfo
    • GCC 与make的使用
    • C++ 中 malloc 与 new
    • 位运算符操作
    • Base of C macro
    • C 语言中 extern 关键字的用法
  • Distributed
    • info
      • 分布式理论概述
  • Java
    • info
      • Java 内存模型
  • Prometheus
    • Prometheus -- 不灭之火
Powered by GitBook
On this page
  • 1. trait 作用
  • 2. trait 使用
  • 2.1 定义一个 trait
  • 2.2 默认实现
  • 3. Trait bound
  • 通过 where 简化代码

Was this helpful?

  1. RustInfo
  2. Info

trait

[TOC]

1. trait 作用

类似于很多语言中的接口,Rust 使用 trait 定义一些类型的共享功能。使用 trait 以一种抽象的方式定义共享行为。trait 通常与反省一起使用,通过trait bound指定泛型是任何拥有特定行为的类型。

trait 定义了一个类型需要实现的所有行为,只给出函数签名。

2. trait 使用

2.1 定义一个 trait

使用trait关键字定义一个 trait 。一般定义在库项目中,为了其他的项目可以使用,定义为pub。

pub trait Summary {
    fn summarize(&self) -> String;
}

然后可以为一个具体类型的实现该 trait,但是注意,必须位于同一个crate的本地作用域下定义的类型才可以实现 trait。 这个限制与golang一致,主要为了满足相干性(coherence),或者叫孤儿规则(orphan rule),得名于不存在父类型。如果没有这个规则的话,对于同一个类型可以在两个不同的 crate 中实现相同的 trait, 那么调用时就会出现混淆。

// Article online
pub mod online {
    pub trait Summary {
        fn summary(&self) -> String;
    }

    // News article type
    #[derive(Debug)]
    pub struct NewsArticle {
        pub headline: String,
        pub location: String,
        pub author: String,
        pub content: String,
    }

    // Tweet limits words of 0-280
    #[derive(Debug)]
    pub struct Tweet {
        pub username: String,
        pub content: String,
        pub reply: bool,
        pub retweet: bool,
    }

    impl Summary for NewsArticle {
        fn summary(&self) -> String {
            format!("{}, by {} {}", self.headline, self.author, self.location)
        }
    }

    impl Summary for Tweet {
        fn summary(&self) -> String {
            format!("{}: {}", self.username, self.content)
        }
    }

}

而且注意需要使用一个 trait 的方法时,需要将其引入当前作用域。

2.2 默认实现

为trait中的某些或者全部方法提供默认的行为是很有用的。当某个特定类型实现 trait 时3可以选择保留或者重载 trait 的方法。这是与其他语言的接口的不同之处。对于使用默认trait默认实现的类型,可以在impl块中不实现。

pub trait Summary {
    fn summarize_author(&self) -> String;

    fn summarize(&self) -> String {
        format!("(Read more from {}...)", self.summarize_author())
    }
}

默认实现允许调用相同 trait 中的其他方法,即使这些方法没有默认实现。如此,trait 可以实现很多有用的功能,而只需要实现一小部分特定的内容。

impl Summary for Tweet {
    fn summarize_author(&self) -> String {
        format!("@{}", self.username)
    }
}

3. Trait bound

使用 trait 作为参数,配合泛型进行使用,可以接受多种类型的参数,实现代码的多态化。可以指定一些实现了某个trait的类型作为参数,使用关键字impl trait:

fn foo(item: impl Summary);

这种方式适合于较短的例子,可以写作一个较为完整的语法糖,称为 Trait bound,也就是以 trait 作为类型的边界,规定一个函数或者方法可使用的类型范围。

fn foo<T: Summary>(item: T);

使用 trait bound,可以使得多参数的语法更为明了。同时可以使用+连接多个trait用以表示实现了多个trait的类型的泛型。

fn foo<T: Summary + Display, U: Summary + Clone>(i1: T, i2 U);

通过 where 简化代码

但是使用trait bound也有自己的缺点,如果泛型参数过多,那么参数列表长度很长,可读性较低。可以使用where在方法内部指定泛型参数的trait。

fn some<T, U>(T: T, u: U) -> i32 {
    where T: Display + Clone,
          U: Clone + Debug
}
PreviousRust 中的所有权Next不安全 Rust

Last updated 5 years ago

Was this helpful?