My First Few Days In Rust

Where I'm coming from

I spent the first year of my professional career doing Borland 6 C++ development but for the last 10 years I have very much been in the .net/c# stack. It's a testament to just how good the .net development ecosystem is. Everything I've needed to do has been catered for or at least I've managed to make it work till there is an 'official' way to do X.

BUT...... It's hard to shake of that feeling of maybe just maybe there is a better development experience or at least more fun project workflow to be had if I was further down the stack.

Rust 🦀

I've had my eye on Rust for little over a year with even a failed attempt to write a command line application to automate .net/npm package upgrades last year and increased exposure from all the fancy new Linux tools are getting written in it.

It looks to be one step above C++ in the stack with the sharp edges rounded off. No garbage collection with memory management done ahead of time by the compiler using a strong ownership system to work out when to clean up memory. Everything is immutable by default to help with thread safety.

Time to try to learn in the right way....

Rust has one of the best 'getting started' experiences of anything technical I've ever tried to learn. Rust Book a full length everything you need to learn to take you from a beginner to expert. The examples are well explained with enough suggestions of "why don't you go try it out" to make you go try stuff yourself and play around with the language.

I got about 30% through the book in this initial stint before I hit the need to try and make something...

Learning Project

As a little test I thought I would try to implement a simple version of Conway's Game of Life.

The full rules and history of the game can be found on Wikipedia - Conway's Game of Life but its simple a grid based system for cells to turn on or off.

There are basically 3 rules:

So what does this look like a console application in rust.. well.....

console.png

Ha ha.. ok its basic but it is working...

I managed to get this up and running in about 2 hours which is maybe the fastest I've ever got a version of this game up in a new language.

I found Rust as a language very complicated at first glance looks but this is mostly due to the control you have around ownership. If we take my setup function to randomly setup the grid array as an example.

fn setup(mut data: Array2<bool>, &width: &usize, &height: &usize) -> Array2<bool> {
    let mut rng = rand::thread_rng();

    for x in 0..width {
        for y in 0..height {
            data[[x, y]] = rng.gen::<bool>();
        }
    }
    data
}

Oddly I think it looks closer to Typescript than anything else but lets try to break it down a bits.

You can see the data parameter is marked as mut to say yes we can make changes to it but the width/height are only pointers as we only need to use these values not update them.

Secondly the what feels like semi-random data on the last line of the function at first seems odd with no return statement but this is due to how statements/expressions work in Rust. In real basic terms this is the function passing ownership back of this data to the parent. If we didn't do this the parent wouldn't be able to use the output of this function.

I really like the way you can define loops in shorthand with for x in 0..100 . This just feels the cleanest way to write these loops.

The last bit of my experience to talk about is the compiler. Yes I do agree with some it seems a little slower than other languages but from my understanding its doing a huge amount for you compared to a normal C++ compiler. For me though the feedback it gives you alone is worth it. The compiler error messages are the best I have ever seen. Doing my little project I had loads of compile errors but no runtime errors which for me totally offsets the extra compile time.

So there defiantly seems to be something here that appeals to me and I think most people at some point should try Rust.

Next for me is another chunk of the Rust book and probably another larger project in Rust....

Jonathan Dent