The goal of this guide is to smooth the process of using Rust on fuchsia for the first time. As such this guide will have some redundancy with other existing docs.
Before starting, this guide assumes you can successfully build and run fuchsia, either on real hardware or virtualized. If you have not built fuchsia before, please see the Getting Started on Fuchsia Docs.
If this is your first time using Rust, we recommend you learn a little more about the language before proceeding. We recommend the canonical intro to Rust resource, The Rust Programming Language Book (TRPL) . Alternatively, Learning Rust With Entirely Too Many Linked Lists is also a good option.
The recommended way of installing rust is to use rustup.
There are two approaches to building a Rust binary which will run on Fuchsia.
For the first approach, we use a tool called Fargo, which is a convenience layer built on top of Cargo (Rust's package manager and build system) to make compiling binaries targeting Fuchsia easier. See getting started in Fargo's readme for how to get started building and running Rust binaries using Fargo.
The second approach will require using GN, Fuchsia‘s build tool, to define Rust binaries and packages. For more on how to define fuchsia packages written in Rust see the fuchsia rust doc. Once you have defined your the package for your Rust binary in GN, you can simple include it when calling packages/gn/gen.py (or fset if you’re using the env.sh functions).
fx set x86-64 --release --packages garnet/packages/my_rust_package
Then, when you build Fuchsia, your binary will be included in the final build. Additionally, when you run Fuchsia your binary should be copied onto the running machine.
Rust has FIDL bindings to support writing FIDL services in Rust. For an example of a fidl service implemented in Rust, check out Rust implementations of the echo server and client.