Rust Playbook

Rust 1.100 · 2024 edition · for PHP and Laravel developers

Rust in 6 weeks, for Laravel developers

Learn to read and write Rust, build real command-line tools, and plug Rust into a Laravel app two ways: a fast binary an artisan command runs, and an axum service Laravel calls over HTTP. Then build the same service on Laravel Rust, Laravel’s own framework for Rust. One small concept at a time, every idea compared with the PHP you already know.

Start here

1. One conceptExplained in plain words, then shown in a small, complete program.
2. The compiler says noThe mistake a PHP developer makes, with the real error cargo prints.
3. Coming from PHPThe same idea in PHP, side by side, so you know where it fits.
4. PracticeRustlings and a short exercise, with the solution folded.

The plan

2 to 3 hours a day. Days 1–5 teach, Day 6 builds the week’s tool, Day 7 is the checkpoint. Every program on this site was compiled with Rust 1.100.1, and every error and output is what cargo really printed. 42 of 42 days are published.

0Day 0: Set up RustInstall the toolchain, an editor that understands Rust, and Rustlings. 30 minutes.Read

Week 1Basics and ownershipbuilds logstat v1

Cargo, types, the ownership rules that make Rust Rust, structs and enums. You finish with a CLI that reads a real laravel.log.

  1. 1Cargo and your first programCreate, run and build a project, and read Cargo.toml the way you read composer.json.5 min
  2. 2Variables, types and functionsImmutable by default, one fixed type per value, and functions whose last expression is the return value.15 min
  3. 3Ownership: who owns a valueEvery value has exactly one owner, assigning a String moves it, and the old name stops working.10 min
  4. 4Borrowing and slicesLend a value without giving it away: many readers or one writer, never both, and &str slices.15 min
  5. 5Structs, enums and matchYour own types: a struct with methods, an enum that carries data, Option instead of null, and match.25 min
  6. 6Build logstat v1Build dayA CLI that reads a laravel.log and counts entries by level, using only what this week taught.10 min
  7. 7Week 1 checkpointCheckpointFive broken programs to fix and explain. Pass them all before Week 2.20 min

Week 2Errors, collections, traitsbuilds logstat v2

Modules, Vec, String and HashMap, Result and ?, generics, traits and tests. logstat gets real flags and clean errors.

  1. 1Modules and project layoutSplit a program into lib.rs, main.rs and files, and control what is public.15 min
  2. 2Vec, String and HashMapThe three collections you will use every day, and the entry API.20 min
  3. 3Result and the ? operatorErrors are values: return them, pass them up with ?, and keep panic! for bugs.20 min
  4. 4Generics and traitsWrite code once for many types, and traits as interfaces you can implement for any type.25 min
  5. 5Lifetimes and testsRead lifetime annotations, and test your code with #[test] and cargo test.15 min
  6. 6Build logstat v2Build dayReal flags with clap, a custom error type with thiserror, and tests for the parser.45 min
  7. 7Week 2 checkpointCheckpointclippy clean, zero unwrap outside tests, and every failure ends in a clear message.20 min

Week 3Iterators, real data, speedbuilds csv2json

Closures, iterator chains, serde, streaming CSV and rayon. You benchmark a Rust tool against the same thing in PHP.

  1. 1ClosuresFunctions you can store and pass, and what they capture.15 min
  2. 2Iteratorsmap, filter, collect and fold, lazy by default, and as fast as a hand-written loop.15 min
  3. 3serde and JSONTyped json_encode and json_decode with derive.20 min
  4. 4Streaming CSVRead a huge CSV one row at a time, and report bad rows instead of stopping.25 min
  5. 5Parallelism with rayonUse every core by changing iter() to par_iter(), and what Send and Sync mean.15 min
  6. 6Build csv2jsonBuild day100K survey rows to JSON Lines with validation, benchmarked against PHP.50 min
  7. 7Week 3 checkpointCheckpointFlat memory on a 1M-row file, and bad rows never stop the run.20 min

Week 4Async and plugging into Laravelbuilds laravel-rust-worker

tokio, reqwest, sqlx, config and logging, and a Rust binary that an artisan command runs end to end.

  1. 1async, await and tokioWhat a future is, why it does nothing until awaited, and running tasks at the same time.20 min
  2. 2HTTP with reqwestCall APIs with timeouts and status checks, many requests in flight with a limit.30 min
  3. 3Databases with sqlxA connection pool, queries checked at compile time, and transactions, on your Laravel database.45 min
  4. 4Config and loggingRead .env, log with tracing, and exit codes PHP can understand.25 min
  5. 5Calling Rust from LaravelRun the binary with Process, JSON over stdin and stdout, and handle every failure in PHP.30 min
  6. 6Build the Laravel workerBuild dayAn artisan command sends a batch to Rust, Rust processes it concurrently and writes results in one transaction.1 h 5 min
  7. 7Week 4 checkpointCheckpointKill the database mid-run: Laravel gets a clear error and no half-written batch.30 min

Week 5Axum microservicebuilds laravel-rust-service

Routing, JSON and validation, shared state, errors as responses, middleware and tests. Laravel calls it with Http::post().

  1. 1Routing and handlersAn axum Router, handlers, and path and query extractors.20 min
  2. 2JSON in and out, with validationRequest and response bodies as types, and validation like a Form Request.30 min
  3. 3Shared stateA database pool every handler can use, with State.20 min
  4. 4Errors as responsesOne AppError that turns every failure into the right status code, like Laravel’s exception handler.30 min
  5. 5MiddlewareTracing, timeouts, CORS, an API key check and graceful shutdown.25 min
  6. 6Build the serviceBuild dayPOST a batch, poll its status, and Laravel calls it with Http.1 h 40 min
  7. 7Tests and the Week 5 checkpointCheckpointIntegration tests with oneshot and a fresh database per test, and the service’s exit check.30 min

Week 6Laravel Rustbuilds the service on Laravel Rust

Laravel’s own framework for Rust (beta, 0.1.4), built on the axum of Week 5: routes, typed input, models, queues and tests, the Laravel way.

  1. 1Laravel Rust: install and the shape of an appThe CLI, a new application, and every folder next to the PHP Laravel one it stands for.10 min
  2. 2Routes, handlers and typed inputRoute files, parameters and route model binding, and validation declared on a struct.15 min
  3. 3Models and migrationsA #[model] on an existing table, typed queries the compiler checks, relationships and migrations in Rust.30 min
  4. 4Errors, middleware and the HTTP clientlaravel::Result and abort, a #[middleware] API key check, and the Http client with retries and fakes.20 min
  5. 5Queues and tasksJobs with #[job], retries and backoff, release and fail, and concurrent work with laravel::task.25 min
  6. 6Build the service on Laravel RustBuild dayThe Week 5 analysis service again: the queue takes over the background work, the retry rule stays yours.15 min
  7. 7Tests and the Week 6 checkpointCheckpoint#[laravel::test] with HTTP and queue fakes, and when to pick axum, Laravel Rust or PHP Laravel.20 min