Learning rust, implementing a webserver
Introduction
I wanted to learn rust months ago. I remember attending a talk in Pittsburgh several years ago about rust and I liked the idea that instead of re-implementing the wheel, it tries to solve issues other new languages never try to fix (or did it in an efficient/ineffective way).
I spent some time over the last two weeks to learn the langage and complete a project in rust: a webserver. Nothing really fancy, it was rather a way to learn the language. I believe the best way to learn a new language is to complete few new projects.
This is the result: I am able to run a webserver that can host this blog, as shown in the picture below.
And this is basically how to launch it:
In this article, I will talk about this project and share some thoughts about rust.
A web server in rust
Specifications
I wanted to implement a simple webserver in rust to understand/practice/use the language. The goal was to be able to have a server able to show a static website. This is an experiment and this should absolutely not be used in production. This project is useful if you are new to rust and/or want a codebase about networking features in rust (such as using the mio or getops crates).
Non goals: full http support, large file handling, security (you can access the full FS ).
Get the code and run it
The code is publicly available on github at: https://github.com/juli1/rust-webserver
# git clone https://github.com/juli1/rust-webserver.git
# cd rust-webserver
# cargo build && cargo run -- -p 9065 -l 127.0.0.1 --rootdir `pwd`
Then, open your browser and go to http://localhost:9065/Cargo.toml. You will be able to see the file from the source directory.
Details of the code
The code is separated into four files:
server.rs
is about all the server part to listen on the socket, communication handling and so on.client.rs
is about handling request/reply for a particular connection.protocol.rs
contains all the code with all the data structure related to the requests and replies.main.rs
checks the options passed to the command line, create a server and … nothing else!
The code does not handles all HTTP headers. The server supports HTTP/1.0 only. The parsing of command is very basic. The server supports basically two file types: html, png and jpg. Yes, this is very basic but met my goal: it was an awesome way to learn rust and I loved it!
Feedback about rust
I really like rust. At first, the language brings a lot of frustration because of all the restrictions and scope, borrow, etc. You really need to undertand these concepts to make any progress. Once you pass the first hours digging online about the errorr messages (that are pretty clear by the way), you realize how the language help you to bring more assurance that your code is safe and free of errors. It reminds me several aspects of Ada.
Rust is what we should be expecting for the next generation of language to do low-level/system programming (program like webservers, file servers, etc.). It will probably not be the language I will use if I design a web application. On the other hand, this will probably be my choice if I need to do a web-server, file server or any program and needs to be safe, reliable and performant.
Rust: pros
- Compiler error messages: Debug messages are clean and useful. We are very far from C++ compilers that produces a tons of useless error messages that hide the cause of the issue. When starting in Rust, you will have a tons of compilation errors but error messages will be easy to understand to fix your program and make progress.
- Speed: programs written in Rust seems fast. I did not benchmark but this seems very promising.
- Predictability: rust reminds me Ada: it is painful to write your first program because of all the language restrictions. However, once it compiles, it generally works as expected. I had very few crashes during the development of the webserver but I had a ton of error messages before it even starts working.
- Ownership : the concept behind ownership is painful at first but after a while, it is easy to understand how powerful it can be.
Rust: cons
- Lifetime: the idea behind lifetime is really powerful and useful. The language needs have a way to annotate the lifetime of objects/variables but the syntax is horrible and the whole mechanism is ultra verbose.
Useful resources
These are some resources I used during my search for information on rust.