Rust简明教程第五章-Trait 特质

观看B站软件工艺师杨旭的rust教程学习记录,有删减有补充

Trait 特质

  • trait:共同行为,与其他类型共享的功能
  • trait bounds(约束):将泛型类型参数指定为实现了指定行为的类型
  • trait只有方法签名没有具体实现
  • 实现trait类型必须实现其方法
rust 复制代码
pub trait MyTrait {
    fn method1(&self);
    fn method2(&self, value: u32) -> u32;
}
struct MyStruct;

impl MyTrait for MyStruct {
    //实现trait
    fn method1(&self) {
        println!("方法1");
    }

    fn method2(&self, value: u32) -> u32 {
        println!("方法2和参数:{}", value);
        value * 2
    }
}
fn main() {
    let my_struct = MyStruct;
    my_struct.method1();//方法1
    let result = my_struct.method2(5);//方法2和参数:5
    println!("结果: {}", result);//结果: 10
}

trait bounds约束

  • 实现trait的前提是trait是在本地crate定义的
  • 默认实现的方法可以调用trait中其他方法,即使这些方法没有默认实现
rust 复制代码
trait MyTrait {
    fn method1(&self) {//method1实现了trait
        println!("调用了 method1");//调用了 method1
        self.method2();
    }
    fn method2(&self);//method2没有实现trait
}
struct MyStruct;
impl MyTrait for MyStruct {
    fn method2(&self) {
        println!("调用了 method2");//调用了 method2
    }
}
fn main() {
    let my_struct = MyStruct;
    my_struct.method1();
}

trait中作为参数

rust 复制代码
trait MyTrait {
    fn method(&self);
}
struct MyStruct;
impl MyTrait for MyStruct {
    fn method(&self) {
        println!(" 方法");
    }
}
// 使用 `impl trait` 方法
fn perform_action_with_impl_trait(item: impl MyTrait) {
    item.method();
}
// 使用 `trait bound` 方法
fn perform_action_with_trait_bound<T: MyTrait>(item: T) {
    item.method();
}
fn main() {
    let my_struct = MyStruct;

    // 使用 `impl trait` 方法调用函数
    // perform_action_with_impl_trait(my_struct);

    // 使用 `trait bound` 方法调用函数
    perform_action_with_trait_bound(my_struct);
}

where子句指定多个trait bound

rust 复制代码
//声明trait
trait Display {
    fn display(&self);
}

trait Debug {
    fn debug(&self);
}

trait Clone {
    fn clone(&self);
}
//声明结构体
struct MyStruct1;
struct MyStruct2;
//实现trait方法
impl Display for MyStruct1 {
    fn display(&self) {
        println!("Displaying Trait");
    }
}
impl Debug for MyStruct1 {
    fn debug(&self) {
        println!("Debugging Trait");
    }
}
impl Clone for MyStruct2 {
    fn clone(&self) {
        println!("Clone Trait");
    }
}
impl Debug for MyStruct2 {
    fn debug(&self) {
        println!("Clone Trait");
    }
}
//指定多个trait
fn perform_action<T, U>(item1: T, item2: U)
where
    T: Display + Debug,
    U: Clone + Debug,
{
    item1.display(); // Displaying Trait
    item1.debug(); // Debugging Trait
    item2.clone(); // Clone Trait
}
fn main() {
    let my_struct1 = MyStruct1;
    let my_struct2 = MyStruct2;
    perform_action(my_struct1, my_struct2);
}

实现trait作为返回类型

rust 复制代码
trait Shape {
    fn area(&self) -> f64;
}

struct Rectangle {
    width: f64,
    height: f64,
}

impl Shape for Rectangle {
    fn area(&self) -> f64 {
        self.width * self.height //返回矩形面积
    }
}

struct Circle {
    radius: f64,
}

impl Shape for Circle {
    fn area(&self) -> f64 {
        std::f64::consts::PI * self.radius.powi(2) //返回圆形面积
    }
}

fn create_shape(is_rectangle: bool) -> Box<dyn Shape> {
    if is_rectangle {
        Box::new(Rectangle {
            width: 5.0,
            height: 3.0,
        })
    } else {
        Box::new(Circle { radius: 2.0 })
    }
}

fn main() {
    let rectangle = create_shape(true);
    let circle = create_shape(false);

    println!("矩形面积: {}", rectangle.area());//矩形面积: 15
    println!("圆形面积: {}", circle.area());//圆形面积: 12.566370614359172
}

有条件的实现方法

rust 复制代码
trait Printable {
    fn print(&self);
}

struct Number {
    value: i32,
}

impl<T> Printable for T
where
    T: std::fmt::Display,
{
    fn print(&self) {
        println!("Value: {}", self);//Value: Hello, world!
    }
}

fn main() {
    let number = Number { value: 42 };
    // number.print();

    let string = "Hello, world!";
    string.print();//只有实现了std::fmt::Display trait的类型才能实现
}

trait定义关联类型,实现该trait时被具体化

rust 复制代码
struct MyIterator {
    data: Vec<i32>,
    index: usize,
}
impl Iterator for MyIterator {
    type Item = i32;//具体化
    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.data.len() {
            let item = self.data[self.index];
            self.index += 1;
            Some(item)
        } else {
            None
        }
    }
}
fn main() {
    let my_iterator = MyIterator {
        data: vec![1, 2, 3, 4, 5],
        index: 0,
    };

    for item in my_iterator {
        println!("元素: {}", item);//元素: 1元素: 2元素: 3元素: 4元素: 5
    }
}

balanket implementations 覆盖实现

为满足trait bound的所有类型实现trait

rust 复制代码
impl<T: Display> ToString for T {//对所有实现两Display的trait实现tostring trait
    // --snip--
}
相关推荐
码事漫谈4 小时前
以后人人都可以成为导演
后端
Resean02234 小时前
Spring 源码分析(二)配置类的解析与扫描
java·后端·spring
一只爱撸猫的程序猿4 小时前
五周、两个开源仓库、九次拦截之后:AI 时代程序员缺的到底是什么
人工智能·后端·开源
ShineWinsu5 小时前
对于Qt:QFormLayout常见属性以及方法的解析
开发语言·c++·qt·面试·客户端·表单·qformlayout
北城笑笑5 小时前
Vue 110 ,若依SSE流式接口限次空500排查与解决
前端·javascript·vue.js·后端·vue
ServBay6 小时前
Local harness 将退役,VS Code 本地开发要不要搬到云端?
后端·ai编程·visual studio code
ashiho6 小时前
【LeetCode hot 100】3. 无重复字符的最长子串 (三种解法)
java·数据结构·后端·算法·leetcode·滑动窗口
张一凡937 小时前
你的 AGENTS.md,真的管到模型写代码了吗?
前端·后端·agent
00后程序员7 小时前
iOS 实时活动怎么开发,Windows 上用 KXBuild 和 ActivityKit 写灵动岛
后端·ios
程序员cxuan7 小时前
推荐 10 个 WorkBuddy 中的邪修用法
人工智能·后端·程序员