Rust (Tokio)
Why Rust?
Section titled “Why Rust?”- ✅ Memory safety without garbage collector
- ✅ Zero-cost abstractions
- ✅ Compiled to single binary
- ✅ Fastest performance
- ✅ No runtime dependencies
Simple HTTP Server (Axum + Tokio)
Section titled “Simple HTTP Server (Axum + Tokio)”use axum::{routing::get, Router};
#[tokio::main]async fn main() { let app = Router::new() .route("/", get(|| async { "Hello from Rust!" }));
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000") .await .unwrap();
println!("Server running on :8000"); axum::serve(listener, app).await.unwrap();}Cargo.toml
Section titled “Cargo.toml”[package]name = "snapcode"version = "0.1.0"edition = "2021"
[dependencies]axum = "0.7"tokio = { version = "1", features = ["full"] }Build & Deploy
Section titled “Build & Deploy”# Developmentcargo run
# Release build (optimized)cargo build --release
# Binary location./target/release/snapcodeSystemd Service
Section titled “Systemd Service”[Unit]Description=SnapCode Rust ServerAfter=network.target
[Service]Type=simpleUser=www-dataWorkingDirectory=/var/www/snapcodeExecStart=/var/www/snapcode/snapcodeRestart=alwaysEnvironment=RUST_LOG=info
[Install]WantedBy=multi-user.targetTokio Runtime
Section titled “Tokio Runtime”// Async/await with Tokio#[tokio::main]async fn main() { // Spawns tasks on thread pool tokio::spawn(async { // Background task });}Performance Comparison
Section titled “Performance Comparison”| Metric | Rust | Go | Node.js | Python |
|---|---|---|---|---|
| Requests/sec | ~500K | ~300K | ~50K | ~10K |
| Memory | ~5MB | ~10MB | ~50MB | ~30MB |
| Startup | ~5ms | ~10ms | ~50ms | ~100ms |
| Binary size | ~5MB | ~10MB | N/A | N/A |
When to Use Rust?
Section titled “When to Use Rust?”- ✅ Performance critical applications
- ✅ System programming
- ✅ WebAssembly targets
- ✅ Memory-constrained environments
- ⚠️ Steeper learning curve