]> piware.de Git - learn-rust.git/blobdiff - call-rust-from-c/src/lib.rs
Call Rust function from C: Skeleton
[learn-rust.git] / call-rust-from-c / src / lib.rs
diff --git a/call-rust-from-c/src/lib.rs b/call-rust-from-c/src/lib.rs
new file mode 100644 (file)
index 0000000..f5e4384
--- /dev/null
@@ -0,0 +1,26 @@
+use std::os::raw;
+
+/// Return The Answer.
+///
+/// # Example
+///
+/// ```
+/// let a = call_rust_from_c::answer();
+/// assert_eq!(a, 42);
+/// ```
+#[no_mangle]
+// C 'int'; it does not actually hurt to have more specific types such as int32_t, but it's good to know that plain ints work
+pub extern "C" fn answer() -> raw::c_int {
+    return 42;
+}
+
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_answer() {
+        assert_eq!(answer(), 42);
+    }
+}