1{{#title Built-in bindings — Rust ♡ C++}} 2# Built-in bindings reference 3 4In addition to all the primitive types (i32 <=> int32_t), the following 5common types may be used in the fields of shared structs and the arguments and 6returns of extern functions. 7 8<br> 9 10<table> 11<tr><th>name in Rust</th><th>name in C++</th><th>restrictions</th></tr> 12<tr><td style="padding:3px 6px">String</td><td style="padding:3px 6px"><b><a href="binding/string.md">rust::String</a></b></td><td style="padding:3px 6px"></td></tr> 13<tr><td style="padding:3px 6px">&str</td><td style="padding:3px 6px"><b><a href="binding/str.md">rust::Str</a></b></td><td style="padding:3px 6px"></td></tr> 14<tr><td style="padding:3px 6px">&[T]</td><td style="padding:3px 6px"><b><a href="binding/slice.md">rust::Slice<const T></a></b></td><td style="padding:3px 6px"><sup><i>cannot hold opaque C++ type</i></sup></td></tr> 15<tr><td style="padding:3px 6px">&mut [T]</td><td style="padding:3px 6px"><b><a href="binding/slice.md">rust::Slice<T></a></b></td><td style="padding:3px 6px"><sup><i>cannot hold opaque C++ type</i></sup></td></tr> 16<tr><td style="padding:3px 6px"><b><a href="binding/cxxstring.md">CxxString</a></b></td><td style="padding:3px 6px">std::string</td><td style="padding:3px 6px"><sup><i>cannot be passed by value</i></sup></td></tr> 17<tr><td style="padding:3px 6px">Box<T></td><td style="padding:3px 6px"><b><a href="binding/box.md">rust::Box<T></a></b></td><td style="padding:3px 6px"><sup><i>cannot hold opaque C++ type</i></sup></td></tr> 18<tr><td style="padding:3px 6px"><b><a href="binding/uniqueptr.md">UniquePtr<T></a></b></td><td style="padding:3px 6px">std::unique_ptr<T></td><td style="padding:3px 6px"><sup><i>cannot hold opaque Rust type</i></sup></td></tr> 19<tr><td style="padding:3px 6px"><b><a href="binding/sharedptr.md">SharedPtr<T></a></b></td><td style="padding:3px 6px">std::shared_ptr<T></td><td style="padding:3px 6px"><sup><i>cannot hold opaque Rust type</i></sup></td></tr> 20<tr><td style="padding:3px 6px">[T; N]</td><td style="padding:3px 6px">std::array<T, N></td><td style="padding:3px 6px"><sup><i>cannot hold opaque C++ type</i></sup></td></tr> 21<tr><td style="padding:3px 6px">Vec<T></td><td style="padding:3px 6px"><b><a href="binding/vec.md">rust::Vec<T></a></b></td><td style="padding:3px 6px"><sup><i>cannot hold opaque C++ type</i></sup></td></tr> 22<tr><td style="padding:3px 6px"><b><a href="binding/cxxvector.md">CxxVector<T></a></b></td><td style="padding:3px 6px">std::vector<T></td><td style="padding:3px 6px"><sup><i>cannot be passed by value, cannot hold opaque Rust type</i></sup></td></tr> 23<tr><td style="padding:3px 6px"><b><a href="binding/rawptr.md">*mut T, *const T</a></b></td><td style="padding:3px 6px">T*, const T*</td><td style="padding:3px 6px"><sup><i>fn with a raw pointer argument must be declared unsafe to call</i></sup></td></tr> 24<tr><td style="padding:3px 6px">fn(T, U) -> V</td><td style="padding:3px 6px"><b><a href="binding/fn.md">rust::Fn<V(T, U)></a></b></td><td style="padding:3px 6px"><sup><i>only passing from Rust to C++ is implemented so far</i></sup></td></tr> 25<tr><td style="padding:3px 6px"><b><a href="binding/result.md">Result<T></a></b></td><td style="padding:3px 6px">throw/catch</td><td style="padding:3px 6px"><sup><i>allowed as return type only</i></sup></td></tr> 26</table> 27 28<br> 29 30The C++ API of the `rust` namespace is defined by the *include/cxx.h* file in 31the CXX GitHub repo. You will need to include this header in your C++ code when 32working with those types. **When using Cargo and the cxx-build crate, the header 33is made available to you at `#include "rust/cxx.h"`.** 34 35The `rust` namespace additionally provides lowercase type aliases of all the 36types mentioned in the table, for use in codebases preferring that style. For 37example `rust::String`, `rust::Vec` may alternatively be written `rust::string`, 38`rust::vec` etc. 39 40## Pending bindings 41 42The following types are intended to be supported "soon" but are just not 43implemented yet. I don't expect any of these to be hard to make work but it's a 44matter of designing a nice API for each in its non-native language. 45 46<br> 47 48<table> 49<tr><th>name in Rust</th><th>name in C++</th></tr> 50<tr><td>BTreeMap<K, V></td><td><sup><i>tbd</i></sup></td></tr> 51<tr><td>HashMap<K, V></td><td><sup><i>tbd</i></sup></td></tr> 52<tr><td>Arc<T></td><td><sup><i>tbd</i></sup></td></tr> 53<tr><td>Option<T></td><td><sup><i>tbd</i></sup></td></tr> 54<tr><td><sup><i>tbd</i></sup></td><td>std::map<K, V></td></tr> 55<tr><td><sup><i>tbd</i></sup></td><td>std::unordered_map<K, V></td></tr> 56</table> 57