
# Rust 如何实现 Go 风格的组合继承## Go 的组合优于继承是什么Go 语言**没有**类继承它通过两种机制实现类似效果| Go 机制 | 作用 | 示例 ||---------|------|------|| **struct 嵌入embedding** | 把一个 struct 作为匿名字段嵌入自动获得其字段和方法 | type Admin struct { User } || **interface** | 只要实现了方法就满足接口隐式满足duck typing | type Reader interface { Read(p []byte) } |---## Rust 对应实现Rust 同样**没有**传统 OOP 继承但它的 Trait 泛型/组合 比 Go 更强。以下是对照### 1. Go interface → Rust Trait几乎一一对应rust// Go: type DiFactroySuite interface { ... }pub trait DiFactroySuite {fn fill_template_suite(self);fn build_suite(self, stru: StructInfo) - String;fn create_suite(self, initFile: str, content: String) - ResultString, io::Error;fn make_di_one_suite(self, dir: str, struName: str, if_force: bool);}### 2. Go 方法集method set→ Rust 的 impl 块拆分Go 允许给任何类型的方法集中继续追加方法。Rust 可以通过 **给同一个 struct 写多个 impl 块** 达到一样的效果而且能分散到不同文件中这正是你项目里 di_factroy.rs 和 di_factroy_suite.rs 正在做的rust// di_factroy.rs impl DiFactroy {pub fn parse_all_files(self, dir: str) { ... }pub fn fill_template(self) { ... }pub fn make_di(self) { ... }}// di_factroy_suite.rs impl DiFactroy {pub fn fill_template_suite(self) { ... }pub fn build_suite(self, stru: StructInfo) - String { ... }pub fn make_di_one_suite(self, dir: str, struName: str, if_force: bool) { ... }}这相当于 Go 里先定义 DiFactroy 类型然后在别的文件里继续给它写 func (d *DiFactroy) fill_template_suite()。### 3. Go struct 嵌入 → Rust 字段组合 TraitGo 写法gotype BaseRepo struct { DB *sql.DB }func (r *BaseRepo) Query(sql string) Result { ... }type UserRepo struct { BaseRepo } // 嵌入自动获得 Query 方法// userRepo.Query(...) // 可以直接调用Rust 等价写法推荐方式rust// 方式 A字段组合 手动委托显式、最安全struct BaseRepo { db: Database }impl BaseRepo {fn query(self, sql: str) - ResultRow { ... }}struct UserRepo {inner: BaseRepo, // 组合}impl UserRepo {fn query(self, sql: str) - ResultRow {self.inner.query(sql) // 委托}}Rust 更地道的方式**用 Trait 定义共享行为**rusttrait Repo {fn query(self, sql: str) - ResultRow;}struct BaseRepo { db: Database }impl Repo for BaseRepo {fn query(self, sql: str) - ResultRow { ... }}struct UserRepo { base: BaseRepo }impl Repo for UserRepo {fn query(self, sql: str) - ResultRow {self.base.query(sql)}}如果嫌委托写起来啰嗦可用 **宏** 或 **delegate crate** 自动生成。### 4. Go 接口默认嵌入 → Rust Trait 继承trait 约束Gogotype Reader interface { Read(p []byte) }type Writer interface { Write(p []byte) }type ReadWriter interface { Reader; Writer } // 嵌入两个接口Rust 完全支持rusttrait Reader { fn read(mut self, buf: mut [u8]) - Resultusize; }trait Writer { fn write(mut self, buf: [u8]) - Resultusize; }trait ReadWriter: Reader Writer {} // trait 继承任何类型只要同时实现了 Reader 和 Writer就自动满足 ReadWriter。### 5. 共享的默认实现 → Rust Trait 提供默认方法你项目里已经在用rustpub trait BaseEntity {fn if_single(self) - bool { false } // 默认实现fn init(mut self) {} // 默认实现fn auto_init(self) - bool { false }fn to_json(self) - ResultString, serde_json::Errorwhere Self: Serialize,{serde_json::to_string_pretty(self)}}rustimpl BaseEntitySingle for DiFactroy {} // 空 impl全部用默认实现这和 Go 里嵌入一个带方法的 struct 后自动获得那些方法是**同一个语义**。---## 完整对照表| Go 特性 | Rust 等价 ||---------|----------|| type T interface { ... } | trait T { ... } || type T struct { ... } 多文件方法集 | 同一个 struct 多个 impl T 块可分散在不同文件 || type B struct { A }嵌入 struct | struct B { a: A } 委托方法 / 宏自动委托 || type C interface { A; B }接口嵌入 | trait C: A B {} || 嵌入 struct 获得默认方法 | Trait 默认方法 空 impl || interface{} 空接口 | dyn Any 或泛型 T || 类型断言 v, ok : x.(T) | if let Ok(v) x.downcast_ref::T() |---## 你项目里的模式总结从 di_factroy.rs di_factroy_suite.rs 的写法看你们实际上已经在用 Rust 版的组合继承了- **DiFactroySuite trait** 相当于 Go 的 interface- **分散的 impl DiFactroy 块** 相当于 Go 的方法集可以追加- **impl BaseEntitySingle for DiFactroy {}** 空实现全用 trait 默认方法 —— 这就是 Go 里嵌入一个带完整方法的 struct 然后直接获得能力这种写法比 Go 更强的一点是Rust 的 trait 可以加 **where 约束**、关联类型、泛型编译期就能做严格检查而 Go 的 interface 检查要到运行时才 panic。