7.2.9 其他类型
09-其他类型 — Comprehensive Rust
1分钟内可阅读完
译文 · 基于 Comprehensive Rust
原文链接: https://google.github.io/comprehensive-rust/android/interoperability/cpp/type-mapping.html
7.2.9 其他类型
| Rust 类型 | C++ 类型 |
|---|---|
String | rust::String |
&str | rust::Str |
CxxString | std::string |
&[T]/&mut [T] | rust::Slice |
Box<T> | rust::Box<T> |
UniquePtr<T> | std::unique_ptr<T> |
Vec<T> | rust::Vec<T> |
CxxVector<T> | std::vector<T> |
- 这些类型可用于共享结构体的字段,以及 extern 函数的参数与返回值。
- 注意 Rust 的
String并不直接映射到std::string。原因有几点:
std::string并不维护String所要求的 UTF-8 不变量。- 两种类型在内存中的布局不同,因此不能在语言之间直接传递。
std::string需要与 Rust 移动语义不匹配的移动构造函数,因此std::string不能按值传给 Rust。