Developing a web app with rust: first steps

Francis Stephan
3 min readMay 25, 2024

--

This the first instalment of a series dedicated to developing a basic CRUD web application with rust. In it I shall describe the motivation and general design decisions. Further instalments will deal with the web framework (actix_web), the database framework (sqlx) and the templating system (tera).

1. Why rust ?

While learning Go language 2 years ago, I discovered it included out of the box all that was needed for a web application on the server side. So I wrote a Go + Gin REST tutorial as well as 2 small applications based on that tutorial, the last one being Chinese character dictionary management

But then I decided I wanted to port that last application to Rust language. Why is that?

  • I very much appreciate rust’s “0 cost abstractions”, allowing for high level language features with functional programming concepts such as Option / Result, mapping over a vector, destructuring and pattern matching. Go provides some substitutes for these concepts, but not enough for my taste.
  • I do not like garbage collectors.

I still think that Go is excellent for learning about web applications and for prototyping them.

2. Design options

While Go comes with all the required software (plus the Gin web framework package which I came to really appreciate), when arriving inrust country, you are on your own…

So I spent a few months reading all I could on web development with rust — once I had read “The rust programming language”, of course. Among the books I read the most helpful were :

  • Zero to production in Rust — an opinionated introduction to backend development, by Luca Palmieri. This is indeed an opinionated book, so I had to come back to it after I read the following:
  • Code like a pro in Rust, by Brenden Matthews (Manning), which dedicates only one chapter to ‘Building an HTTP REST API service’. Complete that chapter, then come back to the Palmieri book, which will make more sense.

After all that reading, I knew I had to get my feet wet. I already had the overall design of my program, based on the Go version. What I needed was to select the adequate libraries for web framework, database interface, templating and front-end framework:

  • web framework: I chose actix_web, based on Palmieri’s recommendation, on the excellent actix_web documentation available and on several resources mentioned in the comments of my source code;
  • templating: I chose Tera for the same reasons, noting that it looked to play well with actix_web;
  • database interface: I chose sqlx, mainly because it directly relies on SQL, making it much more directly usable. I did not, however, use the compilation time checking which looks to be one of the strong points of sqlx; sqlx has the drivers necessary for my sqlite3 database, which is the same as for the Go version;
  • front-end framework: I used htmx which has been my option for the Go version of the program. Actually htmxworks with Rust without any difference, so all the considerations I developed in my htmx article are still valid here.

3. Program layout

I adopted the same layout as for the Go version, except that I chose to have a separate module for handlers, making fo a cleaner code, with the following structure:

In this layout, main.rs defines the overall structure of the program, defining both the application data and the various handlers, with three modules:

  • handlers.rs for the various routes,
  • dbase.rs for database access,
  • forms.rs defining most of the forms used by the program (most of them having the same name and function as in the go version).

The vol directory contains various assets (css, pictures), templates (html files) and the zidian.db sqlite3 database.

The next section will be dedicated to implementing the actix_web framework for the project.

--

--