Last edited 4 months ago

How to develop a Rust Application

Applicable for STM32MP13x lines, STM32MP15x lines

1. Introduction[edit | edit source]

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, which simplifies 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[edit | edit source]

To enable Rust support in the OpenSTLinux SDK, an add-on is necessary. To install this SDK add-on, you need to download the MP1-DEV-x86-RUST software [2] 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-stm32mp1-openstlinux-*-addon-rust.tar.gz
source /opt/st/stm32mp1/sdk/environment-setup-*
sudo --preserve-env SDK-x86_64-stm32mp1-openstlinux-*-addon-rust/install.sh
  SDK add-on is going to be deployed into /opt/st/stm32mp1/sdk, do you agree [y/n]? y
  Deploying add-on files into /opt/st/stm32mp1/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/stm32mp1/sdk/environment-setup-*

3. Create, build, and run Rust applications[edit | edit source]

At this point it is assumed OpenSTLinux SDK is installed and set up correctly, and that the Rust SDK add-on has already been deployed.

3.1. Create[edit | edit source]

A Rust application can be created using the following command:

cargo new --bin hello-world
     Created binary (application) `hello-world` package

This will generate 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[edit | edit source]

Assuming the OpenSTLinux SDK is set up correctly, Rust applications can be built for OpenSTLinux using the "cargo build" command. For instance with 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[edit | edit source]

Once built, the Rust application must be copied onto the running system at the expected location. For instance with the "hello-world" example, using scp:

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[edit | edit source]

4.1. "failed to run `rustc` to learn about target-specific information"[edit | edit source]

If you encounter the error message "failed to run `rustc` to learn about target-specific information," it may indicate that you forgot to source the environment setup script. In that case, running the following command should fix the issue:

source /opt/st/stm32mp1/sdk/environment-setup-*

4.2. "Couldn't resolve host name"[edit | edit source]

If you are seeing the error message "Couldn't resolve host name" while using Cargo, it could be due to issues with your network settings. Please check your network settings (/etc/resolv.conf, https_proxy env. variable, ...) and make sure that they are configured correctly.

4.3. "failed to authenticate while downloading repository"[edit | edit source]

If you encounter the error message "failed to authenticate while downloading repository", it may be caused by problems with your credential settings. To resolve this issue, please verify your settings such as ssh-agent or git's credential helper, and ensure that they are correctly configured.

Note: Even if you are using an unencrypted SSH key, you must use ssh-agent. For instance:

eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
cargo build
...