From e7f2fc92c8fd64485d635b1cc7b0c2897d78bc69 Mon Sep 17 00:00:00 2001 From: Alberto Sartori Date: Tue, 23 Jul 2024 17:24:16 +0200 Subject: rules-rust: add interoperability tutorials --- doc/interoperability/c-from-rust/main.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 doc/interoperability/c-from-rust/main.rs (limited to 'doc/interoperability/c-from-rust/main.rs') diff --git a/doc/interoperability/c-from-rust/main.rs b/doc/interoperability/c-from-rust/main.rs new file mode 100644 index 0000000..ff27dd7 --- /dev/null +++ b/doc/interoperability/c-from-rust/main.rs @@ -0,0 +1,21 @@ +use std::env; + +// declaration of the function implemented in the C library +extern "C" { + fn c_func(input: i32) -> i32; +} + +// wrapper to call the C function +fn c_call(i: i32) -> i32 { + unsafe { + return c_func(i); + } +} + +fn main() { + let args: Vec = env::args().collect(); + match args[1].parse::() { + Ok(i) => println!("Absolute value of {} is {}", i, c_call(i)), + Err(..) => println!("Wrong argument {}", args[1]), + }; +} -- cgit v1.2.3