Rust For Laravel Developers
ASecurityWrite Rust for Laravel and PHP developers, review existing Rust for PHP habits, and explain Rust errors in PHP terms, in Laravel for Rust or any other Rust project. Use when writing Rust for a PHP developer, reviewing Rust for PHP habits (truthy checks, default arguments, copied arrays, null and ??, throw/try-catch, associative arrays, strlen and substr, (int) casts, inheritance, __toString, Collection chains, use() closures, static state, Composer and PSR-4), or explaining an ownership, borr...
- 2 stars
- 0 votes
- 0 copies
- 0 views
- Added December 2, 2026
Works with
Security analysis
100/100Pro scans all 19 files and shows the line behind each finding
npx -y skills add airshell/laravel-php-to-rust --skill rust-for-laravel-developers --agent claude-codeAre you the author of Rust For Laravel Developers?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/airshell-rust-for-laravel-developers)---
name: rust-for-laravel-developers
description: Write Rust for Laravel and PHP developers, review existing Rust for PHP habits, and explain Rust errors in PHP terms, in Laravel for Rust or any other Rust project. Use when writing Rust for a PHP developer, reviewing Rust for PHP habits (truthy checks, default arguments, copied arrays, null and ??, throw/try-catch, associative arrays, strlen and substr, (int) casts, inheritance, __toString, Collection chains, use() closures, static state, Composer and PSR-4), or explaining an ownership, borrow, lifetime, Option, Result, iterator or async error in PHP terms. Maps each PHP behaviour to Rust and shows what the PHP habit does there: a compiler error, a panic, a wrong value or needless work. Every Rust code block compiles with rustc 1.100 (edition 2024); the PHP and Laravel examples were checked on PHP 8.4 and Laravel 13. For porting Laravel framework code use laravel-php-to-rust; for auditing a Laravel for Rust app use laravel-rust-audit.
license: MIT
metadata:
author: airshell
version: "1.1.2"
rustVersion: "1.85+ (edition 2024; tested 1.100)"
phpVersion: "8.3+ (tested 8.4)"
---
# Rust for Laravel Developers
Write Rust for Laravel and PHP developers, review existing Rust for PHP habits, and explain Rust in terms of the PHP they already know. Contains 25 rules across 9 categories. Every Rust code block compiles with rustc 1.100 (edition 2024; the examples use `serde`, `thiserror` and `tokio`). The PHP examples were checked on PHP 8.4, and the Collection examples on Laravel 13 (`illuminate/collections` 13.45). Claims that something panics, returns a value or fails to compile were checked with a test or the compiler.
**Version 1.1.2 (December 2026).** This skill is about the language. For the framework (routes, Eloquent, queues, Blade) in Laravel for Rust, use `laravel-php-to-rust` (porting) and `laravel-rust-best-practices` (writing new code); for auditing a Laravel for Rust app, use `laravel-rust-audit`.
## When to Apply
- Writing Rust for a PHP or Laravel developer, in Laravel for Rust or any other Rust project: follow the rules so the code does not repeat the PHP habits they describe
- Reviewing Rust for PHP habits: `.clone()` everywhere, `.unwrap()` on input, unchecked `as` casts, `HashMap` for records, `panic!` as `throw`, `dedup()` for `unique()`, unawaited futures, per-user statics. Name the rule for each finding
- Explaining a compiler error (moved value, borrow, lifetime, type annotations needed) to someone who knows PHP
## How to Use It
- **Writing**: before writing code that PHP handles differently (null checks, arrays, strings, numbers, errors, collections, async), read the relevant rules in the Quick Map below and follow their Correct examples
- **Reviewing**: follow "Reviewing Rust for PHP Habits" below
- **Explaining**: find the PHP habit in the Quick Map and answer from its rule. When the compiler rejects code, start with `mind-compiler-errors`: the error often marks a bug PHP would only have shown at runtime
Each rule shows the PHP behaviour (checked on PHP 8.4), what changes in Rust with quotes from the official docs, Rust written with the PHP habit and what it does, and the idiomatic version.
## Reviewing Rust for PHP Habits
1. **Run the compiler and clippy.** The default lints already catch some habits (unused futures, iterators and `Result`s, `&String` parameters, a std mutex guard held across `.await`). Then turn on the lints for the rest, on the app code without the tests, where `unwrap()` is fine:
```bash
cargo clippy --all-targets
cargo clippy --lib --bins -- -W clippy::unwrap_used -W clippy::expect_used -W clippy::indexing_slicing -W clippy::string_slice -W clippy::cast_possible_truncation -W clippy::cast_sign_loss -W clippy::cast_possible_wrap
```
2. **Search for the patterns below.** A match is a place to read, not a finding: an `expect()` on a value the code guarantees, a `clone()` of a value that is needed twice, or a `.dedup()` on sorted data is fine.
| Search for | The habit to confirm | Rule |
|---|---|---|
| `.clone()`, and `String` or `Vec<T>` parameters | Cloned to end a move or borrow error, or a parameter that takes ownership only to read | `own-borrow-before-clone`, `mind-compiler-errors` |
| `.unwrap()`, `.expect(` | Panics on input, configuration, rows or responses | `err-panic-unwrap-expect` |
| `panic!(` | Used as `throw` for an expected failure | `err-exceptions-to-result` |
| `.unwrap_or(` with a function call or `to_owned()` | A default built eagerly; PHP's `??` evaluates it only when needed | `types-null-to-option` |
| `as` casts between number types | A cast that can truncate, wrap or lose the sign | `types-numbers` |
| `f64`, `f32` or `parse::<f64>` for money | Floats for amounts | `types-numbers` |
| `HashMap<String, _>` or `serde_json::Value` with fixed keys | A map where a struct belongs | `types-arrays-to-structs-and-maps` |
| A `HashMap` iterated into output, JSON or snapshots | Order that is unspecified where the output needs one | `types-arrays-to-structs-and-maps` |
| Indexing `x[..]` on maps, vectors and strings | Panics on a missing key, index or character boundary | `types-arrays-to-structs-and-maps`, `types-strings-bytes-chars`, `err-panic-unwrap-expect` |
| `.len()` on user text | Bytes counted as characters | `types-strings-bytes-chars` |
| `_ =>` in a `match` on the app's own enum | New variants silently swallowed | `types-enums-and-match` |
| `.dedup()` | Used as `unique()` on unsorted data | `coll-group-sort-unique` |
| `let _ =`, and calls whose result is not used | A future, iterator or `Result` thrown away | `async-futures-await`, `coll-collection-to-iterators`, `err-exceptions-to-result` |
| `std::thread::sleep`, `std::fs::`, heavy loops in `async fn` or `async` blocks (and the functions they call) | Blocking the runtime | `async-futures-await` |
| `tokio::spawn` whose handle is dropped | Work that must survive a restart, which belongs on a queue | `async-futures-await` |
| A `std::sync` `Mutex` or `RwLock` guard still alive at an `.await` (not `tokio::sync::Mutex::lock().await`) | A std guard held across an await | `async-shared-state` |
| `static`, `static mut`, `OnceLock`, `LazyLock`, `thread_local!`, singletons | Per-user data in process-wide state | `mind-long-running-process` |
| `#![allow(`, `#[allow(`, `[lints]` in `Cargo.toml` | A lint silenced more widely than one item, or without a reason | `tool-clippy-fmt-check` |
| `.rs` files that no `mod` declares, and files in `tests/` subfolders | Code Cargo never compiles: a PSR-4 or `tests/Unit` habit. Report it only after checking it is not a target Cargo finds itself (`src/main.rs`, `src/bin/`, `tests/name.rs`, `tests/name/main.rs`, `examples/`, `benches/`) or one declared in `Cargo.toml` (`[[bin]]`, `[[test]]`) | `mod-namespaces-to-modules`, `tool-tests` |
3. **Report each finding** with the file and line, the PHP habit, the rule that explains it, and the fix the rule describes (its Correct example when it fits). Say which findings came from clippy and which from reading the code.
## Quick Map
| PHP | Rust | Rule |
|---|---|---|
| Errors at runtime, PHPStan | Errors at compile time; `cargo check` | `mind-compiler-errors` |
| Fresh state per request (PHP-FPM) | A long-running process; no per-user statics | `mind-long-running-process` |
| `if ($x)`, `$x += 1`, default and named arguments | `bool` conditions, `let mut`, shadowing, expression blocks, options structs | `mind-syntax-habits` |
| `$b = $a` copies arrays, shares objects | Assignment moves; `.clone()` is explicit | `own-move-not-copy` |
| `&$arr` parameters, by-value strings | `&mut Vec<T>`; `&str` and `&[T]` parameters | `own-borrow-before-clone` |
| `use ($x)`, `fn () =>` | Capture inferred from use; `move` | `own-closures-capture` |
| Objects held anywhere (GC) | Owned fields; lifetimes only in short helpers | `own-return-owned-values` |
| `?T`, `??`, `?->` | `Option<T>`, `unwrap_or_else`, `map`/`and_then`, `?` | `types-null-to-option` |
| One array type for everything | `struct`, `Vec`, `HashMap` (unspecified order), `BTreeMap` | `types-arrays-to-structs-and-maps` |
| `strlen`, `substr`, `.` | `len()` is bytes; `chars()`; `format!` | `types-strings-bytes-chars` |
| Overflow to float, `(int)` casts | Overflow panics or wraps; `parse` and `TryFrom` | `types-numbers` |
| Backed enums, `match` at runtime | Enums with data; exhaustive `match` at compile time | `types-enums-and-match` |
| `throw`, `try`/`catch`/`finally` | `Result`, `?`, `match`, `Drop` | `err-exceptions-to-result` |
| `findOrFail`, fatal errors | `unwrap`/`expect` for bugs and tests, not for input | `err-panic-unwrap-expect` |
| Interfaces, abstract classes, traits | Traits with default methods; composition | `trait-interfaces-and-composition` |
| Polymorphic parameters and returns | Generics; `dyn Trait` for mixed values | `trait-generics-and-dyn` |
| `__toString`, `==`, named constructors | `Display`, derives, `From`/`TryFrom` | `trait-std-conversions` |
| Collection chains (eager, keep keys) | Lazy iterators; `collect` with a type | `coll-collection-to-iterators` |
| `groupBy`, `sortBy`, `unique` | Entry API, `sort_by_key`, `HashSet` (not `dedup`) | `coll-group-sort-unique` |
| Synchronous calls | Lazy futures, `.await`, no blocking | `async-futures-await` |
| Shared state in Redis or cache | `Arc`, atomics, a mutex not held across `.await` | `async-shared-state` |
| Namespaces, PSR-4 autoload | `mod` tree; private by default | `mod-namespaces-to-modules` |
| Composer | Cargo, `Cargo.lock`, features | `mod-composer-to-cargo` |
| PHPStan, Pint | `cargo check`, clippy, `cargo fmt` | `tool-clippy-fmt-check` |
| PHPUnit, Pest | `#[test]`, `tests/*.rs`, doc tests | `tool-tests` |
## Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|----------|----------|--------|--------|
| 1 | Mindset | CRITICAL | `mind-` |
| 2 | Ownership | CRITICAL | `own-` |
| 3 | Types | CRITICAL | `types-` |
| 4 | Errors | CRITICAL | `err-` |
| 5 | Traits | HIGH | `trait-` |
| 6 | Collections | HIGH | `coll-` |
| 7 | Async | HIGH | `async-` |
| 8 | Modules and Cargo | MEDIUM | `mod-` |
| 9 | Tooling | MEDIUM | `tool-` |
## Quick Reference
### 1. Mindset (CRITICAL)
- `mind-compiler-errors` - The compiler finds PHP's runtime bugs; fix the cause, do not quiet it with clone, unwrap or `as`
- `mind-long-running-process` - No per-request reset: never keep per-user data in statics or shared state
- `mind-syntax-habits` - No truthiness; immutable by default; shadowing; blocks are expressions; no default or named arguments
### 2. Ownership (CRITICAL)
- `own-move-not-copy` - Assignment moves `String` and `Vec`; `.clone()` is the explicit copy
- `own-borrow-before-clone` - Take `&str`, `&[T]`, `&mut T`; own only to keep or consume
- `own-closures-capture` - Capture is inferred; `move` for closures that outlive what they borrow
- `own-return-owned-values` - Owned fields and return values in app code; few lifetimes
### 3. Types (CRITICAL)
- `types-null-to-option` - `Option` instead of null; `unwrap_or_else`, `and_then`, `let else`
- `types-arrays-to-structs-and-maps` - Structs for records; `HashMap` order is unspecified; `get` instead of indexing
- `types-strings-bytes-chars` - `len()` counts bytes; byte slicing can panic; `format!`
- `types-numbers` - Overflow panics in debug; `as` truncates; `parse` is strict; no float money
- `types-enums-and-match` - Enums carry data; `match` is exhaustive; avoid `_ =>`
### 4. Errors (CRITICAL)
- `err-exceptions-to-result` - `Result` and `?`; error enums with `thiserror`; `match` instead of `catch`
- `err-panic-unwrap-expect` - Panics are for bugs (and tests); input errors stay errors
### 5. Traits (HIGH)
- `trait-interfaces-and-composition` - Traits with default methods; no inheritance
- `trait-generics-and-dyn` - Generics by default; `dyn` for mixed collections and returns
- `trait-std-conversions` - `Display`, derives, `From`/`TryFrom` instead of magic methods
### 6. Collections (HIGH)
- `coll-collection-to-iterators` - Lazy chains, `collect` with a type, no keys to keep
- `coll-group-sort-unique` - Group with the entry API; in-place sorts; `dedup` removes only neighbours
### 7. Async (HIGH)
- `async-futures-await` - Futures do nothing until awaited or spawned; never block the runtime; durable work goes on a queue
- `async-shared-state` - `Arc`, atomics, `Mutex`; no std guard across `.await`
### 8. Modules and Cargo (MEDIUM)
- `mod-namespaces-to-modules` - Files compile only when declared with `mod`; private by default
- `mod-composer-to-cargo` - `Cargo.toml`, `Cargo.lock`, `cargo add`, features
### 9. Tooling (MEDIUM)
- `tool-clippy-fmt-check` - `cargo check`, clippy groups, `cargo fmt`, narrow `allow` with a reason
- `tool-tests` - Unit tests beside the code, integration tests as top-level files in `tests/`, doc tests
## How to Use the Rules
Read individual rule files for the PHP original, the differences, and the incorrect and correct Rust:
```
rules/own-move-not-copy.md
rules/types-null-to-option.md
rules/err-exceptions-to-result.md
```
Each rule file contains:
- The PHP behaviour, checked on PHP 8.4
- What changes in Rust, with quotes from the official documentation
- Rust written with the PHP habit, and what it does (a compiler error, a panic, a wrong value, or needless work)
- Idiomatic Rust
## References
- [The Rust Programming Language](https://doc.rust-lang.org/book/)
- [The Rust Edition Guide (2024)](https://doc.rust-lang.org/edition-guide/)
- [Rust standard library documentation](https://doc.rust-lang.org/std/)
- [Rust API Guidelines](https://rust-lang.github.io/api-guidelines/)
- [Clippy lints](https://rust-lang.github.io/rust-clippy/master/)
- [The Cargo Book](https://doc.rust-lang.org/cargo/)
- [PHP manual](https://www.php.net/manual/en/)
- [Laravel 13 Collections](https://laravel.com/docs/13.x/collections)
## Full Compiled Document
For the complete guide with all rules expanded: `AGENTS.md`
Files in this skill
- AGENTS.md
- README.md
- SKILL.md
- rules/_sections.md
- rules/_template.md
- rules/async-futures-await.md
- rules/async-shared-state.md
- rules/coll-collection-to-iterators.md
- rules/coll-group-sort-unique.md
- rules/err-exceptions-to-result.md
- rules/err-panic-unwrap-expect.md
- rules/mind-compiler-errors.md
- rules/mind-long-running-process.md
- rules/mind-syntax-habits.md
- rules/mod-composer-to-cargo.md
- rules/mod-namespaces-to-modules.md
- rules/own-borrow-before-clone.md
- rules/own-closures-capture.md
- rules/own-move-not-copy.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.