That is, it just links in Rust libraries without breaking a sweat.
I just followed a couple of tutorials about how to create a Rust library
with functions callable from C. The FFI page
in the manual is more than sufficient.
The Rust Code
To get a workable (shared) library, you need the following Rust code, along side the Cargo.toml
file.
File hello_string.rs it the following:
// -*- Mode: Rust -*- // hello_string.rs #[unsafe(no_mangle)] pub extern "C" fn get_string() -> *const u8 { b"Hello FFI World!\0".as_ptr() } // hello_string.rs ends here.
The Cargo.toml file is below:
### Cargo.toml [package] name = "hello_string" version = "0.1.0" edition = "2024" [lib] crate-type = ["cdylib"] path = "hello_string.rs" ### Cargo.toml ends here.
This is what is needed to generate a library file that can then be called by C (I only tested it on Mac OS), and it works as advertised.
$ cargo build --release --lib
Compiling hello_string v0.1.0 (/Users/marcoxa/Projects/Lang/Rust/calling/Code)
Finished `release` profile [optimized] target(s) in 9.62s
$
As you see, there does not seem to be anything special about the creation of the library, so I suspected that CFFI would be able to load it. It does!
The CL CFFI Code
With the file libhello_string.dylib safely tucked in the target/release/ directory
I went on to setup a CFFI spec for it.
;;;; -*- Mode: Lisp -*- ;;;; test-rust-calling.lisp (in-package "RUST-CALLING") (use-package "CFFI") (define-foreign-library hello_string (:darwin "libhello_string.dylib" ; Rust/Cargo generated. :search-path #P"../Code/target/release/" ; Note the (irrelevant) relative path. )) (use-foreign-library hello_string) (defcfun "get_string" :string) (defun rusty-call () (get-string)) ;;;; test-rust-calling.lisp ends here.
That's it!
CL-PROMPT> (load "test-rust-calling.lisp")
; Loading text file /Users/marcoxa/Projects/Lang/Rust/calling/CL/test-rust-calling.lisp
#P"/Users/marcoxa/Projects/Lang/Rust/calling/CL/test-rust-calling.lisp"
CL-PROMPT> (rust-calling::rusty-call)
"Hello FFI World!"
Look ma! No C!
Conclusions
CFFI is not rusty at all!
'(cheers)







