Quick Start
Quick Start
Build your first Rusta API in 5 minutes.
Option A: Scaffold with the CLI (recommended)
The fastest way to get a working Rusta project is with the scaffolder:
cargo install cargo-rustacargo rusta new my-apicd my-apicargo runThe API will be available at http://localhost:3000. See the CLI guide for template options.
Option B: Build from Scratch
1. Create a New Project
cargo new my-api --bincd my-api2. Add Dependencies
# Add to Cargo.tomlcargo add rusta tokio --features full3. Create Your First Controller
Create src/controllers/user_controller.rs:
use rusta::prelude::*;use std::sync::Arc;
#[injectable]pub struct UserService { // Add your dependencies here}
#[controller("/users")]impl UserController { #[get("/")] pub async fn list(&self) -> Response { Http::json(json!([{ "id": 1, "name": "Alice" }])) }
#[get("/:id")] pub async fn get(&self, Path(id): Path<String>) -> Response { Http::json(json!({ "id": id, "name": "User" })) }
#[post("/")] pub async fn create(&self, Json(body): Json<serde_json::Value>) -> Response { Http::created(body) }}4. Bootstrap the Application
Update src/main.rs:
mod controllers;
use rusta::{App, Container};use controllers::UserController;
#[tokio::main]async fn main() { let mut container = Container::new(); container.register(UserController::construct(&container));
App::new() .container(container) .run("0.0.0.0:3000", |res| match res { Ok(addr) => println!("Listening on {}", addr), Err(msg) => eprintln!("Startup error: {}", msg), }) .await;}Create src/controllers/mod.rs:
pub mod user_controller;5. Run and Test
cargo run
# Test endpointscurl http://localhost:3000/userscurl http://localhost:3000/users/123curl -X POST http://localhost:3000/users -H "Content-Type: application/json" -d '{"name":"Bob"}'Next Steps
- Controllers Guide — Learn route patterns
- Dependency Injection — Add services
- Middleware — Add authentication