From 8d6c49536569b517a97bf3104edb83636e2b910e Mon Sep 17 00:00:00 2001 From: Shoji Tokunaga Date: Thu, 18 Jun 2026 23:53:58 +0900 Subject: [PATCH] examples/rust: Show command arguments in hello_rust_cargo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Print each argument passed from nsh with its index, and report when the example is running on the simulator target. This makes the Rust example demonstrate both C argv handling and target-specific output. Signed-off-by: Shoji Tokunaga  --- examples/rust/hello/src/lib.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/examples/rust/hello/src/lib.rs b/examples/rust/hello/src/lib.rs index dbe71ca0e..e9b0e0ade 100644 --- a/examples/rust/hello/src/lib.rs +++ b/examples/rust/hello/src/lib.rs @@ -1,7 +1,7 @@ extern crate serde; extern crate serde_json; -use core::ffi::{c_char, c_int}; +use core::ffi::{c_char, c_int, CStr}; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize)] @@ -10,10 +10,29 @@ struct Person { age: u8, } -// Function hello_rust_cargo without manglng +fn print_args(argc: c_int, argv: *mut *mut c_char) { + if argv.is_null() { + return; + } + + for i in 1..argc { + let arg = unsafe { *argv.add(i as usize) }; + if arg.is_null() { + continue; + } + + let arg = unsafe { CStr::from_ptr(arg) }.to_string_lossy(); + println!("{}: {}", i, arg); + } +} + +// Function hello_rust_cargo without mangling #[no_mangle] -pub extern "C" fn hello_rust_cargo_main(_argc: c_int, _argv: *mut *mut c_char) -> c_int { - // Print hello world to stdout +pub extern "C" fn hello_rust_cargo_main(argc: c_int, argv: *mut *mut c_char) -> c_int { + print_args(argc, argv); + + #[cfg(feature = "sim")] + println!("On simulator"); let john = Person { name: "John".to_string(), @@ -50,12 +69,5 @@ pub extern "C" fn hello_rust_cargo_main(_argc: c_int, _argv: *mut *mut c_char) - .block_on(async { println!("Hello world from tokio!"); }); - - #[cfg(not(feature = "sim"))] - loop { - // Do nothing - } - - #[cfg(feature = "sim")] 0 }