3
3
Rust Language Support
4
4
#####################
5
5
6
- Rust is a systems programming language focused on safety, speed, and
7
- concurrency. Designed to prevent common programming errors such as
8
- null pointer dereferencing and buffer overflows, Rust emphasizes
9
- memory safety without sacrificing performance. Its powerful type
10
- system and ownership model ensure thread-safe programming, making it
11
- an ideal choice for developing reliable and efficient low-level code.
12
- Rust's expressive syntax and modern features make it a robust
13
- alternative for developers working on embedded systems, operating
14
- systems, and other performance-critical applications.
6
+ Rust is a systems programming language focused on safety, speed, and concurrency. Designed to
7
+ prevent common programming errors such as null pointer dereferencing and buffer overflows, Rust
8
+ emphasizes memory safety without sacrificing performance. Its powerful type system and ownership
9
+ model ensure thread-safe programming, making it an ideal choice for developing reliable and
10
+ efficient low-level code. Rust's expressive syntax and modern features make it a robust alternative
11
+ for developers working on embedded systems, operating systems, and other performance-critical
12
+ applications.
15
13
16
14
Enabling Rust Support
17
15
*********************
18
16
19
- Currently, Zephyr supports applications written in Rust and C. The
20
- enable Rust support, you must select the :kconfig:option: `CONFIG_RUST `
21
- in the application configuration file.
17
+ Currently, Zephyr supports applications written in Rust and C. The enable Rust support, you must
18
+ select the :kconfig:option: `CONFIG_RUST ` in the application configuration file.
22
19
23
- The rust toolchain is separate from the rest of the Zephyr SDK. It
24
- is recommended to use the `rustup `_ tool to install the rust
25
- toolchain. In addition to the base compiler, you will need to install
26
- core libraries for the target(s) you wish to compile on. It is
27
- easiest to determine what needs to be installed by trying a build.
28
- The diagnostics from the rust compilation will indicate the rust
29
- command needed to install the appropriate target support:
20
+ The rust toolchain is separate from the rest of the Zephyr SDK. It is recommended to use the
21
+ `rustup `_ tool to install the rust toolchain. In addition to the base compiler, you will need to
22
+ install core libraries for the target(s) you wish to compile on. It is easiest to determine what
23
+ needs to be installed by trying a build. The diagnostics from the rust compilation will indicate
24
+ the rust command needed to install the appropriate target support:
30
25
31
26
.. _rustup : https://rustup.rs/
32
27
@@ -39,10 +34,9 @@ command needed to install the appropriate target support:
39
34
= note: the `thumbv7m-none-eabi` target may not be installed
40
35
= help: consider downloading the target with `rustup target add thumb7m-none-eabi`
41
36
42
- In this case, the given ``rustup `` command will install the needed
43
- target support. The target needed will depend on both the board
44
- selected, as well as certain configuration choices (such as whether
45
- floating point is enabled).
37
+ In this case, the given ``rustup `` command will install the needed target support. The target
38
+ needed will depend on both the board selected, as well as certain configuration choices (such as
39
+ whether floating point is enabled).
46
40
47
41
Writing a Rust Application
48
42
**************************
@@ -52,8 +46,7 @@ See :zephyr_file:`samples/rust` for examples of an application.
52
46
CMake files
53
47
-----------
54
48
55
- The application should contain a :file: `CMakeFiles.txt `, similar to
56
- the following:
49
+ The application should contain a :file: `CMakeFiles.txt `, similar to the following:
57
50
58
51
.. code-block :: cmake
59
52
@@ -67,22 +60,20 @@ the following:
67
60
Cargo files
68
61
-----------
69
62
70
- Rust applications are built with Cargo. The Zephyr build system will
71
- configure and invoke cargo in your application directory, with options
72
- set so that it can find the Zephyr support libraries, and that the
73
- output will be contained within the Zephyr build directory.
63
+ Rust applications are built with Cargo. The Zephyr build system will configure and invoke cargo in
64
+ your application directory, with options set so that it can find the Zephyr support libraries, and
65
+ that the output will be contained within the Zephyr build directory.
74
66
75
- The :file: `Cargo.toml ` will need to have a ``[lib] `` section that sets
76
- ``crate-type = ["staticlib"] ``, and will need to include ``zephyr =
77
- "0.1.0" `` as a dependency. You can use crates.io and the Crate
78
- ecosystem to include any other dependencies you need. Just make sure
79
- that you use crates that support building with no-std.
67
+ The :file: `Cargo.toml ` will need to have a ``[lib] `` section that sets ``crate-type =
68
+ ["staticlib"] ``, and will need to include ``zephyr = "0.1.0" `` as a dependency. You can use
69
+ crates.io and the Crate ecosystem to include any other dependencies you need. Just make sure that
70
+ you use crates that support building with no-std.
80
71
81
72
Application
82
73
-----------
83
74
84
- The application source itself should live in file:`src/lib.rs `. A few
85
- things are needed. A minimal file would be:
75
+ The application source itself should live in file:`src/lib.rs `. A few things are needed. A minimal
76
+ file would be:
86
77
87
78
.. code-block :: rust
88
79
@@ -94,30 +85,25 @@ things are needed. A minimal file would be:
94
85
extern "C" fn rust_main() {
95
86
}
96
87
97
- The ``no_std `` declaration is needed to prevent the code from
98
- referencing the ``std `` library. The extern reference will cause the
99
- zephyr crate to be brought in, even if nothing from it is used.
100
- Practically, any meaningful Rust application on Zephyr will use
101
- something from this crate, and this line is not necessary. Lastly,
102
- the main declaration exports the main symbol so that it can be called
103
- by C code. The build ``rust_cargo_application() `` cmake function will
104
- include a small C file that will call into this from the C main
105
- function.
88
+ The ``no_std `` declaration is needed to prevent the code from referencing the ``std `` library. The
89
+ extern reference will cause the zephyr crate to be brought in, even if nothing from it is used.
90
+ Practically, any meaningful Rust application on Zephyr will use something from this crate, and this
91
+ line is not necessary. Lastly, the main declaration exports the main symbol so that it can be
92
+ called by C code. The build ``rust_cargo_application() `` cmake function will include a small C file
93
+ that will call into this from the C main function.
106
94
107
95
Zephyr Functionality
108
96
********************
109
97
110
- The bindings to Zephyr for Rust are under development, and are
111
- currently rather minimalistic.
98
+ The bindings to Zephyr for Rust are under development, and are currently rather minimalistic.
112
99
113
100
Bool Kconfig settings
114
101
---------------------
115
102
116
- Boolean Kconfig settings can be used from within Rust code. Due to
117
- design constraints by the Rust language, settings that affect
118
- compilation must be determined before the build is made. In order to
119
- use this in your application, you will need to use the
120
- ``zephyr-build `` crate, provided, to make these symbols available.
103
+ Boolean Kconfig settings can be used from within Rust code. Due to design constraints by the Rust
104
+ language, settings that affect compilation must be determined before the build is made. In order to
105
+ use this in your application, you will need to use the ``zephyr-build `` crate, provided, to make
106
+ these symbols available.
121
107
122
108
To your ``Cargo.toml `` file, add the following:
123
109
@@ -126,17 +112,16 @@ To your ``Cargo.toml`` file, add the following:
126
112
[build-dependencies]
127
113
zephyr-build = "0.1.0"
128
114
129
- Then, you will need a ``build.rs `` file to call the support function.
130
- The following will work:
115
+ Then, you will need a ``build.rs `` file to call the support function. The following will work:
131
116
132
117
.. code-block :: rust
133
118
134
119
fn main() {
135
120
zephyr_build::export_bool_kconfig();
136
121
}
137
122
138
- At this point, it will be possible to use the ``cfg `` directive in
139
- Rust on boolean Kconfig values. For example:
123
+ At this point, it will be possible to use the ``cfg `` directive in Rust on boolean Kconfig values.
124
+ For example:
140
125
141
126
.. code-block :: rust
142
127
@@ -149,8 +134,8 @@ Rust on boolean Kconfig values. For example:
149
134
Other Kconfig settings
150
135
----------------------
151
136
152
- All bool, numeric and string Kconfig settings are accessible from the
153
- `` zephyr::kconfig `` module. For example:
137
+ All bool, numeric and string Kconfig settings are accessible from the `` zephyr::kconfig `` module.
138
+ For example:
154
139
155
140
.. code-block :: rust
156
141
@@ -159,5 +144,5 @@ All bool, numeric and string Kconfig settings are accessible from the
159
144
Other functionality
160
145
-------------------
161
146
162
- Access to other functionality within zephyr is a work-in-progress, and
163
- this document will be updated as that is done.
147
+ Access to other functionality within zephyr is a work-in-progress, and this document will be updated
148
+ as that is done.
0 commit comments