X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=call-rust-from-c%2Fsrc%2Flib.rs;fp=call-rust-from-c%2Fsrc%2Flib.rs;h=f5e43842b908f49ffbbe5e3799f75156aa186521;hp=0000000000000000000000000000000000000000;hb=7ed2d982ac15df78e852fd5835d137881536a4ef;hpb=b74219aafd6977a99de7e52bc47f956b0b45cb01 diff --git a/call-rust-from-c/src/lib.rs b/call-rust-from-c/src/lib.rs new file mode 100644 index 0000000..f5e4384 --- /dev/null +++ b/call-rust-from-c/src/lib.rs @@ -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); + } +}