TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
C++ / Programming Languages / Rust

The Rust-C++ Bridge: A New Path Forward

As Rust approaches its 10th anniversary, the Rust Foundation aims to make the systems programming language play nice with C++, addressing a key barrier to enterprise adoption.
Nov 15th, 2024 2:00pm by
Featued image for: The Rust-C++ Bridge: A New Path Forward
Featured image via Unsplash+.

The Rust Foundation this week released a problem statement addressing the challenges in C++ and Rust interoperability.

The problem statement is a key deliverable of the Rust Foundation’s Interop Initiative, which was launched last February with a $1M contribution from Google.

The initiative recognizes that both C++ and Rust have essential roles in the future of systems programmingJon Bauman, who joined the foundation as Rust-C++ Interoperability Engineer in June, is the honcho of the initiative.

Using the Languages Together

“While both languages excel in similar domains, using them together effectively is crucial for organizations seeking to balance safety and performance with maintainability,” the foundation wrote in a statement. “The core challenge lies in developing a mature, standardized approach to C++ and Rust interoperability, despite Rust’s historical focus on C compatibility.”

Indeed, the problem statement marks a significant step toward making cross-language development more accessible and approachable for the wider programming community, the foundation said. Rather than prescribing specific solutions, the problem statement serves as a foundation for community input and participation in shaping both the strategic direction and tactical implementation of improved C++/Rust interoperability, the foundation said.

The problem statement outlines three key strategic approaches:

  • Improve existing tools and address tactical issues within the Rust Project to reduce interoperability friction and risk in the short term.
  • Build consensus around long-term goals requiring changes to Rust itself and develop the tactical approaches to begin pursuing them.
  • Engage with the C++ community and committee to improve the quality of interoperation for both languages to help realize the mutual goals of safety and performance.

Use Cases

The problem statement also notes that both C++ and Rust will remain important for systems programming for the foreseeable future and currently interoperability relies mostly on C ABI (Application Binary Interface), which limits functionality. The most common use case is C++ systems adding or replacing functionality with Rust.

According to the statement, the desire for interoperability depends on the particular system, but the common use cases are:

  1. C++ systems adding or replacing functionality with Rust
  2. Rust systems using existing C++ code
  3. Polyglot systems supporting Rust (such as for plugin architectures)

“With such a high degree of overlap in applicability, using both languages together is essential to pursuing safety and performance which is maintainable and scalable,” the statement reads. “Empowering technologists to choose the language best suited to their situation and minimizing the costs and risks of interoperation is the core goal of the initiative.

Goals and Challenges

The overall goals of the initiative are to make interoperability more accessible and approachable, minimize additional code requirements, maximize safety and performance, maintain compile-time error checking and avoid undermining Rust’s core values

The main challenges to C++/Rust interoperability include that communication is limited to C-type interfaces, reducing Rust’s safety benefits, and FFI (Foreign Function Interface) boundaries are typically unsafe, the statement said.

“Communication is limited to interfaces expressible in the C type system, precluding much of Rust’s ergonomic and safety benefits,” the statement said, adding that “the FFI boundary itself is typically unsafe, meaning guarantees such as freedom from undefined behavior and data races are lost.”

Rust is nearing the 10th anniversary of its 1.0 stable release and is transitioning from the “early adopters” stage to the “early majority” stage of the technology adoption lifecycle.

“This transition aligns with a growing emphasis from industry and government on adopting memory-safe languages, with Rust standing out as a prime choice,” the statement reads. “Rust uniquely combines robust support for concurrency with guaranteed freedom from undefined behavior, making it a compelling alternative to C++ in environments where garbage collection is not feasible. Its proven reliability in these areas cements Rust as a critical tool for safer, high-performance systems.”

Moreover, according to the problem statement, the three most popular tools, which were initially published many years apart on crates.io, are:

  • bindgen (call C from Rust): 2015
  • cbindgen (call Rust from C): 2017
  • cxx (C++ ↔︎ Rust): 2020

While interoperability has improved, significant challenges remain. For instance, building system integration is particularly challenging, the statement said.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.