测试用例:单位说明

RustRustBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,我们将使用 Rust 提供的示例代码来研究带有虚类型参数的 Add 特性的实现。

注意:如果实验未指定文件名,你可以使用任何你想要的文件名。例如,你可以使用 main.rs,并通过 rustc main.rs &&./main 进行编译和运行。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("Rust")) -.-> rust/DataTypesGroup(["Data Types"]) rust(("Rust")) -.-> rust/FunctionsandClosuresGroup(["Functions and Closures"]) rust(("Rust")) -.-> rust/AdvancedTopicsGroup(["Advanced Topics"]) rust(("Rust")) -.-> rust/BasicConceptsGroup(["Basic Concepts"]) rust/BasicConceptsGroup -.-> rust/variable_declarations("Variable Declarations") rust/DataTypesGroup -.-> rust/floating_types("Floating-point Types") rust/DataTypesGroup -.-> rust/string_type("String Type") rust/DataTypesGroup -.-> rust/type_casting("Type Conversion and Casting") rust/FunctionsandClosuresGroup -.-> rust/function_syntax("Function Syntax") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("Expressions and Statements") rust/AdvancedTopicsGroup -.-> rust/traits("Traits") rust/AdvancedTopicsGroup -.-> rust/operator_overloading("Traits for Operator Overloading") subgraph Lab Skills rust/variable_declarations -.-> lab-99356{{"测试用例:单位说明"}} rust/floating_types -.-> lab-99356{{"测试用例:单位说明"}} rust/string_type -.-> lab-99356{{"测试用例:单位说明"}} rust/type_casting -.-> lab-99356{{"测试用例:单位说明"}} rust/function_syntax -.-> lab-99356{{"测试用例:单位说明"}} rust/expressions_statements -.-> lab-99356{{"测试用例:单位说明"}} rust/traits -.-> lab-99356{{"测试用例:单位说明"}} rust/operator_overloading -.-> lab-99356{{"测试用例:单位说明"}} end

测试用例:单位说明

可以通过使用虚类型参数实现 Add 特性来研究一种有用的单位转换方法。下面来研究 Add 特性:

// 此结构将强制要求:`Self + RHS = Output`
// 其中,如果在实现中未指定,RHS 默认值为 Self。
pub trait Add<RHS = Self> {
    type Output;

    fn add(self, rhs: RHS) -> Self::Output;
}

// `Output` 必须为 `T<U>`,以便 `T<U> + T<U> = T<U>`。
impl<U> Add for T<U> {
    type Output = T<U>;
  ...
}

完整实现:

use std::ops::Add;
use std::marker::PhantomData;

/// 创建空枚举以定义单位类型。
#[derive(Debug, Clone, Copy)]
enum Inch {}
#[derive(Debug, Clone, Copy)]
enum Mm {}

/// `Length` 是一个带有虚类型参数 `Unit` 的类型,
/// 并且不是长度类型(即 `f64`)的泛型。
///
/// `f64` 已经实现了 `Clone` 和 `Copy` 特性。
#[derive(Debug, Clone, Copy)]
struct Length<Unit>(f64, PhantomData<Unit>);

/// `Add` 特性定义了 `+` 运算符的行为。
impl<Unit> Add for Length<Unit> {
    type Output = Length<Unit>;

    // add() 返回一个包含总和的新 `Length` 结构体。
    fn add(self, rhs: Length<Unit>) -> Length<Unit> {
        // `+` 调用 `f64` 的 `Add` 实现。
        Length(self.0 + rhs.0, PhantomData)
    }
}

fn main() {
    // 指定 `one_foot` 具有虚类型参数 `Inch`。
    let one_foot:  Length<Inch> = Length(12.0, PhantomData);
    // `one_meter` 具有虚类型参数 `Mm`。
    let one_meter: Length<Mm>   = Length(1000.0, PhantomData);

    // `+` 调用我们为 `Length<Unit>` 实现的 `add()` 方法。
    //
    // 由于 `Length` 实现了 `Copy`,`add()` 不会消耗
    // `one_foot` 和 `one_meter`,而是将它们复制到 `self` 和 `rhs` 中。
    let two_feet = one_foot + one_foot;
    let two_meters = one_meter + one_meter;

    // 加法运算有效。
    println!("one foot + one_foot = {:?} in", two_feet.0);
    println!("one meter + one_meter = {:?} mm", two_meters.0);

    // 无意义的操作按预期失败:
    // 编译时错误:类型不匹配。
    //let one_feter = one_foot + one_meter;
}

总结

恭喜你!你已经完成了“测试用例:单位说明”实验。你可以在 LabEx 中练习更多实验来提升你的技能。