Contenido principal

Generate Rust Bindings for Simulink Generated C Code

This example shows how to use the coder.RustWrapper.genRustBindings function to generate Foreign Function Interface (FFI) bindings for C code generated from Simulink® models. These bindings enable Rust® programs to call functions generated from Simulink models and access model data directly. To use this function, you must have an Embedded Coder® license and install Simulink Support Package for Rust Code. For more information about installing the support package, see Install Simulink Support Package for Rust Code.

You can run coder.RustWrapper.genRustBindings function in either of these ways:

  • Run the function automatically by configuring it as a post-code-generation command.

  • Run the function after code generation using existing build information.

Generate Rust Bindings from a Simulink Model

Open the model.

open_system("ex_if_else_SL")

Run the Rust Wrapper Generator as a Post‑Code‑Generation Command

Open the Configuration Parameters dialog box and navigate to Code Generation > Advanced Parameters. In the Post code generation command box, enter:

coder.RustWrapper.genRustBindings(buildInfo)

Configure the model for Embedded Coder code generation by setting System target file to ert.tlc and Language to C. Build the model to generate code.

slbuild("ex_if_else_SL")

The coder.RustWrapper.genRustBindings command uses the build information to generate Rust wrappers for the C code, resulting in the creation of the rust_bindings folder. By default, MATLAB® places this folder in the working folder, unless you explicitly customize the code generation or file generation path. The generated Rust bindings include interface functions and data extracted from the Simulink code descriptor that contains metadata about the generated C code.

Run the Rust Wrapper Generator Using Build Information from an Existing Build

Navigate to the folder that contains the build artifacts from the previous build, and set it as the current working folder. Load the model and the build information file.

load_system("ex_if_else_SL")
load("buildInfo.mat")

Run the Rust wrapper generator using the loaded buildInfo object.

coder.RustWrapper.genRustBindings(buildInfo)

For more information about the example model ex_if_else_SL, see If-Else.

Limitations

  • The target language must be set to C before generating bindings.

  • Generating Rust bindings for AUTOSAR targets or models that use macro‑based functions is not supported.

  • Generating Rust bindings on macOS is not supported.

  • Generating Rust bindings for models with C API enabled is not supported.

  • Avoid using Rust keywords such as in, match, type, or struct as block names in the Simulink model. Using Rust keywords as block names causes compilation errors in the generated bindings. For a complete list of Rust keywords, see The Rust Reference.

  • Code interface packaging (component) and Code interface packaging (subcomponent) parameters must be set to Nonreusable function.

See Also

|

Topics