run rust code in lua
简介
lua由现成的扩展库,用户调用外部C 函数,使用C 数据结构。因此,rust代码需要先编译成c的动态连接库。
example
Cargo.toml
[package]
name = "example"
version = "0.1.0"
edition = "2018"
[lib]
name = "double_input"
crate-type = ["staticlib", "cdylib"]
lib.rs
#![crate_type = "dylib"]
#[no_mangle]
pub extern fn double_input(input: i32) -> i32 {
input * 2
}
lua
编译rust cargo build --release
ffi = require("ffi")
ffi.cdef[[
int32_t double_input(int32_t n);
]]
rust_lib = ffi.load("./libdouble_input.so")
n = 1234567
print(rust_lib.double_input(n))