Crubit: C++/Rust Bidirectional Interop Tool
Crubit is a bidirectional bindings generator for C++ and Rust, with the goal of integrating the C++ and Rust ecosystems.
Status
See the status page for an overview of the current supported features.
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\n";
}
}
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
- Examples:
examples/rust/
- Examples:
- Walkthrough: Rust Bindings for C++ Libraries
- Examples:
examples/cpp/
- Examples: