High-performance web framework with Flask-like API design
RustFlask combines the familiar, developer-friendly API of Python Flask with Rust's unparalleled performance and safety guarantees. Built on Tokio and Hyper, it delivers production-ready async performance without compromising ease of use.
For Developers Who Want:
- β Familiar Flask-like syntax without learning curve
- β 3.1x performance improvement over Python Flask
- β Memory safety with zero runtime overhead
- β Async/await support built on Tokio runtime
- β Type safety with compile-time error checking
- β UTF-8 native support for international applications
Add to your Cargo.toml
:
[dependencies]
rust_flask = { path = "./rust_flask" }
Create your first app in minutes:
use rust_flask::{FlaskApp, text_response, json_response};
#[tokio::main]
async fn main() {
let app = FlaskApp::new();
app.get("/", |_req, _params| {
text_response("Hello, RustFlask! π")
}).await;
app.get("/api/data", |_req, _params| {
json_response(&serde_json::json!({"message": "High performance JSON!"}))
}).await;
app.run([127, 0, 0, 1], 8080).await;
}
- Dynamic routing with
{param}
syntax (e.g.,/users/{id}
) - Multiple parameters support (e.g.,
/posts/{post_id}/comments/{comment_id}
) - Easy parameter access via
params.get("param_name")
- Method-specific helpers:
get()
andpost()
for clean routing - Flexible routing:
route()
for custom method handling - Method detection: Built-in HTTP method validation
- JSON parsing: Type-safe request body parsing with
Json<T>
- Response helpers:
text_response()
andjson_response()
built-in - UTF-8 support: Native international text handling
- Debug mode: Development-friendly error reporting
- Custom settings: Flexible host/port configuration
- Runtime options: Tokio-based async runtime
- Built on Hyper: Production-ready HTTP server
- Tokio runtime: Efficient async/await support
- Zero-cost abstractions: No runtime overhead
Add to your Cargo.toml
:
[dependencies]
rust_flask = { path = "./rust_flask" }
use rust_flask::{FlaskApp, text_response, json_response};
use hyper::{Method, Response, Body, StatusCode};
#[tokio::main]
async fn main() {
let app = FlaskApp::new();
// Add routes
app.route("/", |req, _params| {
if req.method() == Method::GET {
text_response("Hello, World!")
} else {
Response::builder()
.status(StatusCode::METHOD_NOT_ALLOWED)
.body(Body::from("Method not allowed"))
.unwrap()
}
}).await;
app.route("/json", |req, _params| {
if req.method() == Method::GET {
let data = vec!["hello", "world"];
json_response(&data)
} else {
Response::builder()
.status(StatusCode::METHOD_NOT_ALLOWED)
.body(Body::from("Method not allowed"))
.unwrap()
}
}).await;
// Start server
app.run([127, 0, 0, 1], 8080).await;
}
# Complete benchmark report with live demos
cargo run --example complete_benchmark_showcase # http://localhost:8086/
# Tech magazine style showcase
cargo run --example combined_demo # http://localhost:8085/
# Corporate professional style
cargo run --example fixed_beautiful_showcase # http://localhost:8084/
# Basic feature demonstration
cargo run --example showcase # http://localhost:8081/
# Enhanced functionality demo
cargo run --example enhanced_example # http://localhost:8083/
# Interactive playground
cargo run --example fancy_showcase # http://localhost:8082/
# UTF-8 encoding test
cargo run --example test_encoding # http://localhost:8001/
cargo test
Method | Description |
---|---|
new() |
Create new app with default configuration |
with_config(config) |
Create app with custom settings |
route(path, handler).await |
Register flexible route handler |
get(path, handler).await |
Register GET route handler |
post(path, handler).await |
Register POST route handler |
run(addr, port).await |
Start the HTTP server |
config() |
Get configuration reference |
Field | Type | Description |
---|---|---|
debug |
bool |
Enable development mode with detailed errors |
host |
String |
Server bind address (e.g., "127.0.0.1") |
port |
u16 |
Server port number |
Field/Method | Description |
---|---|
params |
HashMap<String, String> - All captured parameters |
get(key) |
Get parameter value by name |
insert(key, value) |
Add or update parameter |
Function | Description | Example |
---|---|---|
text_response(content) |
Create text/plain response | text_response("Hello World!") |
json_response(data) |
Create JSON response | json_response(&json!({"key": "value"})) |
Method | Description |
---|---|
Json::<T>::from_request(req) |
Parse JSON request body with type safety |
Use {parameter}
syntax in route paths:
/users/{id}
- Single parameter capture/posts/{post_id}/comments/{comment_id}
- Multiple parameters- Parameters accessible via
params.get("param_name")
Comprehensive benchmarking against Python Flask demonstrates significant performance advantages:
- RustFlask: 3,847 requests/second
- Python Flask: 1,242 requests/second
- Performance Improvement: 3.1x faster
- RustFlask: 2.6ms average response time
- Python Flask: 8.1ms average response time
- Latency Reduction: 68% improvement
- RustFlask: 4,156 RPS (peak)
- Python Flask: 1,398 RPS (peak)
- Memory Usage: 28MB vs 72MB (2.6x more efficient)
- Success Rate: 100% (RustFlask) vs 95.1% (Flask)
- Memory Safety: Zero runtime overhead
- Type Safety: Compile-time error prevention
- Test Environment: Apple M1 Pro, 16GB RAM, macOS 15.5
- Concurrency: 10 concurrent connections
- Total Requests: 800 across 8 endpoint types
- Measurement: High-precision microsecond accuracy
# Execute comprehensive performance comparison
cd benchmarks
python benchmark_runner.py
# Results saved to: reports/benchmark_results.json
# Visual report: reports/magazine_benchmark_report.html
# Clone and build
git clone <repository-url>
cd rust_flask
cargo build
# Run tests
cargo test
# Check code quality
cargo clippy
cargo fmt
- Hyper HTTP Server: Production-ready HTTP/1.1 implementation
- Tokio Runtime: Efficient async task scheduling
- Serde: High-performance serialization/deserialization
- Chrono: Date/time handling with timezone support
- Memory Safety: Zero-cost ownership system prevents leaks
- Type Safety: Compile-time error detection
- Thread Safety: Fearless concurrency without data races
- UTF-8 Safety: Built-in Unicode text handling
- Zero-copy parsing: Minimal memory allocations
- Connection pooling: Efficient connection reuse
- Async I/O: Non-blocking request handling
- Compiled efficiency: Native machine code execution
We welcome contributions! Please see our contributing guidelines and feel free to:
- π Report bugs via GitHub issues
- π‘ Suggest features through discussions
- π Submit pull requests for improvements
- π Improve documentation and examples
This project is licensed under the MIT License - see the LICENSE file for details.
- Flask Team for the inspiration and excellent API design
- Rust Community for the incredible ecosystem and tooling
- Tokio Project for the robust async runtime
- Hyper Team for the production-ready HTTP server