Discuss a project

Blog / Backend

Loco: the comfort of Rails, the speed of Rust

Loco is a modern web framework for Rust inspired by the philosophy of Ruby on Rails. Its main idea is to give developers a "Rails-like" DX (developer experience): generators, MVC architecture, convention over configuration, built-in infrastructure and a fast start.

If Rails historically made web development productive in the Ruby world, Loco is trying to do the same for Rust: keep the comfort of development, but add memory safety, high performance and strict typing. For more on choosing Rust for production, see our article on apps on Rust and Zig.


Why Loco appeared in the first place

For a long time Rust was associated with low-level services, async infrastructure and complex backend code. For web development there were:

  • Axum
  • Actix Web
  • Rocket

But they are all more like "construction kits" than full-fledged batteries-included frameworks.

Loco is built on top of Axum and tries to provide:

  • a ready-made project structure;
  • MVC;
  • code generation;
  • ORM integration;
  • caching;
  • background jobs;
  • authentication;
  • migrations;
  • CLI tools.

In other words, everything Rails developers got used to many years ago.


Loco architecture

Loco uses the classic MVC architecture:

  • Models — working with data;
  • Views — presentation;
  • Controllers — handling HTTP requests.

This closely resembles Rails.

Project structure

A typical project looks roughly like this:

src/
   ├── controllers/
   ├── models/
   ├── views/
   ├── workers/
   ├── tasks/
   ├── middleware/
   └── app.rs

Very similar to Rails:

app/
   ├── controllers/
   ├── models/
   ├── views/

Developers do not have to invent the project architecture — the framework sets the conventions.

Models in Loco

Loco is tightly integrated with SeaORM, one of the most popular ORMs for Rust.

Why SeaORM

SeaORM suits Loco well for several reasons:

  • async-first architecture;
  • strict typing;
  • entity generation;
  • PostgreSQL/MySQL/SQLite support;
  • integration with migrations;
  • an ActiveRecord-like style.

Unlike Rails ActiveRecord, SeaORM is more explicit and type-safe.

Model example

use sea_orm::entity::prelude::*;

  #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
  #[sea_orm(table_name = "users")]
  pub struct Model {
      #[sea_orm(primary_key)]
      pub id: i32,

      pub email: String,
      pub password: String,
  }

The similarity with Rails is visible here:

class User < ApplicationRecord
  end

But in Rust everything is more strictly typed.

ActiveModel and data changes

SeaORM uses the concept of ActiveModel.

Example of creating a user:

let user = users::ActiveModel {
      email: Set("test@example.com".to_string()),
      password: Set("secret".to_string()),
      ..Default::default()
  };

  user.insert(db).await?;

Rails makes this simpler:

User.create!(
    email: "test@example.com",
    password: "secret"
  )

But Loco/SeaORM win in:

  • compile-time checks;
  • type control;
  • performance;
  • no surprises at runtime.

Controllers

Controllers in Loco are responsible for HTTP routes and business logic.

Example:

pub async fn list(State(ctx): State<AppContext>) -> Result<Response> {
      let users = users::Entity::find()
          .all(&ctx.db)
          .await?;

      format::json(users)
  }

This is very reminiscent of Rails:

class UsersController < ApplicationController
    def index
      render json: User.all
    end
  end

But under the hood Loco uses async Rust and Axum.

Views

Loco supports views, although API projects more often use JSON.

You can use:

  • HTML templates;
  • JSON responses;
  • server-side rendering;
  • API-only mode.

Templates usually rely on Rust template engines:

  • Askama
  • Tera

Code generation through the CLI

One of Loco's key "Rails features" is its powerful CLI.

Rails is known for its generators:

rails generate model User
  rails generate controller Users

Loco offers a similar workflow.

Generating a project

cargo loco new

or

loco new myapp

The framework creates:

  • the project structure;
  • configs;
  • controllers;
  • models;
  • migrations;
  • routes.

Generating models

cargo loco generate model User

The CLI can automatically create:

  • a SeaORM entity;
  • a migration;
  • boilerplate code.

This speeds up development significantly.

Generating controllers

cargo loco generate controller Users

It creates:

  • routes;
  • a controller file;
  • handlers;
  • a template for CRUD operations.

Very similar to Rails generators.

Generating views

Loco can also create views and templates through the CLI.

For example:

cargo loco generate scaffold Post

The scaffold approach is directly inspired by Rails.


Built-in infrastructure: cache, workers and configuration

Built-in cache support

Loco includes built-in caching infrastructure.

This is an important difference from "minimalist" Rust frameworks, where you have to assemble the cache by hand.

Supported:

  • in-memory cache;
  • Redis;
  • an asynchronous caching layer.

Cache usage example

ctx.cache.set("key", value).await?;

Reading:

let value = ctx.cache.get("key").await?;

This is reminiscent of the Rails cache store:

Rails.cache.write("key", value)
  Rails.cache.read("key")

Background jobs and workers

Like the ActiveJob/Sidekiq ecosystem in Rails, Loco supports workers.

Structure:

src/workers/

You can run:

  • email sending jobs;
  • asynchronous jobs;
  • scheduled jobs;
  • background data processing.

Configuration

Loco uses a layered configuration system:

config/
   ├── development.yaml
   ├── production.yaml
   └── test.yaml

Almost identical to Rails environments.


What Loco borrows from Rails

1. Convention over configuration

Minimal manual setup.

2. CLI generators

Automatic boilerplate generation.

3. MVC

A clear separation of responsibilities.

4. Batteries included

Many features "out of the box".

5. The scaffold approach

Fast CRUD development.


Where Loco differs from Rails

Performance

Rust is significantly faster than Ruby.

Loco is a good fit for:

  • high-load APIs;
  • real-time systems;
  • microservices;
  • low-latency backends.

Safety

Rust guarantees:

  • memory safety;
  • no data races;
  • fewer runtime errors.

Rails largely relies on runtime checks.

Complexity

Rails is simpler for beginners.

Loco inherits the complexity of Rust:

  • ownership;
  • lifetimes;
  • the async ecosystem;
  • generics.

The barrier to entry is higher.

SeaORM vs ActiveRecord

Comparing the ORMs is the key point.

Feature SeaORM ActiveRecord
Typing Strict Dynamic
Async Yes Limited
Compile-time checks Yes No
DX Good Excellent
Performance High Medium
SQL flexibility High Medium

ActiveRecord is more "magical".

SeaORM is more explicit and safe.


When to choose Loco

Loco is especially interesting if you need:

  • Rust and high performance;
  • a Rails-like DX;
  • strict typing;
  • an async backend;
  • convenient CRUD;
  • a modern architecture.

If you are choosing Rust as the main language of your project, take a look at our comparison of Rust and C++ strategies.

When Rails is still better

Ruby on Rails remains stronger in:

  • ecosystem maturity;
  • the number of gems;
  • MVP development speed;
  • hiring developers;
  • documentation;
  • community.

Rails is a time-tested framework with a huge history.

Loco is still young.


Summary

Loco is one of the most interesting attempts to bring the Rails philosophy into the Rust world.

The framework combines:

  • MVC
  • generators
  • SeaORM
  • cache
  • workers
  • conventions
  • modern async Rust

The main idea of Loco is to make Rust web development not only fast, but also convenient.

If Rails gave developers "developer happiness", then Loco is trying to give:

developer happiness + the performance of Rust + type safety.

Shall we discuss your project?

Tell us about your task and we will come back with an estimate of the time and the cost within two working days.