FIDL is the primary mechanism for structured IPC within Fuchsia. The easiest
way to use FIDL from Rust is by generating a "FIDL crate" from a FIDL library
and then importing it from your Rust library or binary.

See [the FIDL Rust bindings](https://fuchsia.dev/docs/reference/fidl/bindings/rust-bindings) to
understand how different FIDL constructs map into their Rust equivalents, and
[the FIDL Rust tutorials](https://fuchsia.dev/docs/development/languages/fidl/tutorials/rust) for examples on using the Rust
bindings.

## Build Instructions

When a [GN `fidl` rule](https://cs.opensource.google/fuchsia/fuchsia/+/main:/build/fidl/fidl.gni) is defined for a FIDL library,
a corresponding FIDL Rust crate is automatically generated under
the original target name appended with `_rust`. Transitive dependencies on
other FIDL libraries are resolved automatically.
For example, given the declaration:

    # //src/tictactoe/BUILD.gn

    fidl("games.tictactoe") { ... }

The FIDL crate target is
`//src/tictactoe:games.tictactoe_rust`. To use the FIDL crate,
add the target to the `deps` field of the
[`rustc_*` build rule](https://fuchsia.dev/docs/development/languages/rust/README#build)
for your Rust crate. For example:

    rustc_binary("tictactoe") {
      # ...
      deps = ["//src/tictactoe:games.tictactoe_rust"]
    }

The Rust crate will be named `fidl_games_tictactoe` and its items can now be
imported:

    use fidl_games_tictactoe::BOARD_SIZE;

In the Fuchsia tree, frequently used FIDL crates are often aliased to
a shorter name for brevity, like so:

    use fidl_fuchsia_io as fio;
    use fidl_fuchsia_data as fdata;

## Generated Documentation

Documentation in HTML format can be automatically generated for a FIDL crate
using the `fx rustdoc-link` command. Generate the documentation with:

    fx rustdoc-link //src/tictactoe:games.tictactoe_rust

Open the docs/rust/doc directory (found in your root build directory) using
your browser. See `fx rustdoc-link --help` for more.

FIDL crates in the public Fuchsia source tree are published in the
[Fuchsia Rust API reference](https://fuchsia.dev/go/rustdoc/).

## Generated Rust Code

To manually inspect the generated Rust code for a FIDL crate, the Rust
source files are available under the `BUILD_DIR/fidling/gen` (refer to the
[Generated code guide](https://fuchsia.dev/docs/development/languages/fidl/guides/generated-code#rust) for an example). Note that
the FIDL crate must first have been built (e.g. using `fx build`).
[](https://issues.fuchsia.dev/issues/new?component=1439230&template=1881640&description=Issue+on:+https://fuchsia.dev/docs/development/languages/rust/fidl_crates.md "Report a bug or suggest improvements on this page") [](https://cs.opensource.google/fuchsia/fuchsia/+/main:docs/development/languages/rust/fidl_crates.md "View the source code of this page") [](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/development/languages/rust/fidl_crates.md&cloneRepo=fuchsia/fuchsia:refs/heads/main&fullPath=fuchsia/docs/development/languages/rust/fidl_crates.md "Edit the source code of this page")