BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Loco is a New Framework for Rust Inspired by Rails

Loco is a New Framework for Rust Inspired by Rails

This item in japanese

Loco, a new Ruby on Rails-like framework, enables developers to write MVC-style web applications in Rust. Rust's language features, such as concurrency, safety, strong typing, and performance, are some of the advantages over Rails or its derivatives. However, creators of Loco are focused on Rust developers looking to build MVC-style applications easily and without the need to look elsewhere for a familiar developer experience. 

Rust has a comprehensive list of libraries and frameworks like Axum, Actix, Rocket, Tokio, Warp, and Reqwest. However, Loco is the first framework in the Rust ecosystem of its kind. Loco 0.3.1 is the latest version, with 16 minor versions shipped since November 2023. Loco has its own CLI, an app creation wizard, and a local development server, and follows the same principles as Rails but for developers programming in Rust.

Loco is inspired by "The One Person Framework," a blog post by David Heinemeier Hansson that promotes the idea of a toolkit powerful enough for a single individual to create modern applications. A single developer can only spend a little time on all the complex processes of taking an application to the production stages. Important decisions like the selection of libraries, robustness, and scalability of the architecture are all but some of the challenges that require time and effort. The "One Person Framework" takes those decisions for the developers by bundling them into the framework. However, the tradeoff is a potential lack of flexibility, and this implementation style relies heavily on conventions and patterns. 

In the case of the Rails framework, this approach has been widely applauded over the years. The uptake of Rails was phenomenal and has inspired many other frameworks. One is Grails, based on Groovy, a language that runs on the JVM (Java Virtual Machine). Loco aims to provide this functionality for Rust developers. 

Loco can be installed idiomatically within the Rust ecosystem:

$ cargo install loco-cli

A new Loco app can be created similar to `rails new`:

$ loco new 

Controllers and routing also work in a similar fashion, and the framework uses Axiom. The following function returns a JSON response:

async fn current() -> Result<Json<HomeResponse>> {
    format::json(HomeResponse::new("loco"))
}

A list of routes can be defined as follows, also similar to how a developer would define them in Rails.

pub fn routes() -> Routes {
    Routes::new()
            .add("/", get(current))
            .add("/loco", get(current))
.add("/:id", get(get_one))
}

One of the powerful features of Rails is the ability to generate models and tie them together with a REST API or the front-end views and controllers. Loco uses `sea_orm` for models. To generate a model is also easy with the following example command.

$ cargo loco generate model books title:string isbn:string

Defining behavior is also quite straightforward with `ActiveModelBehaviour,` with the possibility of defining before and after functions. Furthermore, it also offers database migrations. 

$ cargo loco generate migration add_web_url

Loco also offers a testing process that developers can enable in the `Cargo.toml` as follows.

[dev-dependencies]
loco-rs = { version = "*", features = ["testing"] }

The authentication process is also quick to set up with default User entities and secure APIs with JWT. It also has the commonly used functions in the default API, e.g., register, activation, reset password, etc. 

When the app is created as a `Saas app` via the `loco-cli` it generates a starter with predefined routes. E.g. 

$ cargo loco routes
 .
 .
 .
[POST] /auth/forgot
[POST] /auth/login
[POST] /auth/register
[POST] /auth/reset
[POST] /auth/verify
[GET] /user/current

Loco builds on Rails's experience and Rust's robust language features. It integrates nicely with the Rust ecosystem, offering model generation, behavior definition, migrations, testing, and more. The project is relatively fresh, yet it's another exciting addition to web frameworks in Rust.
 

About the Author

Rate this Article

Adoption
Style

BT