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

相互運用性

翻訳が古くなっています RustとCとの相互運用性は、常に2つの言語間のデータ変換に依存しています。 そこで、2つの専用モジュールがstdlib内にあります。 std::ffiと呼ばれるものです。

std::ffi provides type definitions for C primitive types, such as char, int, and long. It also provides some utility for converting more complex types such as strings, mapping both &str and String to C types that are easier and safer to handle.

As of Rust 1.30, functionalities of std::ffi are available in either core::ffi or alloc::ffi depending on whether or not memory allocation is involved. The cty crate and the cstr_core crate also offer similar functionalities.

Rustの型中間表現Cの型
StringCStringchar *
&strCStrconst char *
()c_voidvoid
u32 or u64

, c_uint, unsigned int, tr(( en: [etc], de: [usw.], ja: [他], )), […], […], ) )

A value of a C primitive type can be used as one of the corresponding Rust type and vice versa, since the former is simply a type alias of the latter. For example, the following code compiles on platforms where unsigned int is 32-bit long.

fn foo(num: u32) {
    let c_num: c_uint = num;
    let r_num: u32 = c_num;
}

他のビルドシステムとの相互運用性

組込みプロジェクトにRustを組み込むための共通の要件は、Cargoとmakeやcmakeのような既存のビルドシステムとを組み合わせることです。

issue #61でこれに関する事例とユースケースを集めています。

RTOSとの相互運用性

RustをFreeRTOSやChibiOSといったRTOSに統合することは、まだ作業を進めている状態です。 特に、RTOSの関数をRustから呼び出すことはトリッキーです。

Currently, the following projects publicly support Rust<->RTOS interoperability:

issue #62でこれに関する事例とユースケースを集めています。