Learning Rust one test at a time

My second job was a lot of fun, I was fresh out of university and I got to play with graphics and draw cool effects on the screen using DirectX and OpenGL.

Since then, I have worked in many other companies and learnt how to write better code but I have always had a place in my heart for using matrix transformations to render 3D worlds using colors, reflections, lighting and shadows. That’s why when I’ve saw a book titled: “The Ray Tracer Challenge” I knew I had to get it. The book teaches how to build a 3D renderer form scratch – using test first (TDD/BDD) approach.

Since it was a long time since I’ve learned a new programming language I’ve decided to use Rust to and so I’ll re-learn something old (3D graphics, lighting) and something new (Rust).

I have been using JetBrains Rust plugin (running inside CLion) and it has been easy to use, all of the refactoring options, hints and run configurations – just work and make writing Rust code a breeze.

I have reached chapter 4 so far and have been fun learning a new language and getting thigs to appear on the screens (only dots so far – but I’m getting there), you can check my efforts on GitHub, I have been trying to find time each week to progress a but, unfortunately/luckily I have just committed myself to yet another project (I promise more details in a couple of months) so my Rust endeavor will suffer. I am, looking for feedback – it’s hard to tell how things supposed to be done when starting a new language, how to name your files – do you write all of the struct code in the same place, and so on.

My experience with Rust

so far it seems that Rust is a highly opinionated language, I get warnings when I write the wrong casing for method names or name my files not according to the standards – and I like it, although I’m not sure what some developers would do if they cannot argue about the right place to put their curly brackets.

Other than that, I found Rust to be simple to use once you get how methods are declared, operators “overloaded” and forget all you ever knew about inheritance and method overloading and learn how to use (and not abuse) macros, it’s a bit painful at first but then things do become a little simpler.

Rust is a safe programming language – just as advertised, it’s impossible to shoot yourself in the foot, in fact whenever you try Rust will explain why you’re an idiot and suggest what you should have done, that is one thing I wish more compilers will do, whenever I got a compilation error the compiler told me what I did wrong, and how I might correct it. It made learning Rust so simple, although I still get the nagging feeling that I’ve used ‘&’ incorrectly, but I get that in C++ from time to time, so I guess all is well. Writing code where everything is immutable as the default, cannot be null is so liberating and helps avoid writing those checks we <insert language here) developers are so familiar with, and the rust compiler really makes an effort to help you, finding unused code or suggest a better way to initialize your variables.

But there are a few downsides here, Rust is a relatively new language and some of the core libraries are still begin developed, some of the tools and frameworks are not there yet, for example the “unit testing framework” is a set of minimal macros and that’s it, and sometimes the type system feels too rigid – for example you decide on the size of an array when you write the code (otherwise use Vector) and in fact an array of 10 items is a type of its own which is a different type from an array of 5 items, it opens a few possibilities (as I discovered when creating matrices) but close a few others.

All in all, I enjoy writing code in Rust, its quick and intuitive but different from my Day Job’s Java/C++/C# work.

What I think about the book

Before I start talking about how much I like this book, a word of warning (or disclaimer if you will), if you intend to learn BDD or TDD from this book – walk away, this book does not intend to teach you how to write good tests or even how to write tests at all, there are better books for that depending on your language of choice.

In fact I find some of the tests to be “less than ideal” with strange inputs, multiple asserts, not covering all of the possible scenarios, writing more than one test before rushing to the implementation and other small sins in the Test first world – but that is not the point of this book.

This book does something different, Instead of explaining about Points, vectors, matrices and lighting it helps you create working software, one test at a time, by writing the test then the implementation I find I understand better the concepts and code, and by the way, the book does not have code snippets – just pseudo code and Gherkin like features/tests for you to write and implement.

I really do like the book, it helps you write tedious code in an interesting way (I mean how interesting can multiplying a vector can be), and in the end of almost every chapter you get to use the code you’ve implemented to create a working piece of software and see the code you wrote run.

And I do like this book, instead of just reading how things are done – you get to participate, in a well written, well thought of lesson, and have something to show for at the end.

So, if you’re interested in 3D rendering or just want to learn something new – I highly recommend this book. Read it, write the tests and then the code, other than programming, no prior knowledge needed.

Happy Coding…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.