summaryrefslogtreecommitdiff
path: root/doc/interoperability/c-from-rust/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'doc/interoperability/c-from-rust/main.rs')
-rw-r--r--doc/interoperability/c-from-rust/main.rs21
1 files changed, 21 insertions, 0 deletions
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<String> = env::args().collect();
+ match args[1].parse::<i32>() {
+ Ok(i) => println!("Absolute value of {} is {}", i, c_call(i)),
+ Err(..) => println!("Wrong argument {}", args[1]),
+ };
+}