该介绍目前仅提供英文版本。
Workers-rs
Workers-rs is the official Rust SDK for Cloudflare Workers, allowing developers to write and deploy Workers using Rust and WebAssembly. This SDK provides a seamless experience for building high-performance serverless applications with the safety and performance benefits of Rust.
Core Features
- Rust-First Development: Write Workers entirely in Rust
- WebAssembly Output: Compiles to WebAssembly for optimal performance
- Type Safety: Leverage Rust’s strong type system
- Workers Runtime API: Complete access to Workers runtime features
- Middleware Support: Built-in middleware system
- Ergonomic Macros: Simplified Worker development with proc macros
- HTTP Integration: First-class support for HTTP requests and responses
Quick Start
-
Prerequisites:
# Install Rust curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # Install wrangler npm install -g wrangler -
Create a New Project:
wrangler generate my-worker https://github.com/cloudflare/workers-rs/templates/worker cd my-worker -
Development:
wrangler dev -
Deployment:
wrangler deploy
Example Worker
use worker::*;
#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
Response::ok("Hello from Rust!")
}
Key Components
- Worker Runtime: Complete Rust bindings for the Workers runtime
- HTTP Types: Ergonomic types for HTTP handling
- Environment Bindings: Access to Workers KV, R2, and other services
- Router: Built-in routing system
- Middleware: Extensible middleware framework
- WebAssembly: Optimized WASM compilation
Use Cases
- High-performance API endpoints
- Complex data processing
- WebAssembly-powered applications
- CPU-intensive tasks
- Security-critical applications
- Edge computing solutions
Advanced Features
- KV Storage: Native bindings for Workers KV
- R2 Storage: Interface with R2 object storage
- Durable Objects: State management with DO support
- Fetch API: Complete HTTP client functionality
- Crypto Operations: Access to Web Crypto API
- Custom Domains: Domain management support
- Scheduled Tasks: Cron trigger support
Development Tools
- wrangler: CLI tool for development and deployment
- console_error_panic_hook: Better error messages
- wasm-bindgen: WebAssembly bindings generation
- workerd: Local development environment
Community and Support
License
This project is licensed under the Apache License 2.0.