Build a CRUD API with Rust

Sean Wragg
Sean’s Blog
Published in
9 min readMar 19, 2018

Since my initial Node/Rust REST comparison, I’ve wanted to follow up with a comprehensive guide for getting simple CRUD operations up and running in Rust.

While Rust has its complexities, coming from Node.js, one thing that’s kept me attracted to the language is its simplicity. It’s sleek and strikes a great balance between safety and cognitive intuition.

In any event, hopefully this guide helps those that are new to Rust and encourages those who are on the fence.

Scroll down if you’d like to see how Rust compares against Java and Node.js

Major frameworks used

  • Rocketweb framework for writing fast web applications
  • Serdeframework for serializing and deserializing Rust data structures
  • Dieselsafe, extensible ORM and query builder

Creating our application in Rust

First, we’ll create a new Rust project using the commands below.

[sean@lappy486:~] $ cargo new hero-api --bin && cd hero-api
Created binary (application) `hero-api` project

Before we move forward though, Rocket requires us to use the nightly Rust build. Thankfully, there’s a quick command we can use to switch channels.

$ rustup default nightly
$ rustup update && cargo update

You’ll get some output here regarding the switch but, can confirm it was successful by invoking the --version flag on cargo and rustc

$ cargo --version && rustc --version
cargo 1.26.0-nightly (5f83bb404 2018-03-09)
rustc 1.26.0-nightly (55c984ee5 2018-03-16)

Adding our first Rust dependency (Rocket)

Alright, within our hero-api directory, you’ll find a Cargo.toml and src folder. Cargo.toml will act similar to Node’s package.json which we’ll cover later — and I suspect src is descriptive enough :)

We’ll need to add Rocket as a dependency by editing Cargo.toml and adding the following two dependencies. There’s certainly more to add later but, this will at least get us started.

rocket = "0.3.6"
rocket_codegen = "0.3.6"

Now we can edit src/ — we’ll just borrow the starter code provided to us by to ensure everything works at this point.