🎯

rust-zero-cost

🎯Skill

from huiali/rust-skills

VibeIndex|
What it does

Analyzes and optimizes Rust generic abstractions and dispatch strategies for zero-cost performance and compile-time flexibility.

📦

Part of

huiali/rust-skills(30 items)

rust-zero-cost

Installation

📋 No install commands found in docs. Showing default command. Check GitHub for actual instructions.
Quick InstallInstall with npx
npx skills add huiali/rust-skills --skill rust-zero-cost
3Installs
-
AddedFeb 4, 2026

Skill Details

SKILL.md

"零成本抽象与泛型专家。处理泛型, trait, monomorphization, static dispatch, dynamic dispatch, impl Trait, dyn Trait, 泛型, 特征, 单态化, 零成本抽象"

Overview

# 零成本抽象与泛型

核心问题

需要编译时多态还是运行时多态?

选择正确的抽象层次直接影响性能。

---

泛型 vs Trait Object

| 特性 | 泛型 (static dispatch) | trait object (dynamic dispatch) |

|-----|----------------------|--------------------------------|

| 性能 | 零开销 | vtable 查找 |

| 代码大小 | 可能膨胀 | 更小 |

| 编译时间 | 更长 | 更短 |

| 灵活性 | 类型必须已知 | 运行时决定 |

| 异构集合 | 不支持 | Vec> |

---

何时用泛型

```rust

// 类型在编译时已知

fn process(item: T) {

item.process();

}

// 返回同一类型

fn create_processor() -> impl Processor {

// 返回具体类型

}

// 多个类型参数

fn combine(a: A, b: B) -> String {

format!("{} and {}", a, b)

}

```

---

何时用 trait object

```rust

// 运行时决定类型

trait Plugin {

fn run(&self);

}

struct PluginManager {

plugins: Vec>,

}

// 异构集合

let handlers: Vec> = vec![

Box::new(HttpHandler),

Box::new(GrpcHandler),

];

```

---

对象安全规则

```rust

// ❌ 不是对象安全的

trait Bad {

fn create(&self) -> Self; // 返回 Self

fn method(&self, x: Self); // 参数有 Self

}

// ✅ 对象安全

trait Good {

fn name(&self) -> &str;

}

```

---

impl Trait vs dyn Trait

```rust

// impl Trait:返回具体类型(静态分发)

fn create_processor() -> impl Processor {

HttpProcessor

}

// dyn Trait:返回 trait object(动态分发)

fn create_processor() -> Box {

Box::new(HttpProcessor)

}

```

---

性能影响

```rust

// 泛型:每个类型生成一份代码

fn process(item: T) {

item.method();

}

// 编译后:

// fn process_Http(item: Http) { ... }

// fn process_Ftp(item: Ftp) { ... }

// trait object:单一路径

fn process(item: &dyn Trait) {

item.method(); // 通过 vtable 调用

}

```

---

常见错误

| 错误 | 原因 | 解决 |

|-----|------|-----|

| E0277 | 缺少 trait bound | 添加 T: Trait |

| E0038 | trait object 不安全 | 检查对象安全规则 |

| E0308 | 类型不匹配 | 统一类型或用泛型 |

| E0599 | 未找到实现 | 实现 trait 或检查约束 |

---

优化策略

  1. 热点代码用泛型 - 消除动态分发开销
  2. 插件系统用 dyn - 灵活性优先
  3. 小集合用泛型 - 避免 Box 分配
  4. 大集合用 dyn - 减少代码膨胀

More from this repository10

🎯
rust-skill🎯Skill

Provides expert Rust programming assistance, solving compilation errors, ownership, lifetimes, concurrency, and performance optimization challenges.

🎯
rust-skill-index🎯Skill

Indexes and provides quick navigation for 35 Rust skills across core, advanced, and expert categories.

🎯
rust-async🎯Skill

Handles advanced Rust async patterns like Stream processing, backpressure, select, cancellation, and concurrency management with Tokio.

🎯
rust-error🎯Skill

Expertly handles Rust error scenarios by providing comprehensive guidance on Result, Option, error types, propagation, and panic strategies.

🎯
rust-anti-pattern🎯Skill

Identifies and helps refactor Rust anti-patterns like unnecessary cloning, unwrapping, and inefficient iterations to improve code quality.

🎯
rust-web🎯Skill

Builds robust Rust web services using frameworks like axum and actix, handling routing, database interactions, and API design with type-safe, performant code.

🎯
rust-concurrency🎯Skill

Expertly handles Rust concurrency challenges by safely managing threads, async operations, and preventing race conditions and deadlocks.

🎯
rust-ownership🎯Skill

Skill

🎯
rust-middleware🎯Skill

Implements comprehensive Rust web middleware for request tracing, CORS configuration, rate limiting, and advanced middleware patterns.

🎯
rust-mutability🎯Skill

Expertly manages Rust mutability challenges, resolving borrowing conflicts and providing safe interior mutability strategies across different contexts.