The Rust Foundation has published a document on GitHub that describes the biggest problems in the interaction between Rust and C++. The Foundation proposes short-term and long-term measures for improved interoperability between programming languages.
Advertisement
The document is part of the Interop Initiative: Google awarded a US$1 million grant to the Rust Foundation in February 2024 to improve the interaction between C++ and Rust.
Where Rust meets C++
Interoperability is particularly in demand in system-level programming. Rust has now established itself there, but very few projects start on greenfield sites. Instead, they are often encountered with existing C++ applications.
The Rust Foundation document lists three use cases: C++ systems that implement new functionality with Rust, Rust systems that depend on existing C++ code, and systems with multiple programming languages that implement Rust, among other things. We do. Since C++ has a much longer history than Rust, the first use case is by far the most common.
Problems occur primarily on systems with limited resources, including embedded environments.
Integrating Rust into the Linux kernel is not a problem because there is no C++ code in the kernel. Linus Torvalds called C++ (and other object-oriented languages) a terrible language. However, Rust has also entered the kernel in the community as early as 2024. Discussion rekindles about C++,
Roam through C
To date, there is no toolchain that handles mixed code bases of C++ and Rust code in a single file. Therefore, there is a need for an FFI-based (Foreign Function Interface) approach to interaction.
Connection is currently done through C’s application binary interface (ABI), as C-ABIs are more stable and less platform-dependent than C++-ABIs. Rounding is especially important where C++-specific features have to be transferred to the C-ABI first.
Conversations with C were on the agenda from the very beginning of Rust development. The Rust Foundation describes C as a common language for FFI integration. The biggest challenges are that Rust’s security features are largely lost when interacting with C and communication must occur at the common linguistic level, which limits Rust features.
The most important tools for interaction of Rust with programming languages C and C++ are bindingTo invoke Si from Jung, and cbindgenIt’s much more widespread to call Rust than C. as cxx For bidirectional communication between Rust and C++.
way out of misery
In its “C++/Rust Interoperability Problem Statement”, the Rust Foundation names three essential strategies to improve the interoperability of programming languages.
First, the Foundation wants to help improve the tools and eliminate fundamental problems in the Rust project that make it difficult to work together. Then, consensus must be reached on a strategy for identifying and making changes to Rust necessary for interoperability. Ultimately, the Foundation’s goal is to work with the C++ community and the C++ Standards Committee to improve collaboration and achieve common security and performance goals for both languages.
Further details can be found here C++/Rust problem description on GitHub and Rust Foundation announcement Removal
(rme)