Copilot instructions for Rust 2024 crates covering borrowed parameters, thiserror 2 error enums, non-blocking Tokio code and a justified dependency policy.
.github/instructions/github-copilot-rust-guidelines.instructions.md. Downloaded files need to be placed in that repository path.---
applyTo: "**/*"
---
# Rust Instructions
Applies to: Rust crates in this repository (`**/*.rs`, `Cargo.toml`).
Version baseline: checked against the Rust 2024 edition (stable since Rust 1.85) and thiserror 2.x. Read `Cargo.toml` (`edition`, `rust-version`, existing dependencies) first and stay within them.
## Ownership and borrowing
- Take the most general borrowed form a function needs: `&str` over `&String`, `&[T]` over `&Vec<T>`, `&Path` over `&PathBuf`.
- Take ownership (`String`, `Vec<T>`) only when the function stores or consumes the value; let the caller decide whether to clone.
- Take `&mut` only when the function mutates; return new values when that is clearer than mutating.
- Do not add `.clone()` to satisfy the borrow checker without saying why; restructure the borrow first.
```rust
fn checksum(data: &[u8]) -> u32 {
data.iter().map(|&b| u32::from(b)).sum()
}
fn strip_zeros(data: &mut Vec<u8>) {
data.retain(|&b| b != 0);
}
```
## Errors
- Libraries: define error enums with `thiserror` and keep variants meaningful to callers.
- Binaries and top-level glue: `anyhow::Result` with `.context("...")` is fine.
- No `unwrap()`/`expect()` on fallible input in library code; `expect("reason")` is acceptable for real invariants, with the invariant stated.
```rust
use std::collections::HashMap;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum AppError {
#[error("user {0} not found")]
NotFound(String),
#[error(transparent)]
Database(#[from] sqlx::Error),
}
pub fn get_user(users: &HashMap<String, User>, id: &str) -> Result<User, AppError> {
users
.get(id)
.cloned()
.ok_or_else(|| AppError::NotFound(id.to_owned()))
}
```
Only add the `Database(#[from] sqlx::Error)` variant if the crate already depends on `sqlx`.
## Async (Tokio)
- Use `#[tokio::main]` only in the binary entry point; libraries expose `async fn` and do not start a runtime.
- Never block inside async code: use `tokio::fs`, `tokio::time::sleep`, or `tokio::task::spawn_blocking` for CPU-heavy or blocking work.
- Do not hold a `std::sync::Mutex` guard across `.await`.
- Bound concurrency and channels (`mpsc::channel(n)`, `JoinSet`, `Semaphore`) instead of spawning without limit.
```rust
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = load_config().await?;
run(config).await
}
```
## Dependencies
- Prefer the standard library and crates already in `Cargo.lock`. When adding a crate, say what it is for and why existing dependencies do not cover it.
- Enable only the features you use (`tokio = { version = "1", features = ["macros", "rt-multi-thread"] }`).
- Keep `Cargo.lock` committed for binaries.
## Verify before finishing
Run these when they are available in the project:
- `cargo fmt --check`
- `cargo clippy --all-targets -- -D warnings`
- `cargo test`
Copilot instructions for TypeScript project setup covering strict tsconfig flags and their effects, type-only imports, module hygiene and TSDoc for public APIs.
Cursor rules for Rust systems code: borrowing-first APIs, thiserror/anyhow error layers, Tokio async with correct mutex choice, allocation-aware performance and project layout.
Copilot instructions for React 19 TypeScript components that forward native props, use effects only for synchronization and are tested by role.
Copilot
systems
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A Rust 2024 workspace with a library crate and a Tokio binary uses these Copilot instructions while a developer asks for a lookup function and its error type.
In the users library crate, add get_user(users, id) that returns a cloned User or a NotFound error, and wire it into the binary's main. The crate already uses thiserror; it does not use sqlx.
Illustrative; not a recorded model run. A function taking &HashMap<String, User> and &str and returning Result<User, AppError>; an AppError enum derived with thiserror that has a NotFound(String) variant and no sqlx variant because sqlx is not a dependency; no unwrap in the library; the binary uses anyhow::Result in main; no new crates added.
GitHub Copilot reads repository-wide custom instructions from .github/copilot-instructions.md and path-specific instructions from .github/instructions/*.instructions.md files with an applyTo glob. Copilot can also use AGENTS.md files, where the nearest file in the directory tree takes precedence. Which file types are honored depends on the surface: VS Code, Visual Studio, JetBrains, Xcode, Eclipse and GitHub.com differ.
.github/instructions/github-copilot-rust-guidelines.instructions.mdStarting point, not a tested configuration for your repository. Used for files matching applyTo on surfaces that support path-specific instructions. Review the instructions, adapt the file paths and run your project's existing checks.
---
applyTo: "**/*"
---
# Rust Instructions
Applies to: Rust crates in this repository (`**/*.rs`, `Cargo.toml`).
Version baseline: checked against the Rust 2024 edition (stable since Rust 1.85) and thiserror 2.x. Read `Cargo.toml` (`edition`, `rust-version`, existing dependencies) first and stay within them.
## Ownership and borrowing
- Take the most general borrowed form a function needs: `&str` over `&String`, `&[T]` over `&Vec<T>`, `&Path` over `&PathBuf`.
- Take ownership (`String`, `Vec<T>`) only when the function stores or consumes the value; let the caller decide whether to clone.
- Take `&mut` only when the function mutates; return new values when that is clearer than mutating.
- Do not add `.clone()` to satisfy the borrow checker without saying why; restructure the borrow first.
```rust
fn checksum(data: &[u8]) -> u32 {
data.iter().map(|&b| u32::from(b)).sum()
}
fn strip_zeros(data: &mut Vec<u8>) {
data.retain(|&b| b != 0);
}
```
## Errors
- Libraries: define error enums with `thiserror` and keep variants meaningful to callers.
- Binaries and top-level glue: `anyhow::Result` with `.context("...")` is fine.
- No `unwrap()`/`expect()` on fallible input in library code; `expect("reason")` is acceptable for real invariants, with the invariant stated.
```rust
use std::collections::HashMap;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum AppError {
#[error("user {0} not found")]
NotFound(String),
#[error(transparent)]
Database(#[from] sqlx::Error),
}
pub fn get_user(users: &HashMap<String, User>, id: &str) -> Result<User, AppError> {
users
.get(id)
.cloned()
.ok_or_else(|| AppError::NotFound(id.to_owned()))
}
```
Only add the `Database(#[from] sqlx::Error)` variant if the crate already depends on `sqlx`.
## Async (Tokio)
- Use `#[tokio::main]` only in the binary entry point; libraries expose `async fn` and do not start a runtime.
- Never block inside async code: use `tokio::fs`, `tokio::time::sleep`, or `tokio::task::spawn_blocking` for CPU-heavy or blocking work.
- Do not hold a `std::sync::Mutex` guard across `.await`.
- Bound concurrency and channels (`mpsc::channel(n)`, `JoinSet`, `Semaphore`) instead of spawning without limit.
```rust
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = load_config().await?;
run(config).await
}
```
## Dependencies
- Prefer the standard library and crates already in `Cargo.lock`. When adding a crate, say what it is for and why existing dependencies do not cover it.
- Enable only the features you use (`tokio = { version = "1", features = ["macros", "rt-multi-thread"] }`).
- Keep `Cargo.lock` committed for binaries.
## Verify before finishing
Run these when they are available in the project:
- `cargo fmt --check`
- `cargo clippy --all-targets -- -D warnings`
- `cargo test`
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Instruction support differs by surface: VS Code supports repository-wide, path-specific and AGENTS.md instructions; Visual Studio supports repository-wide and path-specific; JetBrains and Xcode use .github/copilot-instructions.md. GitHub's documentation describes AGENTS.md files stored anywhere in the repository, with the nearest file in the directory tree taking precedence, or alternatively a single CLAUDE.md or GEMINI.md in the root. Support differs by surface; GitHub's IDE page lists AGENTS.md among the instruction types for VS Code.
Documentation checked 2026-11-05: GitHub Docs: Adding repository custom instructionsGitHub Docs: Repository instructions in your IDE
Copilot instructions for TypeScript project setup covering strict tsconfig flags and their effects, type-only imports, module hygiene and TSDoc for public APIs.
Cursor rules for Rust systems code: borrowing-first APIs, thiserror/anyhow error layers, Tokio async with correct mutex choice, allocation-aware performance and project layout.
Copilot instructions for React 19 TypeScript components that forward native props, use effects only for synchronization and are tested by role.