Crubit, C++/Rust Bidirectional Interop Tool

Lobsters Hottest Tools

Summary

Crubit is Google's bidirectional bindings generator that enables seamless interoperability between C++ and Rust, allowing each language to call the other's functions. The documentation includes examples, walkthroughs, and a status page of supported features.

<p><a href="https://lobste.rs/s/9ofzvx/crubit_c_rust_bidirectional_interop_tool">Comments</a></p>
Original Article
View Cached Full Text

Cached at: 08/06/26, 10:14 PM

# Home - Crubit Documentation Source: [https://crubit.rs/](https://crubit.rs/) ## Keyboard shortcuts Press←or→to navigate between chapters PressSor/to search in the book Press?to show this help PressEscto hide this help ## Crubit Documentation [![rust workflow](https://github.com/google/crubit/actions/workflows/rust.yml/badge.svg)](https://github.com/google/crubit/actions/workflows/rust.yml)[![GitHub](https://img.shields.io/badge/github-google%2Fcrubit-blue?logo=github)](https://github.com/google/crubit) Crubit is a bidirectional bindings generator for C\+\+ and Rust, with the goal of integrating the C\+\+ and Rust ecosystems\. ## [Status](https://crubit.rs/#status) See the[status](http://crubit.rs/overview/status)page for an overview of the current supported features\. ## [Example](https://crubit.rs/#example) Consider the following Rust library: ``` #![allow(unused)] fn main() { pub struct Account { pub id: u64, pub balance: f64, } impl std::fmt::Display for Account { ... } // Takes shared references, mapped to const references in C++. pub fn calculate_interest(account: &Account, rate: f64) -> f64 { ... } // Takes a string slice, mapped to rs_std::StrRef in C++. pub fn is_valid_username(username: &str) -> bool { ... } } ``` You can call these Rust functions from C\+\+: ``` #include "path/to/account.h" #include <iostream> void demo() { account::Account my_account{.id = 123, .balance = 1000.0}; double interest = account::calculate_interest(my_account, 0.05); // my_account is printable because Account implements Display in Rust! std::cout << my_account << std::endl; if (account::is_valid_username("bob")) { std::cout << "Valid user" << std::endl; } } ``` Consider the following C\+\+ header: ``` #include <string_view> #include <optional> #include <memory> struct User { int id; double balance; }; std::optional<User> FindUser(int id); std::unique_ptr<User> CreateUser(std::string_view name, int id); ``` You can call these C\+\+ functions from Rust: ``` use cc_std::std::unique_ptr; use ffi_11::{c_double, c_int}; use user_api::{CreateUser, FindUser, User}; let id: c_int = 123; let user: Option<User> = FindUser(id); if let Some(u) = user { let balance: c_double = u.balance; println!("User {} has balance {}", u.id, balance); } let new_user: unique_ptr<User> = CreateUser("Alice".into(), 456); ``` ## [Getting Started](https://crubit.rs/#getting-started) We have detailed walkthroughs on how to use C\+\+ from Rust, or Rust from C\+\+, using Crubit, as well as copy\-pastable example code\. The example code also includes spanshots of what the generated bindings look like\. - Walkthrough:[C\+\+ Bindings for Rust Libraries](http://crubit.rs/rust)- Examples:[`examples/rust/`](https://github.com/google/crubit/tree/main/examples/rust) - Walkthrough:[Rust Bindings for C\+\+ Libraries](http://crubit.rs/cpp)- Examples:[`examples/cpp/`](https://github.com/google/crubit/tree/main/examples/cpp) ## [Test Matrix](https://crubit.rs/#test-matrix) [https://crubit.rs/overview/status.html](https://crubit.rs/overview/status.html) [https://crubit.rs/overview/status.html](https://crubit.rs/overview/status.html)

Similar Articles

crustc: Entirety of rustc, translated to C

Lobsters Hottest

A Rust-to-C compiler toolchain called cilly has successfully translated the entire rustc compiler into 46 million lines of C, resulting in a functional Rust compiler that can be built with GCC. The project aims to enable Rust on old/obscure hardware by generating portable C code.

Cpp2Rust: Automatic Translation of C++ to Safe Rust

Lobsters Hottest

Cpp2Rust is an open-source tool that automatically translates C++ code to safe Rust using clang's AST and a runtime library, enabling safe memory-safe conversion with support for both safe and unsafe output modes.

Diplomat: Multi-language FFI for Rust Libraries

Lobsters Hottest

Diplomat is a multi-language unidirectional FFI tool for wrapping Rust libraries, designed to expose Rust APIs to languages like C++, JS, Dart, and JVM without requiring FFI expertise, addressing a gap in the Rust tooling ecosystem.

Compiling Rust to readable C with Eurydice

Hacker News Top

LWN 报道了 Eurydice 项目——一个将 Rust 代码编译为结构清晰、可读的 C 代码的编译器工具,面向高可靠性软件验证场景,已用于后量子密码学例程的编译,由 Inria 与 Microsoft 团队维护的 Aeneas 项目的一部分。