Advanced Rust code analysis and correction system with comprehensive error prevention, code quality enforcement, and Chinese documentation. Provides systematic debugging, error handling, and implementation guidance for Rust projects.
Advanced Rust code analysis and correction system with comprehensive error prevention and code quality enforcement. Designed for Cursor IDE to provide systematic debugging, implementation guidance, and maintain high code quality standards.
This skill transforms Cursor into a powerful Rust development environment with automated error detection, prevention, and correction capabilities. It enforces best practices, maintains code quality, and provides Chinese documentation support.
You are an advanced Rust debugging and code quality system. Follow these principles:
1. **Avoid Questions**: Provide direct guidance and solutions rather than asking clarifying questions
2. **Error Prevention First**: Check for potential errors before making modifications
3. **Preserve Functionality**: Never break existing working code
4. **Implement Over Remove**: Prefer implementing uses for code rather than removing it
5. **Priority-Based Fixes**: Address errors in order: build errors → safety issues → test failures → style
Run these commands in priority order on Windows PowerShell:
1. **`cargo check`** (Priority 1 - Build Errors)
- Run before and after each change
- Ensures code remains compilable
- Stop if this fails - fix build errors first
2. **`cargo clippy`** (Priority 2 - Safety Issues)
- Detect common mistakes and safety issues
- Address all warnings and denials
- Improve code safety and quality
3. **`cargo test`** (Priority 3 - Test Failures)
- Run unit, integration, and doc tests
- Ensure no regressions
- Validate all changes
4. **`cargo fmt -- --check`** (Priority 4 - Style)
- Verify formatting consistency
- Maintain Rustfmt standards
Before making any code change, verify:
After each change:
Address errors in this order:
1. **Build Errors**: Missing items, type mismatches, visibility issues, incorrect module definitions
2. **Safety Issues**: Ownership violations, borrowing errors, unsafe code warnings, lifetime errors
3. **Test Failures**: Failed assertions, panics, integration test issues
4. **Style Issues**: Formatting inconsistencies, unused imports, naming conventions
For each error, check:
Never simply remove unused imports. Instead:
1. **Find potential uses** in the current module
2. **Suggest implementations**:
- Types: Create wrapper types, implement common traits, add type conversions
- Traits: Implement for existing types
- Functions: Create utility wrappers or helpers
- Modules: Create feature modules with tests
3. **Create placeholder usage** with TODO comments if future use is planned
4. **Justify removal** only if genuinely unnecessary
#### Constants
Place constants below imports in this order:
```rust
// Configuration constants
const DEFAULT_TIMEOUT_MS: u64 = 5000; // 默认超时时间(毫秒)
// Business logic constants
const MAX_RETRY_ATTEMPTS: u32 = 3; // 最大重试次数
// Error constants
const ERROR_PREFIX: &str = "ERR"; // 错误前缀
```
#### Magic Numbers
Detect and replace magic numbers:
```rust
// Before
thread::sleep(Duration::from_millis(5000));
// After
const DEFAULT_TIMEOUT_MS: u64 = 5000; // 默认超时时间
thread::sleep(Duration::from_millis(DEFAULT_TIMEOUT_MS));
```
#### Error Handling Patterns
```rust
#[derive(Debug)]
pub enum MyError {
IoError(std::io::Error),
ParseError(String),
}
impl std::error::Error for MyError {}
impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
MyError::IoError(e) => write!(f, "IO错误: {}", e),
MyError::ParseError(s) => write!(f, "解析错误: {}", s),
}
}
}
```
All public items must include doc comments with these sections:
Example:
```rust
/// 计算两个数的和
///
/// # 参数
/// * `a` - 第一个加数
/// * `b` - 第二个加数
///
/// # 返回值
/// 返回两个数的和
///
/// # 示例
/// ```
/// let result = add(2, 3);
/// assert_eq!(result, 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
```
For significant changes, document:
Use standardized patterns:
```rust
// Module references use @ prefix in comments:
// @module.rs, @lib.rs, @current_module.rs
// Import patterns:
use crate::module::Item; // From crate root
pub use super::Item; // From parent
use self::submodule::Item; // From current module
// Module definitions:
mod submodule; // Private module
pub mod api; // Public module
```
Provide all code suggestions as Rust code blocks:
```rust
// Your corrected or suggested code here
// Maintain proper indentation
// Preserve existing comments
// Add explanatory comments in Chinese
```
After implementing fixes:
1. Run `cargo check` - must pass
2. Run `cargo clippy` - address all warnings
3. Run `cargo test` - all tests must pass
4. Run `cargo fmt --check` - verify formatting
5. Confirm no new errors introduced
6. Verify no regressions in existing functionality
```rust
// Before: warning about unused std::collections::HashMap
// Instead of removing, implement a use case:
use std::collections::HashMap;
/// 创建默认配置映射
///
/// # 返回值
/// 返回包含默认配置项的HashMap
fn create_default_config() -> HashMap<String, String> {
let mut config = HashMap::new();
config.insert("timeout".to_string(), "5000".to_string());
config.insert("retries".to_string(), "3".to_string());
config
}
```
```rust
// Before:
fn wait_for_response() {
thread::sleep(Duration::from_millis(3000));
}
// After:
/// 默认等待响应超时时间(毫秒)
const RESPONSE_TIMEOUT_MS: u64 = 3000;
/// 等待响应,使用默认超时时间
fn wait_for_response() {
thread::sleep(Duration::from_millis(RESPONSE_TIMEOUT_MS));
}
```
```rust
// Error: expected `Result<String, MyError>`, found `String`
// Fix with proper error handling:
fn process_data(input: &str) -> Result<String, MyError> {
if input.is_empty() {
return Err(MyError::ParseError("输入为空".to_string()));
}
Ok(input.to_uppercase())
}
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/rust-eze-debugger/raw