Using Rocket With Sled for a Rust Production Web App

I used rust with rocket and sled to build a production web app. I learned a lot of lessons when building it. This post acts as a guide for anybody who wants to do the same.

If you’re using a different version of rocket (0.4) or sled (0.34) than me then some of this stuff won’t be valid. Consider this article a checklist of things you want to consider when building a rocket+sled app.

We all love Ferris

Advice 1 – Manage Sled Database

Use rocket::ignite().manage(Database{ ... }) when initializing rocket to have access to your database on all routes.

You can add db: State<Database> as an argument to whatever route you define to have access to your DB.

Advice 2 – Dealing With Sled IVec

Sled’s IVec’s are kind of difficult to deal with at first.

The way I like to think of it is that IVec is similar to binary. Basically, you gotta explain to sled how to store your structs in the DB.

Some types already have a as_bytes method on them (String for example).

But for more complicated structs you have to explain yourself to sled. Thankfully it’s quite easy with thebincodelibrary, which you can slap on a struct as an impl method named as_bytes or similar.

Then for deserializing, you need to explain to sled how to go from bytes to Rust types.

Some types already have this implemented (String from_utf8 for example).

But for more complicated structs you gotta (again) do it yourself. You can again use bincode for easy deserializing.

Tip: consider implementing Rust’s From traits for both ways

Advice 3 – Use Trait Extensions

Standardize your database calls by implementing a DatabaseExt trait for State<'_, Database.

You’re constantly going to be needing the same DB information across different routes, so why not standardize it?

Specifically, you need to create a DatabaseExt trait with methods you use constantly like get_userupdate_user etc.

Then you implement that trait on the external type (in this case impl DatabaseExt for State<'_, Database>).

And from now on you can call `db.get_user()` or any other method you’ve implemented.

Most Important Thing For Rust Production Web App

The truth is that there are very few articles out there explaining the issues that you’ll be dealing with on your journey. But that’s OK. Figure things out.

If you are really trying to build a production app in Rust+Rocket+Sled, my best advice is to go look at code that’s already out there and analyze the hell out of it. Take a look at my repo or Dev and dev’s repo (which I used heavily myself). Go back to previous commits and understand what’s going on.

And if you have any questions, feel free to ask the community! (or me)

Thanks for reading! I’m Davide, a 19-year-old self-learner who runs The Feynman Mafia, exploring how learning by explaining can be used to teach yourself any topic.

If you want to follow along on my journey, you can join my newsletter, check out my website, and follow me on YouTube or Twitter.