No BS Guide to Using Rocket With Sled for a Production App (Rust)

Alright let’s cut right to it. If you’re using a different version of rocket (0.4) or sled (0.34) then 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

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

Advice 2

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

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_user, update_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.

Final words

If you are really trying to build a production app in Rust+Rocket+Sled, my best advice is 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 checking in! 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.

To learn more about me, check out my website, and follow me on YouTube or Twitter.