diff options
| author | zhang san <[email protected]> | 2025-12-08 18:34:25 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-08 18:34:25 +0800 |
| commit | 69980f8b9d29b38dbd4ddf6373b4b45f3a138409 (patch) | |
| tree | 21dd9a8ce80e3d30192e9ffa56ea39a199bae309 /components/rust/examples/application/fs/src | |
| parent | cd1d47b87c5d47e131730e5506eb3ad77d357653 (diff) | |
Adding Rust Language Support for RT-Thread #10910
Diffstat (limited to 'components/rust/examples/application/fs/src')
| -rw-r--r-- | components/rust/examples/application/fs/src/lib.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/components/rust/examples/application/fs/src/lib.rs b/components/rust/examples/application/fs/src/lib.rs new file mode 100644 index 0000000000..53cf8561d1 --- /dev/null +++ b/components/rust/examples/application/fs/src/lib.rs @@ -0,0 +1,73 @@ + +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author notes + * 2025-10-20 foxglove Rust file operation test. + * 2025-10-29 foxglove Updated to demonstrate new modular macro interface + */ +#![no_std] + +extern crate alloc; + +use rt_macros::msh_cmd_export; +use rt_rust::{fs, println}; +use rt_rust::param::Param; +use em_component_log::{info, error}; + +#[msh_cmd_export(name = "rust_file_demo", desc = "Rust example app.")] +fn main(_param: Param) { + println!("[rust_file_test] start"); + + let mut file = match fs::OpenOptions::new() + .read(true) + .write(true) + .create(true) + .append(false) + .truncate(true) + .open("test.txt") + { + Ok(f) => { + info!("open test.txt ok"); + f + } + Err(e) => { + error!("open error: {:?}", e); + return; + } + }; + + if let Err(e) = file.write_all("Hello from FS wrapper!\n") { + error!("write_all error: {:?}", e); + return; + } + info!("write_all done"); + + if let Err(e) = file.flush() { + error!("flush error: {:?}", e); + } else { + info!("flush ok"); + } + + match file.read_to_string() { + Ok(s) => info!("read_back: {}", s), + Err(e) => error!("read_to_string error: {:?}", e), + } + + if let Err(e) = file.set_len(5) { + error!("truncate error: {:?}", e); + } else { + info!("truncate to 5 ok"); + } + + if let Err(e) = file.close() { + error!("close error: {:?}", e); + } else { + info!("close ok"); + } + + info!("[rust_file_test] end"); +} |
