1. Introduction
This article describes how to develop Rust[1] applications using Cargo in an OpenSTLinux environment. Rust is a system programming language that focuses on performance, safety, and concurrency. It is designed to be memory-safe and thread-safe, making it ideal for building reliable and efficient software.
Cargo is Rust's package manager and build tool, simplifying the process of managing dependencies, compiling code, and creating distributable packages. Together, Rust and Cargo provide a powerful and modern development environment for building high-performance applications.
2. Deploy the Rust SDK add-on
To enable Rust support in the OpenSTLinux SDK, an add-on is necessary. To install this SDK add-on, download the MPx-DEV-x86-RUST software for STM32MP1[2] and/or for STM32MP2[3], and extract it to any location. Once extracted, and after installing and sourcing the SDK environment script, run the install.sh script from the SDK add-on. For instance:
tar -xf en.sdk-x86_64-stm32mpX-openstlinux-...-addon-rust.tar.gz
source /opt/st/stm32mpX/sdk/environment-setup-...
sudo --preserve-env SDK-x86_64-stm32mpX-openstlinux-...-addon-rust/install.sh
SDK add-on is going to be deployed into /opt/st/stm32mpX/sdk, do you agree [y/n]? y
Deploying add-on files into /opt/st/stm32mpX/sdk
SDK add-on has been successfully deployed and is ready to be used.
You must source the environment setup script again.
source /opt/st/stm32mpX/sdk/environment-setup-*
3. Create, build, and run Rust applications
At this point it is assumed OpenSTLinux SDK has been installed and set up correctly, and that the Rust SDK add-on has already been deployed.
3.1. Create
A Rust application can be created using the following command:
cargo new --bin hello-world
Created binary (application) `hello-world` package
This generates a directory that contains everything required to build a minimal Rust application:
tree hello-world
hello-world
├── Cargo.toml
└── src
└── main.rs
cat hello-world/src/main.rs
fn main() {
println!("Hello, world!");
}
3.2. Build
Assuming the OpenSTLinux SDK has been set up correctly, Rust applications can be built for OpenSTLinux using the cargo build
command. The example below uses the "hello-world" example created previously:
cd hello-world
cargo build
Compiling hello-world v0.1.0
Finished dev [unoptimized + debuginfo] target(s) in 0.17s
3.3. Run
Once built, the Rust application must be copied onto the running system at the expected location. The example below uses the "hello-world" example and the scp
command:
scp target/${CARGO_BUILD_TARGET}/debug/hello-world root@<board ip address>:/usr/bin
Now it can be executed easily from the running system:
hello-world
Hello, world!
4. Potential issues & solutions
4.1. "failed to run `rustc` to learn about target-specific information"
The error message "failed to run `rustc` to learn about target-specific information" may indicate that the environment setup script was not sourced. If this is the case, run the following command to fix the issue:
source /opt/st/stm32mp1/sdk/environment-setup-*
4.2. "Couldn't resolve host name"
The error message "Couldn't resolve host name" while using Cargo may be due to issues with the network settings. Check the network settings (such as /etc/resolv.conf and the https_proxy env. variable) and make sure that they are configured correctly.
4.3. "failed to authenticate while downloading repository"
The error message "failed to authenticate while downloading repository" may be caused by problems with the credential settings. To resolve this issue, verify these settings, such as ssh-agent or git's credential helper, and make sure that they are configured correctly.
Note: Even when using an unencrypted SSH key, using ssh-agent is mandatory. For instance:
eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
cargo build
...