Rust Graphics

02 Feb 2023

The first response I’ve heard from many developers when I suggest using Rust is that C++ is already there, already in use in Mesa, and has many of the same features as Rust. There’s no particularly significant reason why C++ couldn’t be used for implementing Vulkan API entrypoints, but so far none of the driver teams in the Mesa community have chosen to use it for the API portion of their driver. The VkOn12 driver used C++ initially because it made working with D3D12 easier but switched to C before it was merged into Mesa.

While modern C++ has many features which can help with these issues if applied correctly, they’re all opt-in and it’s still easy to write C-like code with all the same bugs. Using these features incorrectly or mixing C and C++ patterns for things like error handling can make bugs even subtler and harder to find and fix. Unlike C++, Rust’s safety features are built in to the language from day one and they intentionally make the unsafe C patterns hard while making the safe patterns easy. Rust also takes a very different approach, eschewing the object-oriented programming model in favor of its traits system. When used effectively, Rust traits provide a powerful programming model while avoiding unnecessary heap allocation and virtual function tables in most cases. In Android, a recent Google blog post showed that using Rust has reduced the number of memory safety vulnerabilities vs. the preexisting C and C++ code.