Skip to content

Commit dfa345f

Browse files
authored
Add Copilot instruction file for Rust (github#154)
* add instructions for rust * update readme
1 parent adfebcd commit dfa345f

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Team and project-specific instructions to enhance GitHub Copilot's behavior for
7070
| [Quarkus](instructions/quarkus.instructions.md) | Quarkus development standards and instructions | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fquarkus.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fquarkus.instructions.md) |
7171
| [ReactJS Development Instructions](instructions/reactjs.instructions.md) | ReactJS development standards and best practices | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Freactjs.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Freactjs.instructions.md) |
7272
| [Ruby on Rails](instructions/ruby-on-rails.instructions.md) | Ruby on Rails coding conventions and guidelines | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fruby-on-rails.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fruby-on-rails.instructions.md) |
73+
| [Rust Coding Conventions and Best Practices](instructions/rust.instructions.md) | Rust programming language coding conventions and best practices | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Frust.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Frust.instructions.md) |
7374
| [Secure Coding and OWASP Guidelines](instructions/security-and-owasp.instructions.md) | Comprehensive secure coding instructions for all languages and frameworks, based on OWASP Top 10 and industry best practices. | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fsecurity-and-owasp.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fsecurity-and-owasp.instructions.md) |
7475
| [Self-explanatory Code Commenting Instructions](instructions/self-explanatory-code-commenting.instructions.md) | Guidelines for GitHub Copilot to write comments to achieve self-explanatory code with less comments. Examples are in JavaScript but it should work on any language that has comments. | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fself-explanatory-code-commenting.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fself-explanatory-code-commenting.instructions.md) |
7576
| [Spec Driven Workflow v1](instructions/spec-driven-workflow-v1.instructions.md) | Specification-Driven Workflow v1 provides a structured approach to software development, ensuring that requirements are clearly defined, designs are meticulously planned, and implementations are thoroughly documented and validated. | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fspec-driven-workflow-v1.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fspec-driven-workflow-v1.instructions.md) |

