DEV Community

Cover image for Oops, I Did It Again...I Made A Rust Web API And It Was Not That Difficult

Oops, I Did It Again...I Made A Rust Web API And It Was Not That Difficult

Ben Lovy on November 21, 2021

Over two years ago (oof), I posted a walkthrough of my Rust implementation of todo-mvp by @gypsydave5 demonstrating how to build a simple Rust API ...
Collapse
 
kmack profile image
k-mack

Excellent post! Even though you've demonstrated how using a web framework isn't always necessary, a fun follow-up post would be to show how to write this same application on top of warp (since it is also built on hyper).

Collapse
 
deciduously profile image
Ben Lovy

That's a good idea! I quite like Tokio's axum too.

Collapse
 
nabbisen profile image
nabbisen

Wow, really wonderful😲
std::net::SocketAddr is directly used. Must be lightweight and fast.
I have ever used just f/w such as Rocket and Actix. hyper, a bit.
Thank you for your great article and code😃 Now I feel like learning more about Rust and network... to wholly understand them at first...

Collapse
 
lexiebkm profile image
Alexander B.K.

Recently I started to consider a compiled language for backend. Currently I am still learning Go aka Golang. Actually I want to use Java, but it will take longer time to learn if I have to proceed to J2EE aka JEE, although Spring framework (Spring Boot) will help quick development.
I have read about Rust a couple years ago, but didn't really want to get started, seeing that it would only fit to system or embedded programming like C/C++.
But when I read an article from qvault.io/rust/rust-vs-go-in-backe...
my mind was open again. Still, I think I don't have time to learn Rust. Maybe, after sufficient learning and working with Golang, I will spend time to read its documentation again.

Collapse
 
deciduously profile image
Ben Lovy • Edited

Go is a tough competitor in terms of web servers. In my limited experience, Go really excels in that space above anything else I've used. I think Rust will ultimately provide better low-level control and general expressiveness, but pragmatically Go might be more than sufficient for a wide swath of web service use cases. Java fits the bill too. There's a lot of types of applications for which Rust is probably overkill.
Once you've invested the time and energy to get over the hump it's likely capable of comparable productivity, but the learning curve is definitely steeper, so you have to decide up front if it's worth the time and energy.

If you're just curious, I mean, yeah. Come on over :)

Collapse
 
lexiebkm profile image
Alexander B.K.

I have visited the Book again. And like the last time I read a bit about it, I jumped and read a bit about its unique thing : Ownership. I like reading the section that discussed Stack and Heap; this at the same time reminded me of C++ that I wanted to relearn in the correct way (like reading from Stanley Lippman's book of C++ Primer).
I am also glad seeing that Rust provides Struct like in C/C++, and how it defines methods in the context of Struct, Enum or Trait, although it doesn't provide Classes and Inheritances via classes. This way of defining methods immediately reminds me of how Go defines methods using Receiver argument in the context of certain types such as struct.
Therefore, I feel like at the same home when trying to learn Rust in parallel with Go, and actually C/C++ too, since they provide structs and pointers.

Collapse
 
qm3ster profile image
Mihail Malo • Edited

I took the liberty of messing with this project.
Please take a look at the individual commits, who knows, maybe you will like something: github.com/deciduously/todo-mvp/pu...

I know you didn't want JavaScript, but I provided a fallback for the bad people :v (Yes, the fallback is worse)

But the real gains would be caching the CSS and SVGs, of course.

Collapse
 
rduggan profile image
Robert Duggan

Umm, honestly this stuff is way above my head right now. But I do have a question, one you probably get a lot. Which font are you using in your terminal?

Collapse
 
deciduously profile image
Ben Lovy

Yep, I'm definitely assuming familiarity with Rust for this post :) If you're curious, I recommend starting with The Rust Programming Language, available for free!

I currently use JetBrains Mono in both my terminal and my text editor. I also like Source Code Pro.

Collapse
 
peerreynders profile image
peerreynders

I recommend starting with The Rust Programming Language, available for free!

This recommendation is very common but the typical audience will usually be disappointed about the level of competence that will be attained after working through a ~500 page tome.

I think this old tweet gives a clearer account of the journey to basic Rust competence:

IMPORTANT: when you are done reading this book you might think “I get it now” but really, you don’t. The official book does not go nearly deep enough — it’s a great intro but after this you will be smack in the middle of the “got the gist” phase.

1 The Rust Programming Language
2 Rust By Example
3 Programming Rust (Another ~600 pages)
4 This is where you start writing your own code.

Also:

Thread Thread
 
deciduously profile image
Ben Lovy • Edited

Sure, Rust is a complicated topic, but I still recommend starting there. You have to start somewhere. It worked for me, at least.

Collapse
 
qm3ster profile image
Mihail Malo

Pls don't call "spawned tasks" "threads".
Think of the children, this will absolutely destroy them.

Collapse
 
deciduously profile image
Ben Lovy

Whoops, absolutely correct thanks! I'll fix the post :)