mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-02-06 09:02:20 +08:00
RT-Thread Rust Example User Applications
This directory contains a series of example user applications and components that demonstrate Rust programming capabilities in RT-Thread. These examples showcase how to use the Rust programming language for system programming in the RT-Thread real-time operating system.
Directory Structure
example_usrapp/
├── SConscript # SCons build script
├── fs/ # File system operation examples
├── loadlib/ # Dynamic library loading examples
├── mutex/ # Mutex synchronization examples
├── param/ # Parameter handling examples
├── queue/ # Message queue examples
├── semaphore/ # Semaphore synchronization examples
└── thread/ # Thread management examples
Build and Run
Prerequisites
- Ensure Rust support is enabled in RT-Thread configuration (
RT_USING_RUST), and enable examples compilation (RT_USING_RUST_EXAMPLES) and application examples compilation (RT_RUST_BUILD_APPLICATIONS). - Install Rust toolchain and related dependencies
- Configure the correct cross-compilation environment
Build Steps
-
Execute in the RT-Thread project root directory:
scons -j$(nproc) -
The build system will automatically:
- Detect enabled RT-Thread configurations
- Enable corresponding Rust features based on configuration
- Compile all example applications as static libraries
- Link to the final firmware image
Running Examples
After RT-Thread system startup, you can run various examples through the command line:
# View all available examples
help
# Run thread example
rust_thread_demo
# Run file system example
rust_file_demo
# Other examples similarly...
Technical Features
Dependency Management
All example applications depend on the following core components:
rt-rust: RT-Thread Rust binding librarymacro_main: Main function macro support
The fs example also depends on the logging component:
em_component_log: Logging component
Feature Configuration
- Support for conditional compilation features
- Automatic feature detection and enabling
- Modular functionality configuration
Build System Integration
- Fully integrated with SCons build system
- Support for clean and incremental builds
- Automatic dependency management
Development Guide
Adding New Examples
- Create a new directory under
example_usrapp/ - Add
Cargo.tomlconfiguration file - Implement example code in
src/lib.rs - Use
#[macro_main_use]macro to define entry point
Example Template
#![no_std]
use macro_main::macro_main_use;
use rt_rust::param::Param;
use rt_rust::println;
#[macro_main_use(
name = "your_demo_name",
component = "Whether it's a component example",
app = "Whether it's a user application example",
cmd = true,
desc = "Your demo description."
)]
fn main(_param: Param) {
println!("Hello from your demo!");
// Your example code...
}
Troubleshooting
Common Issues
- Compilation failure: Check if Rust toolchain is correctly installed
- Linking errors: Confirm cross-compilation target configuration is correct
- Runtime errors: Check stack size and memory configuration
Debugging Suggestions
- Use
println!for basic debug output - Enable logging features to get detailed information
- Check if RT-Thread configuration items are correctly enabled