instructions/rust.instructions.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
description: 'Rust programming language coding conventions and best practices'
3+
applyTo: '**/*.rs'
4+
---
5+
6+
# Rust Coding Conventions and Best Practices
7+
8+
Follow idiomatic Rust practices and community standards when writing Rust code.
9+
10+
These instructions are based on [The Rust Book](https://doc.rust-lang.org/book/), [Rust API Guidelines](https://rust-lang.github.io/api-guidelines/), [RFC 430 naming conventions](https://github.com/rust-lang/rfcs/blob/master/text/0430-finalizing-naming-conventions.md), and the broader Rust community at [users.rust-lang.org](https://users.rust-lang.org).
11+
12+
## General Instructions
13+
14+
- Always prioritize readability, safety, and maintainability.
15+
- Use strong typing and leverage Rust's ownership system for memory safety.
16+
- Break down complex functions into smaller, more manageable functions.
17+
- For algorithm-related code, include explanations of the approach used.
18+
- Write code with good maintainability practices, including comments on why certain design decisions were made.
19+
- Handle errors gracefully using `Result<T, E>` and provide meaningful error messages.
20+
- For external dependencies, mention their usage and purpose in documentation.
21+
- Use consistent naming conventions following [RFC 430](https://github.com/rust-lang/rfcs/blob/master/text/0430-finalizing-naming-conventions.md).
22+
- Write idiomatic, safe, and efficient Rust code that follows the borrow checker's rules.
23+
- Ensure code compiles without warnings.
24+
25+
## Patterns to Follow
26+
27+
- Use modules (`mod`) and public interfaces (`pub`) to encapsulate logic.
28+
- Handle errors properly using `?`, `match`, or `if let`.
29+
- Use `serde` for serialization and `thiserror` or `anyhow` for custom errors.
30+
- Implement traits to abstract services or external dependencies.
31+
- Structure async code using `async/await` and `tokio` or `async-std`.
32+
- Prefer enums over flags and states for type safety.
33+
- Use builders for complex object creation.
34+
- Split binary and library code (`main.rs` vs `lib.rs`) for testability and reuse.
35+
- Use `rayon` for data parallelism and CPU-bound tasks.
36+
- Use iterators instead of index-based loops as they're often faster and safer.
37+
- Use `&str` instead of `String` for function parameters when you don't need ownership.
38+
- Prefer borrowing and zero-copy operations to avoid unnecessary allocations.
39+
40+
### Ownership, Borrowing, and Lifetimes
41+
42+
- Prefer borrowing (`&T`) over cloning unless ownership transfer is necessary.
43+
- Use `&mut T` when you need to modify borrowed data.
44+
- Explicitly annotate lifetimes when the compiler cannot infer them.
45+
- Use `Rc<T>` for single-threaded reference counting and `Arc<T>` for thread-safe reference counting.
46+
- Use `RefCell<T>` for interior mutability in single-threaded contexts and `Mutex<T>` or `RwLock<T>` for multi-threaded contexts.
47+
48+
## Patterns to Avoid
49+
50+
- Don't use `unwrap()` or `expect()` unless absolutely necessary—prefer proper error handling.
51+
- Avoid panics in library code—return `Result` instead.
52+
- Don't rely on global mutable state—use dependency injection or thread-safe containers.
53+
- Avoid deeply nested logic—refactor with functions or combinators.
54+
- Don't ignore warnings—treat them as errors during CI.
55+
- Avoid `unsafe` unless required and fully documented.
56+
- Don't overuse `clone()`, use borrowing instead of cloning unless ownership transfer is needed.
57+
- Avoid premature `collect()`, keep iterators lazy until you actually need the collection.
58+
- Avoid unnecessary allocations—prefer borrowing and zero-copy operations.
59+
60+
## Code Style and Formatting
61+
62+
- Follow the Rust Style Guide and use `rustfmt` for automatic formatting.
63+
- Keep lines under 100 characters when possible.
64+
- Place function and struct documentation immediately before the item using `///`.
65+
- Use `cargo clippy` to catch common mistakes and enforce best practices.
66+
67+
## Error Handling
68+
69+
- Use `Result<T, E>` for recoverable errors and `panic!` only for unrecoverable errors.
70+
- Prefer `?` operator over `unwrap()` or `expect()` for error propagation.
71+
- Create custom error types using `thiserror` or implement `std::error::Error`.
72+
- Use `Option<T>` for values that may or may not exist.
73+
- Provide meaningful error messages and context.
74+
- Error types should be meaningful and well-behaved (implement standard traits).
75+
- Validate function arguments and return appropriate errors for invalid input.
76+
77+
## API Design Guidelines
78+
79+
### Common Traits Implementation
80+
Eagerly implement common traits where appropriate:
81+
- `Copy`, `Clone`, `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Hash`, `Debug`, `Display`, `Default`
82+
- Use standard conversion traits: `From`, `AsRef`, `AsMut`
83+
- Collections should implement `FromIterator` and `Extend`
84+
- Note: `Send` and `Sync` are auto-implemented by the compiler when safe; avoid manual implementation unless using `unsafe` code
85+
86+
### Type Safety and Predictability
87+
- Use newtypes to provide static distinctions
88+
- Arguments should convey meaning through types; prefer specific types over generic `bool` parameters
89+
- Use `Option<T>` appropriately for truly optional values
90+
- Functions with a clear receiver should be methods
91+
- Only smart pointers should implement `Deref` and `DerefMut`
92+
93+
### Future Proofing
94+
- Use sealed traits to protect against downstream implementations
95+
- Structs should have private fields
96+
- Functions should validate their arguments
97+
- All public types must implement `Debug`
98+
99+
## Testing and Documentation
100+
101+
- Write comprehensive unit tests using `#[cfg(test)]` modules and `#[test]` annotations.
102+
- Use test modules alongside the code they test (`mod tests { ... }`).
103+
- Write integration tests in `tests/` directory with descriptive filenames.
104+
- Write clear and concise comments for each function, struct, enum, and complex logic.
105+
- Ensure functions have descriptive names and include comprehensive documentation.
106+
- Document all public APIs with rustdoc (`///` comments) following the [API Guidelines](https://rust-lang.github.io/api-guidelines/).
107+
- Use `#[doc(hidden)]` to hide implementation details from public documentation.
108+
- Document error conditions, panic scenarios, and safety considerations.
109+
- Examples should use `?` operator, not `unwrap()` or deprecated `try!` macro.
110+
111+
## Project Organization
112+
113+
- Use semantic versioning in `Cargo.toml`.
114+
- Include comprehensive metadata: `description`, `license`, `repository`, `keywords`, `categories`.
115+
- Use feature flags for optional functionality.
116+
- Organize code into modules using `mod.rs` or named files.
117+
- Keep `main.rs` or `lib.rs` minimal - move logic to modules.
118+
119+
## Quality Checklist
120+
121+
Before publishing or reviewing Rust code, ensure:
122+
123+
### Core Requirements
124+
- [ ] **Naming**: Follows RFC 430 naming conventions
125+
- [ ] **Traits**: Implements `Debug`, `Clone`, `PartialEq` where appropriate
126+
- [ ] **Error Handling**: Uses `Result<T, E>` and provides meaningful error types
127+
- [ ] **Documentation**: All public items have rustdoc comments with examples
128+
- [ ] **Testing**: Comprehensive test coverage including edge cases
129+
130+
### Safety and Quality
131+
- [ ] **Safety**: No unnecessary `unsafe` code, proper error handling
132+
- [ ] **Performance**: Efficient use of iterators, minimal allocations
133+
- [ ] **API Design**: Functions are predictable, flexible, and type-safe
134+
- [ ] **Future Proofing**: Private fields in structs, sealed traits where appropriate
135+
- [ ] **Tooling**: Code passes `cargo fmt`, `cargo clippy`, and `cargo test`

0 commit comments

Comments
 (0)