home
programming-rust-3.md

Programming Rust - 03

2022-04-05

I’m currently reading Programming Rust by Jim Blandy, Jason Orendorff and Leonora F.S. Tindall and this is my journal as I read the book. No promises, but the current plan is to at least finish chapter 1.

Time for the section on concurrency! The authors point out how concurrency can have issues in C and C++ and they say that in rust, if it compiles, then there are no data races. They don’t prove it but it seems to be a natural by product of the way rust works. Not sure if I buy that.

The example the authors use to look at concurrency in rust is pretty mathy. I imagine to them and to many others, it’s quite simple but damn! I’m not that smart so I’m going to try and word it out here. It definitely sounds interesting and it’s interesting to note that this example reminded me of SICP. There seems to be a focus on math in programming which as a person who’s self taught I never really understood. This highlights the lack of my theoretical knowledge.

Anyway! Tangent aside, the Mandelbrot set seems to be a set of complex numbers that stay near 0 when you square some number plus the complex number C. As you iterate through n complex numbers, you can check to see if they will trigger an infinity in our Mandelbrot function.

function mandelbrot(c) {
    let x = 0;
    loop {
        x = x * x + C
    } while (true);
}

This is the basic gist of the Mandelbrot function. The goal is that we want to know if x goes to infinity, or if it stays around 0. If it will get stuck around 0, then it is part of the Mandelbrot set, otherwise, it isn’t.

The condition we can place according to the book is that once x is above 2, then it will definitely be out of range. Next, if x is under 2 then we may or may not get stuck in the loop, it could be on the 10th iteration, x will be greater than 2 but it could also be that x will remain under 2 for the next 100 iterations but get’s larger on 101st iteration. This means that we need to add some sort of limit as we don’t want check forever.

The idea is simple and plotting this out on a graph creates a cool looking fractal design. I’m curious where this stuff comes up.

Writing out the code for this function is pretty straightforward. The parse_pair function that comes next in the book was definitely worth writing. It’s written as a generic function and it parses numbers from strings. It uses matches well and it’s something I never really did before. I usually kept matches very simple and/or tried to dodge them but this function is a good example of how matches work and I can see how I would use it in other places.

Writing out the test functions was also helpful and I wish I knew that before.

I need to look at more rust code to see how tests are stored, I like the idea of keeping the test right next to the function but that could get messy. It might make more sense if each function was in it’s own file.

The next major function that the book works through is a function that takes a specified image size and then maps each point to a complex number space. This will then decide which part of the Mandelbrot set we will be iterating through. So we define C by giving an image space and the start and end points of the complex number space. It’s pretty heady to think about as it all feels very sci fi :). I still don’t get why but this would definite fit in some fiction which is lovely.

The render function is pretty straightforward and as I was writing, I noticed that you actually set the color differently based on which iteration the complex number broke through to infinity. So we will be setting the numbers in the set as black and everything else will be varying shades of black, all the way to white for the ones that are immediately greater than 2. The limit also makes it so that if we 255 iterations, then it will automatically be black.

Writing the image seems pretty straightforward but that’s probably because this is going to be in grayscale. It would be cool to write out more colorful images and this is a good intro to it.

I finished up the program and it is quite neat. There’s a feeling of accomplishment that I can actually generate the images and playing around with it is quite fun. Something to be said about writing a program that actually creates something. On my PC this takes a few seconds to generate and it’s pretty clear why concurrency would be great for this program. The pixels are independent of each other and we can actually do all of the calculations at the same time to speed things up. I’ll leave that section for another day.

Highly recommend the book - based off the first 35 pages…

One of the images I generated: