【CXX】6 内置绑定

除了所有基本类型(如 i32 <=> int32_t),以下常见类型可以用于共享结构体的字段以及外部函数的参数和返回值。

Rust 中的名称 C++ 中的名称 限制条件
String rust::String
&str rust::Str
&[T] rust::Slice 不能包含不透明的 C++ 类型
&mut [T] rust::Slice 不能包含不透明的 C++ 类型
CxxString std::string 不能按值传递
Box rust::Box 不能包含不透明的 C++ 类型
UniquePtr std::unique_ptr 不能包含不透明的 Rust 类型
SharedPtr std::shared_ptr 不能包含不透明的 Rust 类型
[T; N] std::array<T, N> 不能包含不透明的 C++ 类型
Vec rust::Vec 不能包含不透明的 C++ 类型
CxxVector std::vector 不能按值传递,不能包含不透明的 Rust 类型
*mut T, *const T T*, const T* 带有裸指针参数的函数必须声明为 unsafe 才能调用
fn(T, U) -> V rust::Fn<V(T, U)> 目前仅支持从 Rust 传递到 C++
Result throw/catch 仅允许作为返回类型

C++ 中 rust 命名空间的 API 由 CXX GitHub 仓库中的 include/cxx.h 文件定义。在使用这些类型时,你需要在 C++ 代码中包含此头文件。当使用 Cargo 和 cxx-build crate 时,可以通过 #include "rust/cxx.h" 包含此头文件。

rust 命名空间还提供了表中所有类型的小写别名,适用于偏好这种风格的代码库。例如,rust::String 和 rust::Vec 也可以写成 rust::string 和 rust::vec 等。

待实现的绑定

以下类型计划在"不久的将来"支持,但目前尚未实现。这些类型的实现预计不会太困难,但需要为每种类型在其非原生语言中设计一个良好的 API。

Rust 中的名称 C++ 中的名称
BTreeMap<K, V> 待定
HashMap<K, V> 待定
Arc 待定
Option 待定
待定 std::map<K, V>
待定 std::unordered_map<K, V>
相关推荐
数据知道11 小时前
claw-code 源码分析:从 TypeScript 心智到 Python/Rust——跨栈移植时类型、边界与错误模型怎么对齐?
python·ai·rust·typescript·claude code·claw code
freshman_y12 小时前
Qtcreator怎么新建安卓项目?编写一个五子棋游戏APP?
android·qt
wljy117 小时前
Qt入门(一)
开发语言·qt
火山上的企鹅17 小时前
QT/QGroundControl 实战:Mission Planner 航线在 QGC 中出现 Takeoff 落到 (0,0) 的排查与修复
qt·mp·qgc·无人机开发
雪的季节18 小时前
qt信号槽跨线程使用时候的坑
java·开发语言·qt
yy_xzz18 小时前
【Qt 开发笔记】能扛住断电、多线程的通用配置类(移植直接用)
笔记·qt
Rust研习社19 小时前
深入浅出 Rust 迭代器:从基础用法到性能优化
rust
丁劲犇20 小时前
改造传统Qt6Widgets程序为多会话MCPServer生产力工具-技巧与实现
qt·ai·agent·并发·mcp·mcpserver·widgets
sycmancia21 小时前
Qt——对话框及其类型
开发语言·qt
@atweiwei21 小时前
langchainrust:Rust 版 LangChain 框架(LLM+Agent+RAG)
开发语言·rust·langchain·agent·向量数据库·rag