Skip to content

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions rust.md
Original file line number Diff line number Diff line change
@@ -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<T, E>`; `anyhow::Result<T>` 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<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`