Backend 5 min read 940 words

Laravel Rust: the same philosophy, a different shell

ES
Laravel Rust: the same philosophy, a different shell

The hermit crab doesn’t make its own shell: it finds empty ones along the beach and, when its current one gets too small, it moves into a bigger, sturdier one. It’s still the same crab, with the same habits, just carrying a different house around.

It came to mind with yesterday’s announcement from Taylor Otwell: Laravel Rust, the first beta of Laravel rewritten in Rust. It isn’t a layer over PHP or a literal port, it’s Laravel’s philosophy (conventions, batteries included, pleasant APIs) moving into a compiled language. And since this blog has talked quite a bit about Laravel and PHP, it felt like a must to take a calm look at it.

What it is

Laravel Rust is built on Axum and Tokio, so everything is asynchronous: HTTP requests, the database, queues, Redis, S3 or WebSockets. According to the announcement, it’s near feature parity with Laravel PHP, and the little that’s missing will arrive in the coming weeks: routing, middleware, sessions, authentication, models, migrations, queues, mail, broadcasting with Reverb (also written in Rust), Inertia with React, Vue or Svelte, and something the announcement presents as new in Laravel: durable workflows, processes that can last days or weeks.

As for performance, according to their internal benchmarks on Laravel Cloud, it runs 18 times faster than Laravel PHP with considerably less CPU and memory. It’s their figure, so we’ll have to wait for independent tests, but with Rust and async end to end it’s not surprising either.

What you notice reading the code

What I liked most is that you recognise Laravel straight away. A form to create a post, with validation and authorization, looks like this:

#[laravel::input(fills = Post)]
#[validate(authenticated)]
pub struct StorePost {
    #[validate(required, max_length = 255, unique(Post::title))]
    pub title: String,
    #[validate(required)]
    pub body: String,
}

pub async fn store(
    Authenticated(user): Authenticated<User>,
    Valid(input): Valid<StorePost>,
) -> laravel::Result<Redirect> {
    let post = create!(Post { user: &user, ..input }).await?;

    Ok(redirect!(show, &post))
}

Anyone who has written a FormRequest in Laravel recognises everything: the rules, the authenticated user, the redirect to the post’s route. The difference is in what the compiler now checks:

In Laravel PHPIn Laravel Rust
Forgetting a FormRequest’s authorize()If the input doesn’t declare who may submit it, it won’t compile
Mass assignment with a badly configured $fillableOnly the fields declared in the struct get filled
An N+1 query from a relationship loaded by accidentRelationships are never loaded on their own, you ask for them with with
A misspelt column name that fails in productionColumns, operators and types checked at compile time
A password that ends up in a logPassword and PasswordHash types are redacted in logs and JSON
A hand-written URL that breaks when a route changesroute! and redirect! checked at compile time

And hand-written SQL is covered too: the db::query! macro looks like it interpolates variables ({active}, {...roles}), but it actually turns each one into a query parameter, so SQL injection is ruled out by construction.

The agents part

This is what I find most interesting about the announcement, beyond the framework itself. Laravel Rust is designed, in their own words, for a world where “humans will increasingly neither write nor manually read application code at scale”. Every new project ships with skills so agents know how to write and test Laravel Rust, and in the companion announcement Taylor Otwell says it bluntly: he hasn’t written a line of code by hand in a year, and “we’re sort of post-programming language”.

The reasoning makes sense. If the code is written by an agent, a strict compiler is a great safety net: many of the mistakes an agent makes in PHP (which you’d only see in production) simply don’t compile here. It’s the same idea I mentioned with Psalm 7 turning security analysis on by default, taken to the extreme, and the natural continuation of Laravel Boost’s skills.

And some reassurance for those of us who live in PHP: on the same day they announced Agent Kit and Converse for Laravel PHP, and Laravel Cloud now also deploys Rails, Django, Go and Rust. It doesn’t look like a farewell to PHP, rather Laravel wanting to be a way of working more than a language.

What I’d look at before jumping in

It’s a beta, and although I love the idea, there are things worth keeping in mind (these are my own notes, not from the announcement):

  • The ecosystem. Much of Laravel PHP’s appeal is the community packages, and that doesn’t get ported in a few weeks.
  • Rust isn’t PHP. Compile times, the borrow checker and the error messages are a different world. If the agent writes the code, maybe it matters less, but someone will have to understand it when something breaks.
  • The not-reading-code part. I think it’s great that the compiler catches mistakes, but a compiler doesn’t know whether the business logic is right. I still think AI is a very fast intern who needs supervising, and to supervise you have to understand what it does.
  • The installation. It’s installed with curl ... | sh, like so many tools. After what we saw with Convex’s skills, it doesn’t hurt to read the script before running it.

If you want to try it:

curl -fsSL https://laravel.com/install.sh | sh
laravel new example-app --rust

In the end, the hermit crab changes shell when the old one gets too small, not before. Laravel Rust is a very well-made new shell, and for projects where performance or compiler guarantees really matter it may be just what was needed. For everything else, the PHP shell is still a great home, and now it has new neighbours too.