Marwan: “What do all these features Rust has to offer mean to me as a programmer and how exactly do they make my life easier? Can you also tell me a bit about how all of this relates to drone flight?”
Hayley: “Generally speaking there’s only a few programming languages that are truly suited for controlling hardware in (soft-)real time.
Languages like Java or Python aren’t really made for that kind of thing because, for example, once every while they will run a garbage collector to free up old memory. In order to do that they have to pause the entire program for an unpredictable amount of time. and, well, that’s not ideal if you’re flying a drone…
Now sure some people will say there’s ways around that but, use the right tools for the job I say. And in that case that’s usually something like C, C++ or Rust.
Then why Rust and not C++? well aside from Rust being more pleasant to work with in my opinion, Rust also gives you safety guarantees that C++ can’t give you; That garbage collector I mentioned before? that has to run because those languages manage memory allocation and lifetimes for you. Automatically freeing variables when they’re no longer used and keeping them around as long as you’re still using them.
In a language like C or C++ you have to manage your own memory, and while modern coding standards make that a bit easier, human error can still occur. And what does ‘human error’ look like when you mess up your memory management? An unpredictably malfunctioning drone.
You can still write a malfunctioning drone in Rust, sure. But if you’re using Rust (and no “unsafe” blocks) it won’t be malfunctioning due to memory-safety issues, because Rust tracks those at compile time for you. Giving you the same kind of safety you would get with a managed language like Python, without the associated overhead.”
Jakub: “Rust is by default safe, meaning that it is not possible to access invalid memory, create data races in parallel code, or get any other kinds of undefined behavior. I don’t have to worry about whole classes of hard-to-discover bugs, which are possible to introduce in languages like C and C++ by making a typo. I can trust the Rust compiler to do this work for me, which reduces my cognitive load and allows me to spend more brainpower on the real problems I’m trying to solve with my code.
The Rust compiler has some of the best error messages I’ve seen. They explain what the issue is, suggest changes to code to fix the problem, and include links to documentation with examples and longer explanation. When something doesn’t work, I can quickly pinpoint and fix the problem.
Rust is a great fit for drones. It compiles to native code and doesn’t use garbage collection, which gives it a predictable execution and high performance comparable to C and C++. Thanks to the safety guarantees made by the compiler and built-in testing infrastructure, it is easier to eliminate bugs during development and prevent failures during flight caused by programming errors.”