Get the FREE Ultimate OpenClaw Setup Guide →

rust-code-debugger

npx machina-cli add skill UtakataKyosui/C2Lab/rust-debugger --openclaw
Files (1)
SKILL.md
3.0 KB

Rust Code Debugger

Instructions

  1. Understand the user request:
    Identify whether the problem is a compiler error, borrow-checker conflict, runtime panic, undefined behavior risk, or incorrect logic. Determine which Rust concepts (ownership, lifetimes, traits, concurrency, async, interior mutability, error handling) are involved.

  2. Reproduce the mental execution flow:
    Walk through the code step by step, tracking:

    • variable bindings and moves
    • reference creation and lifetimes
    • mutation paths (mutable aliasing, interior mutability)
    • control-flow branches
    • async .await suspension points
    • thread boundaries and Send / Sync requirements
  3. Identify the root cause with precision:
    Provide a detailed explanation grounded in Rust’s guarantees:

    • why the borrow checker rejected a pattern
    • why a value was moved earlier than expected
    • why a panic occurred (unwrap, index out of bounds, threading, channel behavior, etc.)
    • why lifetimes cannot be inferred
    • why trait resolution fails
    • why async runtime ordering causes issues
  4. Provide an idiomatic and minimal fix:
    Offer a correction that adheres to Rust best practices, such as:

    • replacing &mut conflicts with scoped blocks or .split_at_mut()
    • using Option or Result instead of panicking APIs
    • using iterator combinators instead of manual loops when appropriate
    • leveraging ? for clean error propagation
    • using Arc<Mutex<T>> or Arc<RwLock<T>> for shared mutable state across threads
    • applying Cow<'a, T> for flexible ownership
    • introducing helper structs or newtypes to reflect invariant boundaries

    Keep the fix minimal but idiomatic, avoiding unnecessary complexity.

  5. Suggest robust alternatives when relevant:
    If the user’s current approach is fragile or overly complex, gently recommend stronger patterns:

    • Use RAII guards for cleanup
    • Introduce separate data structures to enforce invariants
    • Replace manual state machines with enums
    • Use async channels or tasks instead of locks
    • Avoid premature cloning; prefer borrowing when possible
    • Structure code into smaller functions to reduce lifetime complexity
  6. Recommend preventive strategies:
    Provide practical guidance for long-term improvement, including:

    • writing smaller functions to help lifetime inference
    • adding #[derive(Debug)] for easier inspection
    • avoiding unnecessary clone() calls
    • adopting clippy (cargo clippy) and rustfmt
    • using feature flags, miri, and cargo-udeps to maintain healthy code
    • structuring modules around ownership boundaries
  7. Maintain clarity and educational value:
    Every explanation should not only fix the bug but help the user understand how Rust’s model leads to safer and more predictable code.

Source

git clone https://github.com/UtakataKyosui/C2Lab/blob/main/plugins/rust/skills/rust-debugger/SKILL.mdView on GitHub

Overview

Diagnoses and fixes Rust compile errors, borrow-checker conflicts, runtime panics, and logic bugs with clear explanations and idiomatic solutions. It emphasizes Rust's ownership, lifetimes, and concurrency rules to deliver minimal, robust fixes.

How This Skill Works

It analyzes the provided code by walking through ownership moves, borrows, lifetimes, and async boundaries, reproducing potential failure paths. It identifies root causes—such as moved values, conflicting borrows, or panics—and suggests minimal, idiomatic fixes and practical alternatives used in real Rust code.

When to Use It

  • When you encounter a Rust compiler error or borrow-checker conflict.
  • When a runtime panic occurs (unwrap, index, threading, or channel behavior).
  • When a value is moved earlier than it's used, causing borrow or use-after-move errors.
  • When lifetimes are elusive or trait resolution fails.
  • When you need idiomatic patterns for shared state, error handling, or safe concurrency.

Quick Start

  1. Step 1: Paste your Rust code and the exact error message or failing behavior.
  2. Step 2: Let the debugger walk through ownership, lifetimes, and async boundaries to identify root causes.
  3. Step 3: Apply the idiomatic fix and verify with tests; review the explanation and preventive tips.

Best Practices

  • Isolate fixes with scoped blocks or split_at_mut to avoid &mut aliasing.
  • Prefer Option/Result and ? instead of unwraps or panicking calls.
  • Use iterator adapters and combinators rather than manual loops.
  • Propagate errors cleanly and keep functions small to aid lifetime inference.
  • Choose appropriate synchronization (Arc<Mutex<T>> or Arc<RwLock<T>>) for shared mutable state across threads.

Example Use Cases

  • Borrow-checker conflict in a for loop with mutable borrows.
  • Panic caused by unwrap on None in an async task.
  • Move occurs before use in a struct initialization.
  • Index out of bounds when slicing a vector.
  • Sharing mutable state across threads with Arc and Mutex.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers