跳转至

Rust 构建与修复

此命令会调用 rust-build-resolver 代理,以最小改动增量修复 Rust 构建错误。

此命令的工作方式

  1. 执行诊断:运行 cargo check、cargo clippy、cargo fmt --check
  2. 解析错误:识别错误代码和受影响文件
  3. 增量修复:一次修复一个错误
  4. 验证每次修复:每次变更后重新运行 cargo check
  5. 报告摘要:显示已修复项和剩余项

使用时机

使用 /rust-build 的情况: - cargo build 或 cargo check 因错误失败时 - cargo clippy 报告警告时 - 借用检查器或生命周期错误阻止编译时 - Cargo 依赖解析失败时 - 拉取破坏构建的更改后

执行的诊断命令

# プライマリビルドチェック
cargo check 2>&1

# リントと提案
cargo clippy -- -D warnings 2>&1

# フォーマットチェック
cargo fmt --check 2>&1

# 依存関係の問題
cargo tree --duplicates

# セキュリティ監査(利用可能な場合)
if command -v cargo-audit >/dev/null; then cargo audit; else echo "cargo-audit not installed"; fi

会话示例

User: /rust-build

Agent:
# Rustビルド解決

## 初期診断

```bash
$ cargo check
error[E0502]: cannot borrow `map` as mutable because it is also borrowed as immutable
  --> src/service/cache.rs:25:5
error[E0308]: mismatched types
  --> src/handler/api.rs:42:12
error[E0425]: cannot find value `db` in this scope
  --> src/handler/api.rs:58:9
```

検出されたエラー: 3

## 修正 1: ボローチェッカーエラー

File: src/service/cache.rs:25
Error: E0502 — イミュータブルとしてもボローされているため、`map`をミュータブルとしてボローできない
Cause: ミュータブルなinsert中にイミュータブルなボローがまだアクティブ

```rust
// 変更: ミュータブルアクセス前にイミュータブルなボローを終了するよう再構築
if !map.contains_key("key") {
    map.insert("key".into(), default);
}
```

```bash
$ cargo check
# 残りエラー: 2
```

## 修正 2: 型の不一致

File: src/handler/api.rs:42
Error: E0308 — mismatched types: expected `usize`, found `&str`

```rust
// 変更前
let count = params.get("count");
// 変更後
let count: usize = params.get("count")
    .and_then(|s| s.parse().ok())
    .unwrap_or(0);
```

```bash
$ cargo check
# 残りエラー: 1
```

## 修正 3: 未解決の名前

File: src/handler/api.rs:58
Error: E0425 — cannot find value `db` in this scope
Cause: importの欠落

```rust
// 追加
use crate::db;
```

```bash
$ cargo check
# ビルド成功!
```

## 最終検証

```bash
$ cargo clippy -- -D warnings
# 警告なし

$ cargo test
running 6 tests
test service::cache::tests::test_insert ... ok
test service::cache::tests::test_get ... ok
test handler::api::tests::test_parse_count ... ok
test handler::api::tests::test_missing_count ... ok
test handler::api::tests::test_db_import ... ok
test handler::api::tests::test_response ... ok

test result: ok. 6 passed; 0 failed; 0 ignored
```

## サマリー

| メトリクス | 件数 |
|-----------|------|
| 修正されたビルドエラー | 3 |
| 修正されたClippy警告 | 0 |
| 変更されたファイル | 2 |
| 残りの問題 | 0 |

ビルドステータス: SUCCESS

常见修复的错误

错误 典型修复
cannot borrow as mutable 重构以先结束不可变借用。仅在合理时使用 clone
does not live long enough 使用拥有类型或添加生命周期注解
cannot move out of 重构以获取所有权。仅将 clone 作为最后手段
mismatched types 添加 .into()、as 或显式转换
trait X not implemented 添加 #[derive(Trait)] 或手动实现
unresolved import 添加到 Cargo.toml 或修正 use 路径
cannot find value 添加 import 或修正路径

修复策略

  1. 先处理构建错误 — 代码必须能够编译
  2. 再处理 Clippy 警告 — 修复可疑结构
  3. 最后处理格式 — 符合 cargo fmt
  4. 一次一个修复 — 验证每次变更
  5. 最小改动 — 不重构,仅修复

停止条件

代理在以下情况下停止并报告: - 尝试 3 次后同一错误仍然存在 - 修复引入了更多错误 - 需要架构变更 - 借用检查器错误需要重新设计数据所有权

  • /rust-test — 构建成功后运行测试
  • /rust-review — 审查代码质量
  • verification-loop 技能 — 完整验证循环
  • 代理:agents/rust-build-resolver.md
  • 技能:skills/rust-patterns/

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 ev/ECC