< Back to Blog

Why Rust for Robots?

By
|
March 30, 2021
Rust

Table of Contents

Robotics largely runs on C++. Or at least it does *for now*. Here at Tangram Vision, we believe that there is better language for robotics, and that language is Rust.

## C++ for Robotics

For robotic platforms where the intention is to deploy commercially and at scale, C++ has emerged as the standard over the last few decades, and there are a few key reasons why. For one, the level of ubiquity has become a self-reinforcing mechanism, with an ecosystem of libraries, tools and engineers that almost exclusively work in C++. In fact, [ROS](https://en.wikipedia.org/wiki/Robot_Operating_System) (the main client library of the most popular framework used by most roboticists) is primarily written in C++. Similarly, the popular [OpenCV](https://www.opencv.org/) computer vision library is also written in and accessed with C++.

However, ubiquity of tools and engineers alone doesn't explain the popularity of C++ for robotics. There is a more fundamental reason: most robotics platforms must meet acceptable performance thresholds under known resource constraints. C++ is well suited for these embedded applications because of how "close to the metal" the language can get.

This "close to the metal" advantage also makes C++ a potentially huge hazard. Testing, architecture, and memory management for C++ isn't consistent across well-used dependencies; yet, these dependencies might capture and manipulate essential low-level resources. This means it becomes very easy to inadvertently build in a critical bug without realizing it... that is, until you're well into production. That critical bug won't be discovered until the product has been deployed in the real world, where an edge case scenario that slipped past QA triggers a memory leak, or system crash, or [your favorite disaster scenario here].

![Does this Venn diagram look like you?](https://uploads-ssl.webflow.com/5fff85e7f613e35edb5806ed/60634c11ea56402e1ca6550c_Screen%20Shot%202021-03-29%20at%201.35.51%20PM.png)

Unless you resemble this Venn diagram, there's a good chance that you'll inadvertently program in some level of trouble as you build out your robotics codebase in C++.

This is where Rust starts to make sense.

## Enter Rust for Robotics

Rust is a relatively new language for robotics, but there's a rapidly growing set of projects and libraries that provide key frameworks for robotics development. Why the change?

For starters, the biggest benefit of building with Rust is memory safety and management. You have to try *very hard* to create a memory leak or race condition in Rust. Common gotchas like null pointers and data races are blocked altogether and won't compile. Likewise, the memory management approach in Rust is to use the stack to keep track of the program, and then use pointer references aimed at heaps where larger data structures are contained.

To access a data structure, ownership has to be established, thereby preventing multiple variables from accessing or modifying data structures simultaneously. Efficient? Yes. Safe? Also, yes. The best part: developers maintain that "close to the metal" access that they would normally go to C++ for. This makes Rust a highly-efficient, extremely safe language that also allows low-level access, something well-suited to the world of robotics where resource constraints and code safety are critical.

Rust is an obvious choice for robotics, but the transition to platforms written in Rust will take time. Rust itself is just over a decade old, whereas C++ has been around for nearly four decades. That's a lot of inertia to work against, but the Rust community is moving fast.

## Resources for Rust in Robotics

There's a small but growing community of companies and developers (Tangram Vision included!) that are taking some of robotics' most commonly used libraries and tools and making them Rust compatible, as well as developing new tools to ease the development path for creating a Rust-programmed robot.

Here are a few of our favorites that cover some of the critical areas of robotics development. We've chosen to highlight resources that are still actively maintained within the last year.

### Frameworks

- [OpenRR](https://github.com/openrr/openrr): An open-source Rust robotics platform

### ROS

- [rosrust](https://github.com/adnanademovic/rosrust): A pure Rust implementation of the ROS client library
- [ros2-rust](https://github.com/ros2-rust/ros2_rust): Bindings, a code generator and code examples for ROS2
- [rustros_tf](https://github.com/arjo129/rustros_tf): A Rust port of the ROS tf library for keeping track of three dimensional transforms
- [Optimization Engine](https://github.com/alphaville/optimization-engine): Embedded optimization for robots and autonomous systems

### Computer Vision

- [realsense-rust](https://docs.rs/crate/realsense-rust/0.5.1): High-level bindings for using Intel RealSense depth cameras (disclosure: Tangram Vision maintains this library!)
- [opencv-ros-camera](https://lib.rs/crates/opencv-ros-camera): An OpenCV-compatible geometric model of a camera
- [adskalman](https://lib.rs/crates/adskalman): Kalman filter smoothing
- [cam-geom](https://lib.rs/crates/cam-geom): Geometric models of cameras
- [bayes_estimate](https://lib.rs/crates/bayes_estimate): A Bayesian estimation library

### Collision Detection

- [openrr-planner](https://lib.rs/crates/openrr-planner): Path planning with collision avoidance

### Controls

- [stepper](https://lib.rs/crates/stepper): A universal stepper motor driver and controller interface for Rust

### Simulation

- [nphysics](https://github.com/dimforge/nphysics): A 2D and 3D physics engine that can be used for robot simulation

### Mathematics

- [Nalgebra](https://www.nalgebra.org/): It's linear algebra...for Rust
- [petgraph](https://github.com/petgraph/petgraph): Graph data structure library, compatible with Rust

If there are other Rust resources that you've used for a robotics product that worked well, let us know! We plan on keeping this list curated and updated over time.

## Should you build your Robot in Rust?

We think the answer will increasingly be *yes*. The fundamental benefits of the language already make it a great fit for the needs of roboticists and robots. The growing set of libraries and resources make it easier than ever to get started with foundations — and that includes the perception sensor tools and APIs that we are building here at [Tangram Vision](https://www.tangramvision.com). Finally, more and more engineers are adopting Rust as a language of choice, particularly for embedded scenarios like robots. So, yes, the future is robots. And it will also be robots, built with Rust.

Share On:

You May Also Like:

DevOps For Perception

Tangram Vision SDK is free for the first five instances and unlimited users.