Why Rust? (The More Technical Edition)
I’ve written a high-level view of why I got interested in Rust on the Wiresmith Technology site.
After many conversations at GDevCon last week, I felt it would be helpful to put down the technical reasons that Rust appealed to me and how I (and therefore you) could decide what a suitable language is for your test & measurement applications.
What’s In A Language
As I spoke to various people, I got more explicit on the areas I’m evaluating for any given programming language.
- Mental/Programming “Model” – This is a hard one to explain, but the simple version is some languages just fit the way you think and some don’t. The model includes technical aspects such as your comfort with manual memory management or imperative vs. functional styles.
- Runtime/Deployment Capabilities – Can I run this where I want? Different languages are better suited to environments like the web, docker, microcontrollers, and desktops. There are also performance considerations – is this language’s run time environment capable of high-performance or real-time use cases if you need them?
- Tooling – Are the tools around the language high quality and meet your needs? These might be tools like package managers, documentation generators, build automation and orchestration.
These are the three most significant areas I use when examining languages.
Why Look Elsewhere (or where LabVIEW is weak)?
I first started using LabVIEW around 15 years ago. So it isn’t a bad tool. Otherwise, I would have jumped ship long ago!
However, my needs have changed, and I’ve seen new capabilities become available in other languages I envied.
My trajectory has moved me into more projects that I call product development or prototyping. In this area, I found some pain points with LabVIEW:
- I was moving away from its core in test & measurement, meaning more gaps and less investment in the areas I’m using.
- The emphasis on software engineering tools has grown as development is done over a more extended period and put into the hands of more users who aren’t familiar with the technology.
- Only deploying to the desktop or NI’s hardware became restrictive. I’ve had customers where the right solution is an embedded PC or high-performance server, and LabVIEW is difficult to deploy to these.
- Increasingly, there is a need to integrate into web technologies where LabVIEW is poor—for example, running a web server to collect data.
These are my reasons, and they will not apply to your exact circumstances. This is what I want to emphasise here.
My message is not that everyone should be switching to Rust. In my case, it is a great option. If you are working on an automated test system, for example, you probably don’t have the same pain points, and LabVIEW may work very well for you.
Where Did I Look?
So, lets talk through some languages I’ve investigated and highlight some of their unique capabilities and limitations that might be interesting.
Javascript/Typescript
My first trip into text-based languages was Javascript. Having built a web backend in LabVIEW, it turned out the web server had serious scalability issues at the time. LabVIEW’s web services are challenging to use in anything beyond simple use cases.
Javascript is an interpreted language and generally runs in a very event-driven manner. Being interpreted means it isn’t the fastest (although it’s [JIT compiler](https://medium.com/nerd-for-tech/inside-the-v8-engine-b81aff3eecdb) means it is faster than you might think). Still, it is well suited to web environments where you find it as _the_ language in the browser, and Node.js means you can build pretty complex web servers with it.
Put simply, though, it is not designed to interact with the hardware system, so it is unlikely you will ever see this on a desktop or embedded system (though there are projects to make it a little easier).
The tooling is reasonably good, but it is a little disjointed and moves quickly. Picking up an 8-year-old project recently required a decent chunk of time to modernise everything to the changes that have occurred!
C#
In the LabVIEW NXG days, NI told us that it would be built in C#, and at least to start with, you would need to use C# to extend the environment.
So I decided I wanted to get my head around it, so I wrote the first version of G CLI in it.
C# is a compiled language that runs on a virtual machine-like runtime called the .net CLR (common language run time). It is designed to be extremely powerful for large applications, having been created by Microsoft as a response to Java.
One of the critical characteristics of C# is that it is a garbage-collected language.
Garbage collection means that instead of the user manually controlling memory allocation, a periodic process in the runtime reviews the current objects in memory and frees whichever aren’t used any more.
The big pro is that you don’t have to worry about forgetting to free memory or other memory-related issues as a user. A significant con is that when the garbage collector runs, it can affect the performance of your application. This performance isn’t critical in many applications, but high-performance or real-time applications are negatively affected.
The large runtime also impacts its uses in embedded applications, but it will run in most other places now there is Linux support and container support – it is most commonly found in desktop applications and web applications.
I also didn’t like the exception-style error handling, which has bothered me in many languages after coming from LabVIEW.
For me, the performance may limit its usage, so once NXG disappeared, so did my use of C#. However, this could be hugely powerful in many applications where you use LabVIEW if it ticks some other boxes.
C/C++
Because of where I ended up – it is worth a small shout out to C and C++.
These are systems languages. Systems languages are designed to run “close to the hardware” and are what many operating systems are built with and interoperate very well with.
They are widely supported across different hardware, and pretty much every other language in the world can call libraries created with these since their minimal runtime is generally built into every environment you might run code in (excluding bare metal).
I picked them up because I needed maximum performance for part of a LabVIEW application, so we wrote the processing logic in C and called in from LabVIEW.
A lot of crucial elements overlap with Rust, so why did I skip over these:
- C is not a particularly expressive language, and I felt like it would get unproductive without built in templating/generics/polymorphism. C++ is better for this.
- The test library seemed broken again every time I returned to this project. Package management is limited (but improving), and I could see a lot of fighting with tooling.
- It is very easy to make memory mistakes that cause application crashes. I was happy building small functions in them, but building a complete application without memory issues seemed daunting.
Python
Another language I’ve been using more is Python.
Famously, Python has a huge user base and a wide array of libraries written for it, especially in data analysis.
The tooling is also good, with excellent testing capabilities and a reasonable package manager (with a few quirks!)
However, I found two significant downsides:
- Python is an interpreted language, which means it has a significant run time and relatively poor performance. Although not as bad as some believe, most heavy lifting is handed off to compiled libraries.
- I could not get on with the dynamic typing. I completed one web backend in Python and was cursing the typing constantly!
There is a lot of talk about Python in the LabVIEW ecosystem, but I suspect it is overhyped. For me, typing was a big issue that made me very reluctant to build anything significant in it.
Its power is in automation, connecting different libraries quickly and easily. So, in a test environment, it could be great for building individual test steps with a pretty defined scope.
I still use Python for simple automation tasks or interactive data analysis to experiment with different techniques. I know there will be a library somewhere in Python!
Rust
Rust has proven to be my way forward for several reasons. I have been working with it for 3-4 years at this point and found:
- The language and type system to be expressive and powerful (but some find it too complex and explicit).
- The memory safety guarantees resolved my no.1 concern with C++.
- The tooling ecosystem seems to be second to none. Package management, testing, benchmarking, and documentation are all well-covered and ingrained in the language.
- It is flexible with deployment. It is a systems language, so I can deploy it to a microcontroller or a large server. I’ve also been experimenting with OpenCL, which still needs small portions in C but allows the use of GPUs, DSPs and other hardware accelerators.
These allow me to offer a better service to my customers by leveraging a range of modern hardware without bumping up against limits. Meanwhile, I still feel as productive as I do in LabVIEW.
There are downsides, of course – Rust does require you to manage your memory, which can be a learning curve and makes applications more complex. But this is necessary for some of the deployment flexibility.
Summary – What Is Right For You?
So what should you do? Well, in many cases, sticking with LabVIEW is a good option if you aren’t feeling the same pain that I am. This article is my story, not a roadmap for everyone.
If you are feeling pain, though, hopefully, the summary above will help signpost you to a language that may fit what you need and start doing some research.
Does it fit my mental model? Do I like the community? Have others managed to solve similar problems in it?
This article should help you to start asking these questions.
Recent Comments