con un clic
rust
Write Rust code following best practices. Use when developing Rust applications. Covers ownership, error handling, and async patterns.
Menú
Write Rust code following best practices. Use when developing Rust applications. Covers ownership, error handling, and async patterns.
Basado en la clasificación ocupacional SOC
Biome 2.x linting and formatting patterns. Use when configuring code quality tools, setting up linting rules, formatting code, or integrating with CI/CD. Covers migration from ESLint/Prettier.
Hono 4.x web framework patterns. Use when building APIs, middleware, routing, or server-side applications. Covers multi-runtime support (Node, Bun, Cloudflare Workers), validation, CORS, and error handling.
Radix UI primitive patterns. Use when building accessible, unstyled UI components like dialogs, dropdowns, tooltips, tabs, and selects. Covers Tailwind styling, keyboard navigation, animations, and portal management.
React development patterns. Use when building React components, managing state, creating custom hooks, or optimizing React applications. Covers React 19 features, TypeScript integration, and composition patterns.
Tailwind CSS 4.x utility-first styling patterns. Use when building UI components, creating responsive layouts, implementing design systems, or customizing themes. Covers CSS-first configuration, @theme directive, and component patterns.
Vite 7.x build tool patterns. Use when configuring build setup, development server, environment variables, asset handling, or optimizing production builds for React applications.
| name | rust |
| description | Write Rust code following best practices. Use when developing Rust applications. Covers ownership, error handling, and async patterns. |
my-project/
├── Cargo.toml
├── src/
│ ├── main.rs
│ ├── lib.rs
│ └── handlers/
│ └── mod.rs
└── tests/
└── integration.rs
use thiserror::Error;
#[derive(Error, Debug)]
pub enum AppError {
#[error("User not found: {0}")]
NotFound(String),
#[error("Database error: {0}")]
Database(#[from] sqlx::Error),
#[error("Validation error: {0}")]
Validation(String),
}
// Using Result
async fn get_user(id: &str) -> Result<User, AppError> {
let user = sqlx::query_as!(User, "SELECT * FROM users WHERE id = $1", id)
.fetch_optional(&pool)
.await?
.ok_or_else(|| AppError::NotFound(id.to_string()))?;
Ok(user)
}
// Using ? operator
fn process() -> Result<(), AppError> {
let user = get_user("123")?;
validate(&user)?;
save(&user)?;
Ok(())
}
// Ownership transfer
fn take_ownership(s: String) {
println!("{}", s);
} // s is dropped here
// Borrowing (immutable)
fn borrow(s: &String) {
println!("{}", s);
}
// Mutable borrowing
fn mutate(s: &mut String) {
s.push_str(" world");
}
// Lifetimes
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { x } else { y }
}
use tokio;
#[tokio::main]
async fn main() {
let result = fetch_data().await;
}
async fn fetch_all(urls: Vec<String>) -> Vec<Response> {
let futures: Vec<_> = urls
.into_iter()
.map(|url| tokio::spawn(async move { fetch(&url).await }))
.collect();
let results = futures::future::join_all(futures).await;
results.into_iter().filter_map(|r| r.ok()).collect()
}
use axum::{
extract::{Path, State},
http::StatusCode,
Json,
};
async fn get_user(
State(pool): State<PgPool>,
Path(id): Path<String>,
) -> Result<Json<User>, (StatusCode, String)> {
let user = sqlx::query_as!(User, "SELECT * FROM users WHERE id = $1", id)
.fetch_optional(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.ok_or((StatusCode::NOT_FOUND, "User not found".to_string()))?;
Ok(Json(user))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validation() {
let result = validate("valid@email.com");
assert!(result.is_ok());
}
#[tokio::test]
async fn test_async_operation() {
let result = fetch_data().await;
assert!(result.is_ok());
}
}
# Format
cargo fmt
# Lint
cargo clippy -- -D warnings
# Test
cargo test
# Build release
cargo build --release