知识卡片

普通读书笔记卡 · 10647.a · 生成模型:未记录

同名类型参数绑定同类

理解等级:中级

同一个泛型类型参数重复出现,表示一次实例化或调用里相应位置用同一个具体类型。Point 的两个坐标要同型,Point 则能分别确定。对枚举也适用,Result 让成功数据和失败数据各有类型。增加参数前应识别哪些类型真正独立。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 泛型类型、trait 与生命周期 In Struct Definitions · 查看原书依据

      Note that because we’ve used only one generic type to define Point<T>, this definition says that the Point<T> struct is generic over some type T, and the fields x and y are both that same type, whatever that type may be. If we create an instance of a Point<T> that has values of different types, as in Listing 10-7, our code won’t compile.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第10章 泛型类型、trait 与生命周期(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c10.xhtml`,块 0058) - 结论依据:Point<T> 混用整数与小数失败,说明共用参数约束两个字段同型。 - 原始内容:Note that because we’ve used only one generic type to define Point<T>, this definition says that the Point<T> struct is generic over some type T, and the fields x and y are both that same type, whatever that type may be. If we create an instance of a Point<T> that has values of different types, as in Listing 10-7, our code won’t compile.