Getting Rusty

Over the years I have written in many programming languages and even written a few. They all have pros and cons, things where they are ahead of the game and sometimes places where I feel they are going backwards. Really folks writing languages, why leave out  the c preprocessor or an equivalent? It's enormously useful and powerful.

The last few years have been mainly golang which I have been very impressed with. Concise, secure and many innovative features. Not giving it full marks out of ten as I took a big dislike to it's "linter" that imposes styles that are pointlessly strict and actually unsuitable for some projects.

Golang uses a "garbage collection" system for memory which is both a blessing and a curse. It makes things safe and secure for sure, but it means you never know quite what's going on.

Plunging back into C/C++ for these tutorials though has reminded me of just how dangerous c can be, the potential to access beyond the end of arrays and leak memory is something we should never want in executable code, let alone the Linux kernel which was written until recently exclusively in C.  If Kernighan and Ritchie had given c a proper string and array type with bounds checking the world would be a significantly different place where cyber security was far less of an issue.

Recently google has given us a another new language Rust which is finding its way into the Linux kernel. Rust looks interesting for its novel approach to memory which depends neither on "free" or on garbage collection. It is even reputed to be faster in some cases which is not difficult to believe as C for all its vaunted speed handles strings very poorly. I always wince at the idea that strlen() has to count all the way from the start of the string until it finds the 0 terminator.

So lets play a bit with Rust.

Install Rust

First observations

Rust quickly shows its metal to a C programmer with the unsafe construct, it would appear anything in an unsafe block of code can play with pointers without namby-pamby bounds checking. I like the idea that I have the freedom to do smart things with pointers if the need really arises. But would the need arise? 

preprocessing again!  Rust has constructs that try and reinvent the wheel, but end up with a triangle instead.

So where to start coding? In an earlier article I wrote and coded a bit on HyperLogLog an interesting algorithm I implemented in C++ using a slightly naughty trick and access to the low level processor built in __builtin_clz().  Duplicating this in Rust seems an appropriate challenge.

 Here's the github link

What did I learn?

Documentation still isn't great

There are so many basics to a language and I didn't find the rust book a great experience. Even the module structure seems to have over complex documentation that skips the simple way you would probably structure thing, when I had to google something daft, I found plenty of people with the same daft questions.

Being different for the sake of being different

Is it so hard to provide the "for" loop syntax we all know? In the last language I created I simply made just about any loop syntax I had some across work. I confess to almost liking the functions that operate on types.
(2 as u32).pow(keybitcount);
But only almost, rust has the concept of template functions, so it again seems like mixing things up for the sake of difference as do the returnless returns or the awkward Result types for returning errors. golang is just so much neater and terser at this.

At least you can turn the style fascism off

The linting is  a pain it wants snake case where FooBar is frowned on in favour of foo_bar. But there are plenty of circumstance where clarity is achieved by different naming conventions, where you make want to be using the same names that exist in a different eco system with different rules, say the table names from a database.

It's safer

I did make the odd mistake and translation and for a bound check error on running. The C++ code would not have caught that. Equally assuming no bugs in the language itself I can be pretty certain I am not leaking memory.

You only learn so much from very small projects.

This sadly is one of the ultimate truths. I was expecting to have to use more of the interesting memory constructs such as "Borrow" but I didn't. I'm also a big proponent of decent error handling and the error handling in Rust seems to bear similarities with a system I created on the last big project I was involved in. But this program has not caused me to explore it.

Comments

Popular posts from this blog

A reliable tuya authentication in node-red

Arduino boards - what I wish I'd known when starting

node-red a full home automation example