This guide covers the coding standards for Neumann. All contributions must follow these guidelines.
- Prefer iterators over loops
- Use
?for error propagation - Keep functions small and focused
- Prefer composition over inheritance patterns
All code must pass cargo fmt:
cargo fmt --checkAll code must pass clippy with warnings as errors:
cargo clippy -- -D warningsDoc comments (///) are for rustdoc generation. Use them sparingly.
- Types (structs, enums) - explain purpose and invariants
- Non-obvious behavior - when a method does something unexpected
- Complex algorithms - when the "why" isn't clear from code
- Methods with self-explanatory names (
get,set,new,len,is_empty) - Trivial implementations
- Anything where the doc would just repeat the function name
// BAD - restates the obvious
/// Get a field value
pub fn get(&self, key: &str) -> Option<&TensorValue>
// GOOD - no comment needed, name is clear
pub fn get(&self, key: &str) -> Option<&TensorValue>
// GOOD - explains non-obvious behavior
/// Returns cloned data to ensure thread safety.
/// For zero-copy access, use get_ref().
pub fn get(&self, key: &str) -> Result<TensorData>Inline comments (//) should explain "why", never "what".
- Types:
PascalCase - Functions and variables:
snake_case - Constants:
SCREAMING_SNAKE_CASE - Modules:
snake_case
- Use
Resultfor fallible operations - Define error types with
thiserror - Provide context with error messages
#[derive(Debug, thiserror::Error)]
pub enum MyError {
#[error("failed to parse config: {0}")]
ConfigParse(String),
#[error("connection failed: {source}")]
Connection {
#[from]
source: std::io::Error,
},
}- Use
DashMapfor concurrent hash maps - Avoid
Mutexwhere possible (useparking_lotif needed) - Document thread-safety in type docs
- Unit tests in the same file as code (
#[cfg(test)]module) - Test the public API, not implementation details
- Use descriptive names:
test_<function>_<scenario>_<expected>
- Write clear, imperative commit messages
- No emoji in commits
- Reference issue numbers when applicable
- Keep commits atomic - one logical change per commit