MoonBit月兔编程语言 现代编程思想 第九课 接口
-> String ☐ ..... • 我们将这类函数称为方法 ## 接口 Trait ## • 我们通过接口定义一系列方法的实现需求 1. trait Compare { 2. compare(Self, Self) -> Int // Self代表实现该接口的类型 3. } 4. trait Default { 5. default() -> Self 6. } ## • 月兔中的接口是结构化的 ◦ 无需声明为特定的接口实现方法,类型本身实现方法即可 ## 接口 Trait • 我们可以在泛型的参数上添加接口的要求 ☐ 限制参数的类型: <类型参数> : <接口> ☐ 在函数中使用接口定义的方法: <类型参数>::<方法名> 1. fn make[T: Default]( ) -> Queue[T] ☐ ☐ ☐ ☐ ☐ ☐ ☐ ☐ 001\_hello.mbt 1 Type BoxedInt does not implement trait Default: method default is missing [Ln 16, Col 28] ## 接口 Trait 1. fn insert[T : Compare](tree : Tree[T], value : T) -> Tree[T]0 码力 | 16 页 | 346.04 KB | 2 年前3
Rust 程序设计语言 简体中文版 1.85.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 10. 泛型、Trait 和生命周期 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 10.2. Trait:定义共同行为 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 15.2. 使用 Deref Trait 将智能指针当作常规引用处理 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 15.3. 使用 Drop Trait 运行清理代码 . . . . . . . . . . . . . . . . . .0 码力 | 562 页 | 3.23 MB | 8 月前3
Comprehensive Rust(Persian ) 2024128 123 ..... 19.8.1 126 ..... 20 126 ..... Box20.1 128 ..... Rc 20.2 129 ..... Owned Trait Objects 20.3 130 ..... 20.4 132 ..... 20.4.1 136 ..... VI 137 ..... 21 138 ..... (Borrowing) interface interface } trait Pet .Return a sentence from this pet // ;fn talk(&self) -> String .Print a string to the terminal greeting this pet // ; (fn greet(&self) { trait ☐☐ .☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐ ☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐ #### Traits ☐☐☐☐ ☐☐☐☐ 13.2.1 } trait Pet ;fn talk(&self) -> String } (fn greet(&self ;(()self.talk ,"{} ☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐ 0 码力 | 393 页 | 987.97 KB | 1 年前3
The Rust Programming Language,2nd EditionNext, we’re adding another use line: use rand::Rng. Rng is a trait that defines methods that random number generators implement, and this trait must be in scope for us to use those methods. Chapter 10 will Next, we call the gen_range method on the random number generator. This method is defined by the Rng trait that we brought into scope with the use rand: :Rng statement. The gen_range method takes two numbers note: move occurs because `s1` has type `std::string: :String`, which does not implement the `Copy` trait If you’ve heard the terms “shallow copy” and “deep copy” while working with other languages, the concept0 码力 | 617 页 | 1.54 MB | 2 年前3
Rust 程序设计语言简体中文版9.2. 用 Result 处理可恢复的错误 ..... 189 9.3. 要不要 panic! ..... 199 10. 泛型、Trait 和生命周期 ..... 204 10.1. 泛型数据类型 ..... 207 10.2. Trait: 定义共同行为 ..... 215 10.3. 生命周期确保引用有效 ..... 226 11. 编写自动化测试 ..... 240 369 15. 智能指针 ..... 370 15.1. 使用 Box指向堆上数据 ..... 371 15.2. 使用 Deref Trait 将智能指针当作常规引用处理 ..... 377 15.3. 使用 Drop Trait 运行清理代码 ..... 384 15.4. Rc 引用计数智能指针 ..... 388 15.5. RefCell 与内部可变性模式 Traits 的可扩展并发 ..... 433 17. Rust 的面向对象编程特性 ..... 435 17.1. 面向对象语言的特点 ..... 436 17.2. 顾及不同类型值的 trait 对象 ..... 440 17.3. 面向对象设计模式的实现 ..... 446 18. 模式与模式匹配 ..... 462 18.1. 所有可能会用到模式的位置 ..... 463 0 码力 | 600 页 | 12.99 MB | 2 年前3
Rust 语言学习笔记3.1.4 Copy 特性.....32 3.1.5 浅拷贝与深拷贝.....33 3.1.6 高级 copy.....33 3.1.7 Copy trait 与 Clone trait.....33 3.1.8 高级 move.....34 3.2 引用和借用.....35 3.2.1 借用和引用的规则.....35 3.2.2 4.3.trait ..... 48 4.3.1 泛型参数约束 ..... 48 4.3.2 trait 与内置类型 ..... 49 4.3.3 trait 默认实现 ..... 49 4.3.4 trait 的继承 ..... 50 4.3.5 derive 属性 ..... 50 4.3.6 impl Trait ..... 50 4.3.7 trait 对象 . .... 52 4.3.8 trait 定义中的生命周期和可变性声明 ..... 52 第五章 属性与 Cargo 配置 ..... 53 5.1 属性 ..... 53 5.1.1 属性的语法 ..... 53 5.1.2 几种常见的属性 ..... 54 5.2 cargo 参数配置 ..... 57 5.2.1 package 配置 ..... 57 5.2.20 码力 | 117 页 | 2.24 MB | 2 年前3
Comprehensive Rust(English) 20241277 13.2.2 Supertraits 78 13.2.3 Associated Types 78 13.3 Deriving 79 13.4 Exercise: Logger Trait 79 13.4.1 Solution 80 IV Day 2: Afternoon 82 14 Welcome Back 83 15 Generics 84 Functions 84 15.2 Generic Data Types 85 15.3 Generic Traits 86 15.4 Trait Bounds 86 15.5 impl Trait 87 15.6 dyn Trait 88 15.7 Exercise: Generic min 89 15.7.1 Solution 90 16 Standard Library Operators 100 17.3 From and Into 101 17.4 Casting 102 17.5 Read and Write 102 17.6 The Default Trait 103 17.7 Closures 104 17.8 Exercise: ROT13 105 17.8.1 Solution 106 V Day 3: Morning 1080 码力 | 382 页 | 1.00 MB | 1 年前3
Comprehensive Rust ?73 13.2.2 Supertraits 74 13.2.3 Associated Types 74 13.3 Deriving 75 13.4 Exercise: Logger Trait 75 13.4.1 Solution 76 IV Day 2: Afternoon 78 14 Welcome Back 79 15 Generics 80 Functions 80 15.2 Generic Data Types 81 15.3 Generic Traits 81 15.4 Trait Bounds 82 15.5 impl Trait 83 15.6 dyn Trait 84 15.7 Exercise: Generic min 85 15.7.1 Solution 86 16 Standard Library Operators 96 17.3 From and Into 97 17.4 Casting 98 17.5 Read and Write 98 17.6 The Default Trait 99 17.7 Closures 100 17.8 Exercise: ROT13 101 17.8.1 Solution 102 V Day 3: Morning 1040 码力 | 378 页 | 1009.46 KB | 2 年前3
Comprehensive Rust(简体中文) 20241213.1 方法 ..... 67 13.2 特征(Trait) ..... 68 13.2.1 实现 Unsafe Trait ..... 69 13.2.2 更多 trait ..... 69 13.2.3 共享类型 ..... 70 13.3 派生特征 ..... 71 13.4 Exercise: Logger Trait ..... 71 13.4.1 解答 .... 74 14.1 泛型函数 ..... 74 14.2 泛型类型 ..... 75 14.3 泛型 ..... 75 14.4 特征边界 ..... 76 14.5 impl Trait ..... 77 14.6 练习:通用 min 函数 ..... 77 14.6.1 解答 ..... 78 IV 第二天:下午 ..... 79 15 Welcome Rust 174 30.2 解引用裸指针 175 30.3 可变的静态变量 175 30.4 联合体 176 30.5 Unsafe 函数 177 30.6 实现 Unsafe Trait 178 30.7 安全 FFI 封装容器 179 30.7.1 解答 181 IX Android 185 31 欢迎来到 Android 中的 Rust 1860 码力 | 359 页 | 1.33 MB | 1 年前3
Comprehensive Rust(Português do Brasil) 202412Supertraits ..... 78 13.2.3 Tipos Associados ..... 78 13.3 Derivando ..... 79 13.4 Exercício: Trait de Logger ..... 79 13.4.1 Solução ..... 80 IV Dia 2: Tarde ..... 82 14 Bem-vindos de volta Genéricos ..... 85 15.3 Traits Genéricos ..... 85 15.4 Trait Bounds (Limites de Trait) ..... 86 15.5 impl Trait ..... 87 15.6 dyn Trait ..... 88 15.7 Exercício: min Genérico ..... 89 15.7.1 100 17.3 From e Into ..... 101 17.4 Conversões ..... 102 17.5 Read e Write ..... 102 17.6 O Trait Default ..... 103 17.7 Closures ..... 104 17.8 Exercício: ROT13 ..... 105 17.8.1 Solução ..0 码力 | 389 页 | 1.05 MB | 1 年前3
共 310 条
- 1
- 2
- 3
- 4
- 5
- 6
- 31













