Baby steps with Rust

Arnaud Sahuguet
Cornell Tech
Published in
3 min readMar 14, 2017

Why Rust?

Who doesn't like to learn new stuff? At Cornell Tech, two recent research projects involve the Rust language: one to implement a new password storage scheme (research paper here), and one to implement a new consensus protocol for the blockchain (research paper here).

The mission of my team (the Foundry@Cornell Tech) is to help with this kind of development. I had to learn more about the language.

If you are a Rust developer — preferably in the New York City area — , if you like open source, blockchain and crypto, please shoot me an email.

What is Rust?

Rust is a general purpose programming language sponsored by Mozilla Research. It is designed to be a “safe, concurrent, practical language”, supporting functional and imperative-procedural paradigms. Rust is open source software.

Learning more about Rust

The Rust website has lots of examples and tutorials. But being old-fashioned, I purchased the "Programming Rust" book.

Some code examples

I have to admit that looking at code samples did not help me much at first. There were some concepts and syntax constructs I had to check in the book. One project I have been looking at is xsv, "a command line program for indexing, slicing, analyzing, splitting and joining CSV files." I feel that this is one of a sweet spot for a systems programming language like Rust.

My first program in Rust

In a previous life, my first encounter with the Java language was to implement DES. For Rust, I picked a more modest goal: a tool to enumerate hands for the game of poker.

Poker programming

When doing interviews (at Google for wannabe product managers or at Cornell Tech for prospective students), I like to ask poker questions like "how many ways to generate a hand with two pairs?". The question is interesting from a combinatorial point of view. It is also interesting from a programming one because you need to (1) define an encoding for cards, (2) iterate over all possible hands and (3) identify when a hand fits a given property.

https://en.wikipedia.org/wiki/Dogs_Playing_Poker

The Python version

Here is the code I am using in Python.

The Rust version

And here is my Rust equivalent.

I now understand the mutable vs immutable and the use of mut. I have to admit that & and * are still not totally clear me and I rely on the compiler to help me guess. I am glad to be back in a typed universe. I also appreciate the functional elements like filter which are a good workaround for comprehensions.

I was expecting Rust to be way faster than Python: Rust is compiled while Python is interpreted, and Rust claims to be a systems language that is very fast. My first experiment was not very good.

Python returned the answer after less than 7s. Rust took 19s when I used the cargo run command . That’s disappointing. A few StackOverflow queries later, I discovered the magic —release flag which brought the time to under 1s. Much better.

What's next?

My poker.rs program is far from an exhaustive exploration of the language. One possible next step is to use threads to explore the space of poker hands and leverage Rust concurrency primitives.

I also need to understand better how Rust fits in the current ecosystem of "safe, concurrent and practical" systems programming languages, with competition such as go, OCaml, etc.

Thank you for reading through the post. We are still looking for talented Rust developers :-).

If you are a Rust developer — preferably in the New York City area — , if you like open source, blockchain and crypto, please shoot me an email.

Comments on how to improve my Rust style and the performance of my code are welcome.

--

--

Arnaud Sahuguet
Cornell Tech

@sahuguet, SVP Product at Gro Intelligence, previous life includes Cornell Tech, NYU GovLab, Google, Bell Labs, UPenn, X91.