From e7153c6cc32a2f997c456c3c5dcc9d0efea551fb Mon Sep 17 00:00:00 2001 From: Nicholas Renner Date: Wed, 16 Jul 2025 18:48:27 -0400 Subject: [PATCH 1/2] add rust style guide --- rust.md | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 rust.md diff --git a/rust.md b/rust.md new file mode 100644 index 0000000..a921a93 --- /dev/null +++ b/rust.md @@ -0,0 +1,107 @@ +# Rust Guidelines +This guide defines the core Rust conventions for our lab projects. It supplements the [official Rust style guide](https://doc.rust-lang.org/1.0.0/style/) with lab-specific practices for clarity and consistency. + +--- + +## General Guidelines + +- Follow idiomatic Rust. +- Prefer clarity over cleverness. +- Use comments to explain *why*, not *what*. +- Run `cargo fmt` before committing. + +--- + +## Project Structure + +- Use `lib.rs` for libraries, `main.rs` for binaries. +- Organize modules in separate files under `src/`. + +--- + +## Formatting + +- Max line length: 100 chars. +- Indent with 4 spaces (no tabs). +- Run `cargo fmt` and `cargo clippy`. + +--- + +## Naming Conventions + +| 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` | + +--- + +## Imports + +- Group: std → external → internal. +- Alphabetize within groups. + +```rust +use std::fs; +use anyhow::Result; +use crate::config::load; +``` + +--- + +## Error Handling + +- Avoid `unwrap()` and `expect()`. +- Use `Result`; `anyhow::Result` is acceptable for tools. + +--- + +## Linting + +- Run `cargo clippy --all-targets --all-features`. +- Fix all warnings unless justified. + +--- + +## Testing + +- All public code should be tested. +- Use `#[cfg(test)] mod tests` for unit tests. +- Use `tests/` for integration tests. + +--- + +## Comments & Docs + +- Use `///` for public APIs, `//` for internal notes. +- Avoid redundant comments. + +```rust +/// Parses a user token from a header. +fn parse_token(header: &str) -> Option { + // Strip "Bearer " prefix + header.strip_prefix("Bearer ").map(Token::from) +} +``` + +--- + +## TODOs + +- Use `// TODO(name):` format. + +```rust +// TODO(user): handle invalid inputs +``` + +--- + +## Pre-Commit Checklist + +- [ ] `cargo fmt` +- [ ] `cargo clippy` +- [ ] `cargo test` \ No newline at end of file From a80c3a3c4bd52048ecf464ebe1a478e79cae0cda Mon Sep 17 00:00:00 2001 From: Nicholas Renner Date: Wed, 16 Jul 2025 20:48:17 -0400 Subject: [PATCH 2/2] suggested changes and benchmarking section --- rust.md | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/rust.md b/rust.md index a921a93..eaf10a7 100644 --- a/rust.md +++ b/rust.md @@ -56,25 +56,36 @@ use crate::config::load; ## Error Handling - Avoid `unwrap()` and `expect()`. -- Use `Result`; `anyhow::Result` is acceptable for tools. +- Using `Result`; `anyhow::Result` is acceptable for tools. --- ## Linting - Run `cargo clippy --all-targets --all-features`. -- Fix all warnings unless justified. +- Fix all warnings unless there is a reason it should not be fixed. --- ## Testing - All public code should be tested. -- Use `#[cfg(test)] mod tests` for unit tests. +- Test both success cases and common failure cases. +- You do not need 100% test coverage except for in things like public APIs, but ~90% should be the norm. - Use `tests/` for integration tests. --- +## Benchmarking + +- Use the [`criterion`](https://crates.io/crates/criterion) crate for writing reliable benchmarks. +- Place benchmarks in the `benches/` directory using the standard Cargo bench layout. +- Group related benchmarks. +- Run with `cargo bench` and use Criterion's statistical output to compare changes. +- Always benchmark in release mode (`--release`) for realistic results. + +--- + ## Comments & Docs - Use `///` for public APIs, `//` for internal notes.