Files
mcp-memory/agent-rules/rust_concurrency_quirks.md
T

1.6 KiB

Rust Guidelines & Quirks

Concurrency & Locking

  • Lock Poisoning Protection: NEVER use .unwrap() when acquiring a Mutex or RwLock (e.g., lock.write().unwrap()). ALWAYS use .unwrap_or_else(|e| e.into_inner()) to gracefully recover the underlying data from poisoned locks and prevent cascading panics across threads or async tasks.
  • Panic-Free Architecture: Avoid .unwrap() anywhere in production code. Use .expect() for startup initialization errors, and .unwrap_or_else(), .unwrap_or_default(), or proper Result propagation for runtime operations.

IDE & Rust-Analyzer Quirks

  • Boolean NOT Operator (E0600): Avoid using the unary ! operator on complex boolean expressions inside closures (e.g., !(a == b && c == d)). rust-analyzer may lose track of the type boundary and falsely report an E0600 error (cannot apply unary operator ! to type bool). Rewrite these expressions using De Morgan's laws (e.g., a != b || c != d).
  • Option::None Shadowing: If rust-analyzer throws a non_snake_case warning for None during pattern matching (often caused by wildcard imports like use crate::models::*; shadowing standard prelude variants), explicitly namespace the variant as std::option::Option::None to satisfy the LSP.
  • Deep Cloning across Thread Boundaries: When moving large structs (like entities with large text vectors) into a tokio::task::spawn_blocking closure for indexing or processing, construct the required primitive payloads or target structs on the main thread before the closure to avoid .clone()ing the entire massive struct across the 'static boundary.