Crubit: C++/Rust Bidirectional Interop Tool Rust Workflow

zombot1 pts0 comments

Home - Crubit Documentation

Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Auto

Light

Rust

Coal

Navy

Ayu

Crubit Documentation

Crubit: C++/Rust Bidirectional Interop Tool

Crubit is a bidirectional bindings generator for C++ and Rust, with the goal of<br>integrating the C++ and Rust ecosystems.

Status

See the status page for an overview of the<br>current supported features.

Example

Consider the following Rust library:

#![allow(unused)]<br>fn main() {<br>pub struct Account {<br>pub id: u64,<br>pub balance: f64,

impl std::fmt::Display for Account { ... }

// Takes shared references, mapped to const references in C++.<br>pub fn calculate_interest(account: &Account, rate: f64) -> f64 { ... }

// Takes a string slice, mapped to rs_std::StrRef in C++.<br>pub fn is_valid_username(username: &str) -> bool { ... }<br>You can call these Rust functions from C++:

#include "path/to/account.h"<br>#include

void demo() {<br>account::Account my_account{.id = 123, .balance = 1000.0};<br>double interest = account::calculate_interest(my_account, 0.05);

// my_account is printable because Account implements Display in Rust!<br>std::cout

Consider the following C++ header:

#include<br>#include<br>#include

struct User {<br>int id;<br>double balance;<br>};

std::optional FindUser(int id);<br>std::unique_ptr CreateUser(std::string_view name, int id);

You can call these C++ functions from Rust:

use cc_std::std::unique_ptr;<br>use ffi_11::{c_double, c_int};<br>use user_api::{CreateUser, FindUser, User};

let id: c_int = 123;<br>let user: Option = FindUser(id);<br>if let Some(u) = user {<br>let balance: c_double = u.balance;<br>println!("User {} has balance {}", u.id, balance);

let new_user: unique_ptr = CreateUser("Alice".into(), 456);

Getting Started

We have detailed walkthroughs on how to use C++ from Rust, or Rust from C++,<br>using Crubit, as well as copy-pastable example code. The example code also<br>includes spanshots of what the generated bindings look like.

Walkthrough: C++ Bindings for Rust Libraries

Examples:<br>examples/rust/

Walkthrough: Rust Bindings for C++ Libraries

Examples:<br>examples/cpp/

Test Matrix

NightlyStable

rust account balance crubit include user

Related Articles