1.6 KiB
1.6 KiB
Rust Guidelines & Quirks
Concurrency & Locking
- Lock Poisoning Protection: NEVER use
.unwrap()when acquiring aMutexorRwLock(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 properResultpropagation 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-analyzermay 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-analyzerthrows anon_snake_casewarning forNoneduring pattern matching (often caused by wildcard imports likeuse crate::models::*;shadowing standard prelude variants), explicitly namespace the variant asstd::option::Option::Noneto satisfy the LSP. - Deep Cloning across Thread Boundaries: When moving large structs (like entities with large text vectors) into a
tokio::task::spawn_blockingclosure 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'staticboundary.