This guide defines the core Rust conventions for our lab projects. It supplements the official Rust style guide with lab-specific practices for clarity and consistency.
- Follow idiomatic Rust.
- Prefer clarity over cleverness.
- Use comments to explain why, not what.
- Run
cargo fmtbefore committing.
- Use
lib.rsfor libraries,main.rsfor binaries. - Organize modules in separate files under
src/.
- Max line length: 100 chars.
- Indent with 4 spaces (no tabs).
- Run
cargo fmtandcargo clippy.
| Item | Style | Example |
|---|---|---|
| Crates | snake_case |
my_crate |
| Structs | CamelCase |
HttpRequest |
| Enums | CamelCase |
ResponseCode |
| Traits | CamelCase |
Serializable |
| Consts | SCREAMING_SNAKE_CASE |
MAX_RETRIES |
| Vars/Fns | snake_case |
parse_header |
- Group: std → external → internal.
- Alphabetize within groups.
use std::fs;
use anyhow::Result;
use crate::config::load;- Avoid
unwrap()andexpect(). - Use
Result<T, E>;anyhow::Result<T>is acceptable for tools.
- Run
cargo clippy --all-targets --all-features. - Fix all warnings unless justified.
- All public code should be tested.
- Use
#[cfg(test)] mod testsfor unit tests. - Use
tests/for integration tests.
- Use
///for public APIs,//for internal notes. - Avoid redundant comments.
/// Parses a user token from a header.
fn parse_token(header: &str) -> Option<Token> {
// Strip "Bearer " prefix
header.strip_prefix("Bearer ").map(Token::from)
}- Use
// TODO(name):format.
// TODO(user): handle invalid inputs-
cargo fmt -
cargo clippy -
cargo test