Where there be steel there be rust

Mongo DB and Rust

Ilan Toren
2 min readOct 15, 2021

--

Rust is a language of rapidly growing popularity, and it is at the state where its ecosystem is beginning to have things like web frameworks. My use of the language is relatively recent and I’m pretty sure that it will gradually replace python and PHP in that domain.

My interest here is just to show that the same paradigm that I showed in my first post I showed how to use Mongo Compass and Atlas to create a query function using the aggregation framework. The python code was posted there and the similarity of the MongoDB code in this project is evident.

The code is pretty self-evident (after the fact). The thing about Rust is that it uses Future and async/await a great deal. In a program such as this, that is wasted, but in a real application the main thread would pass a future to a function and let some process handle the results. The MongoDB driver uses the asynchronous driver so every interaction with the database is returned as a Future. The other Rust-ness is that some things are returned as Option<> and need to be unpacked with a match x {} block or an await. It is instructive to use an IDE like JetBrains (Inteillij or WebStorm both work) since the IDE shows what is returned, which greatly aids in programming in Rust (especially for newbies I think). I’m going to take this to the next step and implement deserialization with serde and put it into a webserver (Rocket). I’m still debating which of the web frameworks fits my personality.

--

--