mystor
rust-cpp
Rust

Embed C++ directly inside your rust code!

Last updated Jun 28, 2026
847
Stars
46
Forks
21
Issues
+1
Stars/day
Attention Score
68
Language breakdown
Rust 98.4%
C++ 1.2%
Shell 0.4%
โ–ธ Files click to expand
README

rust-cpp - Embed C++ code directly in Rust

Documentation

Overview

rust-cpp is a build tool & macro which enables you to write C++ code inline in your rust code.

let name = std::ffi::CString::new("World").unwrap();
let nameptr = name.asptr();
let r = unsafe {
    cpp!([nameptr as "const char *"] -> u32 as "int32t" {
        std::cout << "Hello, " << name_ptr << std::endl;
        return 42;
    })
};
assert_eq!(r, 42)

The crate also help to expose some C++ class to Rust by automatically implementing trait such as Drop, Clone (if the C++ type can be copied), and others

cpp_class!{
    #[derive(PartialEq)]
    unsafe struct MyClass as "std::unique_ptr<MyClass>"
}

Usage

For usage information and in-depth documentation, see the cpp crate module level documentation.

Differences with the cxx crate

This crate allows to write C++ code "inline" within your Rust functions, while with the cxx crate, you have to write a bit of boiler plate to have calls to functions declared in a different .cpp file.

Having C++ code inline might be helpful when trying to call to a C++ library and that one may wish to make plenty of call to small snippets. It can otherwise be fastidious to write and maintain the boiler plate for many small functions in different places.

These crate can also be used in together. The cxx crate offer some useful types such as CxxString that can also be used with this crate.

The cxx bridge does more type checking which can avoid some classes of errors. While this crate can only check for equal size and alignment.

๐Ÿ”— More in this category

ยฉ 2026 GitRepoTrend ยท mystor/rust-cpp ยท Updated daily from GitHub