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 }