Skip to content

Commit

Permalink
docs: develop: languages: Add initial documentation for Rust
Browse files Browse the repository at this point in the history
Start a simple set of documentation on using Rust within Zephyr.  This
gives an overview of how to use Rust, and pointers to the generated API
documentation.

Signed-off-by: David Brown <[email protected]>
  • Loading branch information
d3zd3z committed Feb 25, 2025
1 parent 9cc8301 commit d9124b3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/develop/languages/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Language Support

c/index.rst
cpp/index.rst
rust/index.rst
91 changes: 91 additions & 0 deletions doc/develop/languages/rust/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
.. _language_rust:

Rust Language Support
#####################

Rust is a modern systems programming language designed to provide memory safety, concurrency, and
performance without sacrificing low-level control. It achieves this through a unique ownership model
that eliminates common bugs like null pointer dereferencing and data races at compile time. Rust’s
emphasis on safety and correctness makes it particularly well-suited for embedded systems and
environments where reliability is critical. Additionally, Rust offers powerful abstractions without
a runtime or garbage collector, allowing developers to write both high-level code and low-level
hardware interactions with confidence and efficiency. These attributes make Rust a strong choice for
projects on Zephyr, where resource constraints and system stability are paramount.

Enabling Rust Support
*********************

In order to enable Rust support in a Zephyr application, a few things need to be done:

1. As Rust is currently an optional module, the module needs to be enabled. The easiest way to do
this is with west:

.. code-block:: console
west config manifest.project-filter +zephyr-lang-rust
west sync
This should cause the Rust language support to be placed in ``modules/lang/rust``.

2. Enable Rust support, via :kconfig:option:`CONFIG_RUST` in the ``prj.conf``. The easiest way to do
this, and the Cmake part is to start with one of the samples in ``modules/lang/rust/samples``.

3. Configure the application's ``CMakeLists.txt`` file to support cmake. Again this is easiest to
copy from a sample, but this will look something like:

.. code-block:: cmake
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(my_app)
rust_cargo_application()
4. Create a ``Cargo.toml`` that describes how to build the Rust application. From the Hello World
sample:

.. code-block:: toml
[package]
# This must be rustapp for now.
name = "rustapp"
version = "0.1.0"
edition = "2021"
description = "The description of my app"
license = "Apache-2.0 or MIT"
[lib]
crate-type = ["staticlib"]
[dependencies]
zephyr = "0.1.0"
log = "0.4.22"
The only required dependency is ``zephyr`` which provides the zephyr crate that is used to
interface with Zephyr.

5. Build as you would any other Zephyr application. Only a few targets currently support Rust
(these can be seen in the ``modules/lang/rust/etc/platforms.txt` file).

Check failure on line 69 in doc/develop/languages/rust/index.rst

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

SphinxLint

doc/develop/languages/rust/index.rst:69 default role used (hint: for inline literals, use double backticks) (default-role)

Check failure on line 69 in doc/develop/languages/rust/index.rst

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

SphinxLint

doc/develop/languages/rust/index.rst:69 found an unbalanced inline literal markup. (unbalanced-inline-literals-delimiters)
API Documentation
*****************
The `API Documentation`_ for the latest version in the module is kept on gh-pages.
.. _API_documentation:
https://zephyrproject-rtos.github.io/zephyr-lang-rust/nostd/zephyr/index.html
This documentation is generated for a general target, with all features enabled. Once you have an
application that is buildable, you can generate documentation specifically for your target:
.. code-block:: console
west build -t rustdoc
...
Generated /my/path/app/zephyr/build/doc/rust/target/riscv32i-unknown-none-elf/doc/rustapp/index.html
The path printed at the end can be opened in a browser. This top level docs will be for your
application itself. Look for the 'zephyr' crate on the left side bar, and this will take you to the
docs for Zephyr. This page will also generate local docs for any dependencies used by your
application, directly or indirectly.

0 comments on commit d9124b3

Please sign in to comment.