Rust and C++ hand in hand: strategies for better interaction
In a statement, the Rust Foundation summarizes the problems in the interoperability between Rust and C++ and provides initial solutions.
(Image: erstellt mit KI (Dall-E) von iX-Redaktion)
The Rust Foundation has published a document on GitHub that describes the biggest problems with the interaction between Rust and C++. The foundation proposes short-term and long-term measures for better interoperability between the programming languages.
The document is part of the Interop Initiative: Google awarded the Rust Foundation a grant of one million US dollars in February 2024 to improve the interaction between C++ and Rust.
Videos by heise
Where Rust meets C++
Interoperability is particularly in demand in system-related programming. Rust has now established itself there, but very few projects start from scratch. Instead, they often encounter existing C++ applications.
The Rust Foundation document names three use cases: C++ systems that implement new functionality with Rust, Rust systems that rely on existing C++ code, and systems with multiple programming languages that implement Rust among others. As C++ has a much longer history than Rust, the first use case is by far the most common.
The problems mainly occur on systems with limited resources, including in the embedded environment.
The integration of Rust into the Linux kernel is not an issue, as the kernel does not contain any C++ code. Linus Torvalds has described C++ (and other object-oriented languages) as a terrible language. However, the inclusion of Rust in the kernel at the beginning of 2024 has also reignited the discussion about C++ in the community.
Detour via C
To date, there is no toolchain that processes a mixed code base of C++ and Rust code in the same file. An FFI-based (Foreign Function Interface) approach is therefore required for the interaction.
The connection is currently made via the Application Binary Interface (ABI) of C, as the C ABIs are more stable and less platform-dependent than the C++ ABIs. The detour is particularly important where C++-specific features must first be transferred to the C ABI.
Interaction with C has been on the agenda from the very beginning of Rust development. The Rust Foundation describes C as the lingua franca for FFI integration. The biggest challenges are that the security functions of Rust are largely lost when interacting with C and that communication must take place on a common linguistic level, which restricts Rust features.
Among the most important tools for the interaction of Rust with the programming languages C and C++, bindgen, for calling C from Rust, and cbindgen, for calling Rust from C, are significantly more comprehensive than cxx for bidirectional communication between Rust and C++.
A way out of the misery
In its "C++/Rust Interoperability Problem Statement", the Rust Foundation names three key strategies for improving the interaction between the programming languages.
Firstly, the foundation wants to help improve the tools and eliminate fundamental problems in the Rust project that make interoperability difficult. Next, it wants to find consensus for a strategy to identify and make changes to Rust that are necessary for interoperability. Finally, the Foundation will work with the C++ community and the C++ standards committee to improve collaboration for both languages and achieve common goals for security and performance.
Further details can be found in the C++/Rust Problem Statement on GitHub and the Rust Foundation's announcement.
(rme)