-
Notifications
You must be signed in to change notification settings - Fork 7
Rust style guide #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rennergade
wants to merge
2
commits into
secure-systems-lab:master
Choose a base branch
from
rennergade:rust-guidelines
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Rust style guide #27
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# 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()`. | ||
- Using `Result<T, E>`; `anyhow::Result<T>` is acceptable for tools. | ||
|
||
--- | ||
|
||
## Linting | ||
|
||
- Run `cargo clippy --all-targets --all-features`. | ||
- Fix all warnings unless there is a reason it should not be fixed. | ||
|
||
--- | ||
|
||
## Testing | ||
|
||
- All public code should be tested. | ||
- 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. | ||
|
||
--- | ||
rennergade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## 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. | ||
- Avoid redundant comments. | ||
|
||
```rust | ||
/// Parses a user token from a header. | ||
fn parse_token(header: &str) -> Option<Token> { | ||
// 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` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.