Rust from a hackers point of view
I’ve been learning Rust after my day job for few weeks. As any curious human being I was really keen to see whether it can do things I currently use python for. These are simple tasks that make me work more efficiently. For example, if people need some metrics on a specific component that has gone into production recently, instead of pulling them manually each day I’ll write something to help me out. So I’ve written something along those lines recently in python and I was curious to see if I can port it to Rust. By no means I’m comparing Rust to Python languages or their communities. I still love python and I can see the same sort of elegance in Rust as in Python.
I’m still learning Rust but roughly 2 days ago I still had just an idea to port code and no code! I started looking up on Google to understand Rust’s way of doing threading and watched this amazing screencast again (highly recommend it if you’re starting to play with Rust) I’ve managed to get something up and running already. I must admit with python this might have taken only about 2 or 3 hours.
So here is the python version of code,
The basic idea is to allow users to access metrics (bacons and sausages) which are in log files via HTTP call.
It has a thread that looks up files in a directory, gets data from each file and then pushes it to a queue. Another thread reads this queue and then updates a dictionary which is shared with HTTP thread. The consumers of this API can hit an URL that gives them data they are after.
Here is similar functionality implemented in Rust,
It is not too far from python in number of lines of code, Python 204 and Rust 294. Even though functionally they are equivalent(roughly as the key names are different in JSON) from API consumer’s point of view (CSV/JSON), I’m doing it in 2 threads in Rust instead of 3 in Python just because I realized I didn’t need it!
I’m happy to see Rust which is positioned as systems language to be this flexible. I owe the speed in prototyping to Rust compiler, in fact most common cases where people will get an error and then go lookup stackoverflow to see workarounds is cut dramatically by Rust compiler. It tells you what is wrong and even suggests workarounds, and as you start following compiler’s advice you will see how helpful it is.
Please don’t use Rust as I’ve written (RAIW), I’m still new to the language and I’m learning my tricks around it, however this code works today. This is not a complete toy problem either. I have a shared mutable data structure, shared between 2 threads. I’m using external crates to get some of my job done, nickel.rs to run a HTTP server for example.
Also writing in this language I don’t have to worry about where it will be deployed and whether the target machine has right version of run time to support my code. I can run Rust code built as a single binary on a 32-bit or 64-bit architecture as cargo with rustup makes it an extremely pleasant packaging experience.
So far I’m really pleased with what I could achieve with Rust in short span of time hacking it all together. This is why I felt this exercise is really rewarding, as I get better I’m sure I can reduce time it takes to write simple straight forward tools to help me do my job better in Rust and in the process I’ll gain more experience with the language. I cannot say the same for lots of other languages I’ve played with in recent past.
I want to port some code written in C++ once I feel happy with my Rust. But before that, I’m still exploring on how to setup tests so I can run them automatically on gitlab which will help me refactor this code into multiple modules and setup loggers as well.
I’ll keep hacking, all suggestions are welcome on Python or Rust code above to make it better!