]> piware.de Git - learn-rust.git/blob - call-rust-from-c/src/lib.rs
Call Rust function from C: Skeleton
[learn-rust.git] / call-rust-from-c / src / lib.rs
1 use std::os::raw;
2
3 /// Return The Answer.
4 ///
5 /// # Example
6 ///
7 /// ```
8 /// let a = call_rust_from_c::answer();
9 /// assert_eq!(a, 42);
10 /// ```
11 #[no_mangle]
12 // 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
13 pub extern "C" fn answer() -> raw::c_int {
14     return 42;
15 }
16
17
18 #[cfg(test)]
19 mod tests {
20     use super::*;
21
22     #[test]
23     fn test_answer() {
24         assert_eq!(answer(), 42);
25     }
26 }