skills/rust-safety-performance/SKILL.md
Rust 安全性與效能最佳實踐。用於 unsafe 使用規範、soundness、記憶體安全、效能優化、profiling、allocator 選擇。
npx skillsauth add vincent119/ai-rules-kit rust-safety-performanceInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
/// # Safety
///
/// `ptr` 必須指向有效的、已初始化的 `T` 值,
/// 且在此函式呼叫期間不得有其他 mutable reference。
pub unsafe fn read_raw<T>(ptr: *const T) -> T {
// SAFETY: 呼叫者保證 ptr 有效且對齊
unsafe { ptr.read() }
}
unsafe block 必須有 // SAFETY: 註解unsafe fn 必須有 # Safety 文件段落// 錯誤:unsound — 允許建立無效 UTF-8 的 String
pub fn from_bytes(bytes: Vec<u8>) -> String {
unsafe { String::from_utf8_unchecked(bytes) } // 呼叫者可能傳入非 UTF-8
}
// 正確:驗證後再轉換
pub fn from_bytes(bytes: Vec<u8>) -> Result<String, std::string::FromUtf8Error> {
String::from_utf8(bytes)
}
// Application binary 使用 mimalloc
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
// 避免在 hot path 分配
fn process_batch(items: &[Item], buffer: &mut Vec<u8>) {
buffer.clear(); // 重用 buffer,不重新分配
for item in items {
item.serialize_into(buffer);
}
}
// 使用 SmallVec 避免小集合的 heap 分配
use smallvec::SmallVec;
fn collect_ids(items: &[Item]) -> SmallVec<[u64; 8]> {
items.iter().map(|i| i.id).collect()
}
// 錯誤:不必要的 clone
fn process(data: &str) -> String {
let owned = data.to_string(); // 不需要所有權時避免
owned.trim().to_string()
}
// 正確:借用到底
fn process(data: &str) -> &str {
data.trim()
}
// 批次處理而非逐一
async fn process_events(rx: &mut Receiver<Event>) {
let mut batch = Vec::with_capacity(64);
loop {
// 收集一批再處理
batch.clear();
if rx.recv_many(&mut batch, 64).await == 0 {
break;
}
process_batch(&batch).await;
}
}
| 工具 | 用途 |
|------|------|
| cargo flamegraph | CPU profiling |
| cargo bench (criterion) | Micro-benchmark |
| tokio-console | Async task 監控 |
| dhat | Heap allocation profiling |
| valgrind --tool=callgrind | 指令級 profiling |
# Cargo.toml - 開發時加速編譯
[profile.dev]
opt-level = 0
debug = true
[profile.dev.package."*"]
opt-level = 2 # 依賴用 O2 編譯
# Release 最佳化
[profile.release]
opt-level = 3
lto = "thin"
codegen-units = 1
strip = true
tools
基於 SLA/SLO 量化評估事故影響的計算模型與業務影響矩陣。適用於「SLA 影響」、「SLO 違反」、「影響評估」、「營收損失估算」、「Error Budget」、「可用性計算」、「事故成本評估」等量化事故業務影響的任務。強化 impact-assessor 的評估能力。注意:事故原因分析與改善規劃不在此技能範圍內。
research
根因分析(RCA)方法論詳細指南。提供 5 Whys、Fishbone 圖、Fault Tree Analysis、變更分析等結構化 RCA 技術,以及認知偏誤防範清單。適用於「根因分析」、「RCA」、「5 Whys」、「魚骨圖」、「Fault Tree」、「原因分析方法論」、「變更分析」等事故原因分析任務。強化 root-cause-investigator 的分析能力。注意:時間軸重建與改善規劃不在此技能範圍內。
testing
事故事後分析(Postmortem)完整流程。協調 7 個執行階段:資訊收集 → 時間軸重建 → 根因分析 → 影響評估 → 改善規劃 → 報告審查 → 整合報告,最終產出完整的 Postmortem 報告。適用於「寫事故報告」、「post-incident 分析」、「RCA 報告」、「事故時間軸整理」、「建立改善措施」等請求。注意:即時 Incident Response(on-call)、監控系統設定、告警配置不在此技能範圍內。
content-media
投影片版面模式庫。提供 20 種投影片類型的最佳版面配置、格線系統、色彩與字型設計 Token。適用於「投影片版面」、「Slide Layout」、「設計系統」、「格線」、「字型」、「色彩規範」等投影片視覺設計任務。強化 visual-designer 的設計能力。注意:PPT/Keynote 檔案直接輸出不在此技能範圍內。