And even if the performance difference in real world scenarios is half of what we see here it's still pretty incredible. Ruby on Rails has this legendary reputation and it's been used by quite a few successful businesses, but Ruby tends to not do amazingly well in performance benchmarks. So since Rails has this amazing developer experience that people rave about and Rust has a better story around performance and safety, it does make sense to try to combine the two to get the best of both worlds.
What Loco.rs is and initial questions
And that is the premise of the Loco.rs crate. If you think that's a crazy idea, it is called Loco, so at least give it credit for being self-aware. It says Loco is strongly inspired by Rails. If you only know Rails and are new to Rust you'll find Loco refreshing. We do not assume you know Rails. That's great because I don't know Rails. I am somewhat familiar with Ruby but I've never actually used Rails, so there's some pretty obvious questions to ask here. Right, is the Loco.rs developer experience as legendary as the Rails experience? Does it actually perform better than Ruby on Rails? Does it feel exactly like Rails or are there differences? How does it compare to Leptos and Dioxus? Who created it? And of course, should you use it for your next web application? We're going to cover all of this. First we're going to dive straight into the code and then we'll kind of zoom out and look at the bigger picture.
MVC overview and template comparison
Now Rails is a server-side MVC framework and it was around before the front-end reactive framework craze in the mid-2010s that was led by frameworks like AngularJS and ReactJS, and those kind of encourage you to have a lot of your business logic on the client side. Again, Rails and Loco are primarily focused on server-side rendering. Let's take a look at an example Rails project side by side with an example Loco project that does exactly the same thing. They're both just basic CRUD apps to create pages in your web app. With Rails or Loco you create these templates with an HTML-like syntax. We can interpolate values into the Rails template like this, and this is what it looks like to do the same thing with Loco.rs. Things look a little different between the two, but the high level idea is pretty much the same. Rails uses its own template engine and Loco uses the Terra templating engine which kind of works in a similar way. This template is the view or the V in MVC. It's focused on how things are going to be displayed to the user. The post variable we're referencing in these views is the model or the M in MVC. It's a piece of data that the view can present to the user in some way. In this case it's a collection of posts that we're iterating over and displaying to the user. How is that data populated? Well, that happens in the controller which is the C in MVC. In Rails that would be the Ruby code. In Loco.rs that would be the Rust code. In both cases we're loading some data from a database and populating the model with it. Each of these functions in the controller is called a controller action and corresponds to a specific URL the user's browser is navigated to. In the case of Loco.rs we can see the routes for this controller are set up here, and for each URL path we specify a function to be run.
Getting started: CLI, generators, and batteries included
To get started with Loco first install the CLI tool with cargo install loco-cli and then to create your Loco-based project run loco new and you get a nice project creation wizard asking you a few questions. You know this is a batteries-included framework when you have the option to set up a new project with a database and user authentication already integrated, and actually that could be a pretty big deal. The authentication mechanism you get out of the box is complete with welcome emails, password recovery, and all the standard stuff. The other distinguishing feature of Rails is its rich command line system. Once your project is created you can run these simple commands like rails generate scaffold to set up the files and boilerplate code for a new data type. In the command I can just specify the name of the entity along with the fields and types it has and it automatically generates all of the template files, the model, view, and controller files, and even the database tables and fields. Loco.rs aims to replicate the system as well and cargo loco generate basically does the same thing. You can see it creates and applies these database migrations under the hood. This is actually using a crate called SeaORM which is currently kind of the golden child of the ORM in the Rust ecosystem. The default ORM used by Rails is called ActiveRecord and it's actually part of the Rails project in general. Loco tends to leverage other Rust crates instead of reinventing the wheel, which is not to say that Rails reinvented the wheel because, well, I don't think the wheel existed yet when they were starting out. It's been around for a while. Those are some technical details and we'll get to performance in a minute.
Backstory: creator, evolution, and motivations
But now let's zoom out for a second because Loco actually has a pretty interesting backstory. It was created by Doorn Naham who created a data security company called Spectral which was acquired by another company called CheckPoint, so clearly he's got some business acumen. Doorn wrote a really great article about his journey and rationale for creating Loco; I'll link it in the description if you'd like to check it out, but here are some of the highlights. One interesting thing is that it was not his first attempt at making a Rails-inspired framework in another language. As he says, believe it or not Loco is a third attempt at building a Rails alternative which is not based on Ruby. Talk about persistence, the resilience kind, not the database kind. He says the first iteration was originally built on Node.js which became Hype.rs or Rails on TypeScript, but then apparently things went downhill. He got frustrated with the JavaScript ecosystem, updates constantly breaking his project. So then he tried the same approach with Rust and had a better result, and then he goes on to kind of revel at the performance numbers he was able to get with Rust. One of the things I thought was really cool is that instead of touting his framework as being objectively better than Rails he actually goes out of his way to suggest that people continue to use Rails if they love Ruby. So there you have it, that's a little slice of the Loco backstory. Again, great article link in the description if you want to check it out.
Now Doorn does refer to great performance a lot in this article so let's dive a little more into that. According to benchmarks on the Loco.rs page it is quite a bit more resource efficient than Rails. They show it can handle an order of magnitude more requests per second than Rails, presumably on the same hardware. One of these benchmarks includes a database query, the other does not, but in both cases Loco handles an order of magnitude more requests per second than Rails. I did not verify these benchmarks myself, but if we take them at face value they're pretty impressive and even if the performance difference in real world scenarios is half of what we see here it's still pretty incredible. There's a pretty clear value proposition here. When you start talking about the cost of cloud hardware, maybe not as big of a deal for large companies where cloud infrastructure costs aren't as big of a cost center, but for any developers and smaller companies using Loco instead of Rails can make a ton of sense because in those situations this difference in performance might represent a pretty significant savings in cloud infrastructure costs. Okay, now taking a step back and looking at all of this after looking at Loco I think I understand the whole Rails mindset now. It is the ultimate opinionated batteries-included framework and I can easily imagine scenarios where that is exactly what is needed. With the added safety and performance of Rust I think Loco.rs can be an obvious choice in a lot of situations.
How does it compare to other Rust web frameworks like Leptos and Dioxus? Well first of all Leptos is my absolute favorite way to build web applications these days so I'm a little biased. But out of the box Leptos does not have a lot of the things that Loco.rs has: the pre-built command line code generation and so on. What Leptos does have though is a lot more flexibility on whether you'd like a given piece of logic to run on the browser or on the server. When you want to move business logic back and forth between the client and the server it's almost effortless. Not only that, but the interface between the client and the server is automatically generated for you, and from the developer perspective you're just defining server functions and invoking those functions directly from the client-side code. Because Loco inherits the Rails approach there's a very clear demarcation between the frontend and the backend code. By default Loco projects have their frontend in a separate directory and by default it is ReactJS-based. Of course you can swap that out for a Rust-based frontend framework if you'd like, but if you're going to do that it might make sense just to use Leptos or Dioxus instead. If you want your business logic to run on the server side anyway or you're planning to write your frontend in TypeScript none of this is going to be an issue and Loco can make a lot of sense. Before we go I do want to talk about this course I've been taking on quantum computing and it's from the sponsor of this video which is Brilliant. I've been working my way through it and so far it's been really, really good. Some of the concepts in quantum computing are pretty challenging and the Brilliant course has really helped me understand them. Brilliant distinguishes itself by putting an emphasis on learning by doing. If quantum computing is not your thing they have thousands of interactive lessons in math, data analysis, programming, and AI skills that can really help give you an edge in this industry. One of the really nice things about Brilliant is that it makes mobile devices first-class citizens so you can jump back into your favorite courses when you're on the go instead of, you know, mindlessly scrolling Tech Twitter or something. To try everything that Brilliant has to offer for free for 30 days visit brilliant.org codetothemoon or scan the QR code on screen or you can click on a link in the description. You'll also get 20% off an annual premium subscription. Thank you so much to Brilliant for sponsoring this video. If you're interested in other Rust crates that are kind of making waves, definitely check out this video about the Colossus crate which is currently my favorite Rust crate for working with large language models. Let me know in the comments what you think of Loco.rs. Other than that thanks for watching and we'll see you in the next one.