├── .gitignore ├── README.md ├── c-to-rust ├── Cargo.toml ├── Makefile └── src │ ├── lib.rs │ └── main.c ├── cpp-to-rust ├── Cargo.toml ├── Makefile └── src │ ├── example.h │ ├── lib.rs │ └── main.cpp ├── crystal-to-rust ├── Cargo.toml ├── Makefile └── src │ ├── double.cr │ └── lib.rs ├── csharp-to-rust ├── .gitignore ├── Cargo.toml ├── Makefile └── src │ ├── double.cs │ └── lib.rs ├── dart-to-rust ├── Cargo.toml ├── Makefile └── src │ ├── lib.rs │ └── main.dart ├── go-to-rust-cgo-dynamic ├── Cargo.toml ├── Makefile └── src │ ├── double.go │ └── lib.rs ├── go-to-rust-cgo-static ├── Cargo.toml ├── Makefile └── src │ ├── double.go │ └── lib.rs ├── haskell-to-rust ├── Cargo.toml ├── Makefile └── src │ ├── Main.hs │ └── lib.rs ├── julia-to-rust ├── Cargo.toml ├── Makefile └── src │ ├── lib.rs │ └── main.jl ├── luajit-to-rust ├── Cargo.toml ├── Makefile └── src │ ├── lib.rs │ └── main.lua ├── node-to-rust ├── Cargo.toml ├── Makefile └── src │ ├── lib.rs │ └── main.js ├── perl-to-rust ├── Cargo.toml ├── Makefile └── src │ ├── lib.rs │ └── main.pl ├── php-to-rust ├── Cargo.toml ├── Makefile ├── README.md └── src │ ├── ffi.php │ └── lib.rs ├── python-to-rust ├── Cargo.toml ├── Makefile └── src │ ├── lib.rs │ └── main.py ├── ruby-to-rust ├── Cargo.toml ├── Makefile └── src │ ├── lib.rs │ └── main.rb ├── rust-to-c ├── Cargo.toml ├── build.rs └── src │ ├── double.c │ └── main.rs ├── rust-to-cmake ├── Cargo.toml ├── build.rs ├── libdouble │ ├── CMakeLists.txt │ └── double.c └── src │ └── main.rs ├── rust-to-cpp ├── Cargo.toml ├── build.rs └── src │ ├── main.rs │ └── triple.cpp ├── rust-to-fortran ├── Cargo.toml ├── build.rs └── src │ ├── double.f90 │ └── main.rs ├── rust-to-go-dynamic ├── Cargo.toml ├── build.rs └── src │ ├── double.go │ └── main.rs └── swift-to-rust ├── Cargo.toml ├── Makefile └── src ├── double.h ├── lib.rs └── main.swift /.gitignore: -------------------------------------------------------------------------------- 1 | Cargo.lock 2 | target 3 | node_modules 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rust FFI Examples 2 | 3 | This is an example repository that shows how to interface between Rust and a 4 | various number of other languages. 5 | 6 | At this time, though, I unfortunately don't have time to continue maintaining 7 | this so I'm going to archive the repository. Thanks to everyone for 8 | contributing! 9 | -------------------------------------------------------------------------------- /c-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c-to-rust" 3 | version = "0.1.0" 4 | authors = ["Alex Crichton "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["staticlib"] 9 | -------------------------------------------------------------------------------- /c-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | LDFLAGS := -Wl,-dead_strip 3 | else 4 | LDFLAGS := -Wl,--gc-sections -lpthread -ldl 5 | endif 6 | 7 | all: target/double 8 | target/double 9 | 10 | target: 11 | mkdir -p $@ 12 | 13 | target/double: target/main.o target/debug/libdouble_input.a 14 | $(CC) -o $@ $^ $(LDFLAGS) 15 | 16 | target/debug/libdouble_input.a: src/lib.rs Cargo.toml 17 | cargo build 18 | 19 | target/main.o: src/main.c | target 20 | $(CC) -o $@ -c $< 21 | 22 | clean: 23 | rm -rf target 24 | -------------------------------------------------------------------------------- /c-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![crate_type = "staticlib"] 2 | 3 | #[no_mangle] 4 | pub extern fn double_input(input: i32) -> i32 { 5 | input * 2 6 | } 7 | -------------------------------------------------------------------------------- /c-to-rust/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern int32_t double_input(int32_t input); 5 | 6 | int main() { 7 | int input = 4; 8 | int output = double_input(input); 9 | printf("%d * 2 = %d\n", input, output); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /cpp-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "cpp-to-rust" 3 | version = "0.1.0" 4 | authors = ["timmonfette1 "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["dylib"] 9 | -------------------------------------------------------------------------------- /cpp-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) 8 | g++ src/main.cpp -L ./target/debug/ -ldouble_input -o run 9 | LD_LIBRARY_PATH=./target/debug/ ./run 10 | 11 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 12 | cargo build 13 | 14 | clean: 15 | rm -rf target 16 | rm -rf run 17 | -------------------------------------------------------------------------------- /cpp-to-rust/src/example.h: -------------------------------------------------------------------------------- 1 | #ifndef _EXAMPLE_H 2 | #define _EXAMPLE_H 3 | 4 | #ifdef __cplusplus 5 | extern "C"{ 6 | #endif 7 | 8 | int double_input(int input); 9 | 10 | #ifdef __cplusplus 11 | } 12 | #endif 13 | #endif 14 | -------------------------------------------------------------------------------- /cpp-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } 5 | -------------------------------------------------------------------------------- /cpp-to-rust/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "example.h" 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int input = 10; 8 | int output = double_input(input); 9 | cout<"] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["dylib"] -------------------------------------------------------------------------------- /crystal-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) 8 | LIBRARY_PATH=$(PWD)/target/debug:$(LIBRARY_PATH) crystal build src/double.cr -o double 9 | LD_LIBRARY_PATH=./target/debug ./double 10 | 11 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 12 | cargo build 13 | cd src 14 | 15 | clean: 16 | rm -rf target double double.dwarf -------------------------------------------------------------------------------- /crystal-to-rust/src/double.cr: -------------------------------------------------------------------------------- 1 | @[Link("double_input")] 2 | lib RustLib 3 | fun double_input(input: Int32) : Int32 4 | end 5 | 6 | puts RustLib.double_input(2) -------------------------------------------------------------------------------- /crystal-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern "C" fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } -------------------------------------------------------------------------------- /csharp-to-rust/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /csharp-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "csharp-to-rust" 3 | version = "0.1.0" 4 | authors = ["Wesley Hill "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["cdylib"] 9 | -------------------------------------------------------------------------------- /csharp-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | all: target/debug/libdouble_input 2 | csc src/double.cs 3 | mono ./double.exe 4 | 5 | target/debug/libdouble_input: src/lib.rs Cargo.toml 6 | rustc --crate-name double_input src/lib.rs --crate-type cdylib -o libdouble_input 7 | 8 | clean: 9 | rm -rf target double.exe 10 | -------------------------------------------------------------------------------- /csharp-to-rust/src/double.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace DoubleClass { 5 | 6 | class Double { 7 | [DllImport ("libdouble_input")] 8 | public static extern int double_input (int input); 9 | 10 | 11 | public static void Main (string[] args) { 12 | Console.WriteLine (double_input (2)); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /csharp-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } -------------------------------------------------------------------------------- /dart-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "dart-to-rust" 3 | version = "0.1.0" 4 | authors = ["Zmant i32 { 4 | a + b 5 | } 6 | -------------------------------------------------------------------------------- /dart-to-rust/src/main.dart: -------------------------------------------------------------------------------- 1 | 2 | import 'dart:ffi' as ffi; 3 | 4 | typedef NativeRustAddFunction = ffi.Int32 Function(ffi.Int32, ffi.Int32); 5 | typedef NativeAddFunction = int Function(int, int); 6 | 7 | main() { 8 | ffi.DynamicLibrary dl = ffi.DynamicLibrary.open("target/debug/librust_add.so"); 9 | var add = dl.lookupFunction("rust_add"); 10 | var number = add(12,13); 11 | print("call rust function add(12,13)=$number"); 12 | } -------------------------------------------------------------------------------- /go-to-rust-cgo-dynamic/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "go-to-rust" 3 | version = "0.1.0" 4 | authors = ["Voronkov Andrey "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["dylib"] 9 | -------------------------------------------------------------------------------- /go-to-rust-cgo-dynamic/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) 8 | go run src/double.go 9 | 10 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 11 | cargo build 12 | 13 | clean: 14 | rm -rf target 15 | -------------------------------------------------------------------------------- /go-to-rust-cgo-dynamic/src/double.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | //#cgo LDFLAGS: -L${SRCDIR}/../target/debug -Wl,-rpath=${SRCDIR}/../target/debug -ldouble_input -ldl 4 | //#include 5 | //extern int32_t double_input(int32_t input); 6 | import "C" 7 | import ( 8 | "fmt" 9 | ) 10 | 11 | func main() { 12 | input := 2 13 | double := C.double_input(C.int32_t(input)) 14 | fmt.Printf("%d * 2 = %d\n", input, double) 15 | } 16 | -------------------------------------------------------------------------------- /go-to-rust-cgo-dynamic/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern "C" fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } 5 | -------------------------------------------------------------------------------- /go-to-rust-cgo-static/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "go-to-rust" 3 | version = "0.1.0" 4 | authors = ["Voronkov Andrey "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["staticlib"] 9 | -------------------------------------------------------------------------------- /go-to-rust-cgo-static/Makefile: -------------------------------------------------------------------------------- 1 | all: target/debug/libdouble_input.a 2 | go run src/double.go 3 | 4 | target/debug/libdouble_input.a: src/lib.rs Cargo.toml 5 | cargo build 6 | 7 | clean: 8 | rm -rf target 9 | -------------------------------------------------------------------------------- /go-to-rust-cgo-static/src/double.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | //#cgo LDFLAGS: -L${SRCDIR}/../target/debug -ldouble_input -ldl 4 | //#include 5 | //extern int32_t double_input(int32_t input); 6 | import "C" 7 | import ( 8 | "fmt" 9 | ) 10 | 11 | func main() { 12 | input := 2 13 | double := C.double_input(C.int32_t(input)) 14 | fmt.Printf("%d * 2 = %d\n", input, double) 15 | } 16 | -------------------------------------------------------------------------------- /go-to-rust-cgo-static/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern "C" fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } 5 | -------------------------------------------------------------------------------- /haskell-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "haskell-to-rust" 3 | version = "0.1.0" 4 | authors = ["Bill Murphy "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["dylib"] 9 | -------------------------------------------------------------------------------- /haskell-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) 8 | ghc src/Main.hs target/debug/libdouble_input.$(EXT) -o main 9 | 10 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 11 | cargo build 12 | 13 | clean: 14 | rm -rf target 15 | rm main 16 | rm src/*.o src/*.hi 17 | -------------------------------------------------------------------------------- /haskell-to-rust/src/Main.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE ForeignFunctionInterface #-} 2 | 3 | import Foreign.C 4 | 5 | foreign import ccall "double_input" doubleInput :: CInt -> CInt 6 | 7 | main = do 8 | let input = 4 9 | let output = doubleInput input 10 | putStrLn $ show input ++ " * 2 = " ++ show output 11 | -------------------------------------------------------------------------------- /haskell-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } 5 | -------------------------------------------------------------------------------- /julia-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "julia-to-rust" 3 | version = "0.1.0" 4 | authors = ["timmonfette1 "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["dylib"] 9 | -------------------------------------------------------------------------------- /julia-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) 8 | julia src/main.jl 9 | 10 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 11 | cargo build 12 | 13 | clean: 14 | rm -rf target 15 | -------------------------------------------------------------------------------- /julia-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } 5 | -------------------------------------------------------------------------------- /julia-to-rust/src/main.jl: -------------------------------------------------------------------------------- 1 | input = Int32(10) 2 | output = ccall((:double_input, "target/debug/libdouble_input"), 3 | Int32, (Int32,), input) 4 | 5 | print(input) 6 | print(" * 2 = ") 7 | println(output) 8 | -------------------------------------------------------------------------------- /luajit-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "luajit-to-rust" 3 | version = "0.1.0" 4 | # Modified based on python version 5 | authors = ["Livio Ribeiro "] 6 | 7 | [lib] 8 | name = "double_input" 9 | crate-type = ["dylib"] 10 | -------------------------------------------------------------------------------- /luajit-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) 8 | luajit src/main.lua 9 | 10 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 11 | cargo build 12 | 13 | clean: 14 | rm -rf target 15 | -------------------------------------------------------------------------------- /luajit-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } 5 | -------------------------------------------------------------------------------- /luajit-to-rust/src/main.lua: -------------------------------------------------------------------------------- 1 | local ffi = require('ffi') 2 | 3 | local ext 4 | 5 | if ffi.os == 'Linux' then 6 | ext = 'so' 7 | else 8 | ext = 'dylib' 9 | end 10 | 11 | ffi.cdef[[ 12 | int32_t double_input(int32_t input); 13 | ]] 14 | 15 | local lib = ffi.load('target/debug/libdouble_input.' .. ext) 16 | local double_input = lib.double_input 17 | 18 | local input = 4 19 | local output = double_input(input) 20 | 21 | print(input .. " * 2 = " .. output) 22 | -------------------------------------------------------------------------------- /node-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "node-to-rust" 3 | version = "0.1.0" 4 | authors = ["Alex Crichton "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["dylib"] 9 | -------------------------------------------------------------------------------- /node-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) node_modules/ffi 8 | node src/main.js 9 | 10 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 11 | cargo build 12 | 13 | node_modules/ffi: 14 | npm install ffi 15 | 16 | clean: 17 | rm -rf target 18 | rm -rf node_modules 19 | -------------------------------------------------------------------------------- /node-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } 5 | -------------------------------------------------------------------------------- /node-to-rust/src/main.js: -------------------------------------------------------------------------------- 1 | var ffi = require('ffi'); 2 | 3 | var lib = ffi.Library('target/debug/libdouble_input', { 4 | 'double_input': [ 'int', [ 'int' ] ] 5 | }); 6 | 7 | var input = 4; 8 | var output = lib.double_input(input); 9 | console.log(input + " * 2 = " + output); 10 | -------------------------------------------------------------------------------- /perl-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "perl-to-rust" 3 | version = "0.1.0" 4 | #modified based on python version 5 | authors = ["Hao Wu "] 6 | 7 | [lib] 8 | name = "double_input" 9 | crate-type = ["dylib"] 10 | -------------------------------------------------------------------------------- /perl-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) 8 | perl src/main.pl 9 | 10 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 11 | cargo build 12 | 13 | clean: 14 | rm -rf target 15 | -------------------------------------------------------------------------------- /perl-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } 5 | -------------------------------------------------------------------------------- /perl-to-rust/src/main.pl: -------------------------------------------------------------------------------- 1 | use v5.10; 2 | use FFI::Raw; 3 | 4 | my $double_input = FFI::Raw->new( 5 | "target/debug/libdouble_input.so", 6 | 'double_input', 7 | FFI::Raw::int, # return value 8 | FFI::Raw::int # arg #1 9 | ); 10 | 11 | my $input = 4; 12 | my $output = $double_input->call($input); 13 | say $input . " * 2 = " . $output; 14 | 15 | -------------------------------------------------------------------------------- /php-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "php-to-rust" 3 | version = "0.1.0" 4 | authors = ["Nicolas Far "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["dylib"] 9 | -------------------------------------------------------------------------------- /php-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) 8 | php src/ffi.php 9 | 10 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 11 | cargo build 12 | 13 | clean: 14 | rm -rf target 15 | -------------------------------------------------------------------------------- /php-to-rust/README.md: -------------------------------------------------------------------------------- 1 | PHP 7.4 is needed 2 | 3 | At time of writing, that means [compiling from source](https://github.com/php/php-src#building-php-source-code). 4 | 5 | Make sure to pass --with-ffi as a parameter to ./configure -------------------------------------------------------------------------------- /php-to-rust/src/ffi.php: -------------------------------------------------------------------------------- 1 | double_input(3)); 9 | ?> 10 | -------------------------------------------------------------------------------- /php-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } 5 | 6 | -------------------------------------------------------------------------------- /python-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "python-to-rust" 3 | version = "0.1.0" 4 | authors = ["Alex Crichton "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["dylib"] 9 | -------------------------------------------------------------------------------- /python-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) 8 | python src/main.py 9 | 10 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 11 | cargo build 12 | 13 | clean: 14 | rm -rf target 15 | -------------------------------------------------------------------------------- /python-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } 5 | -------------------------------------------------------------------------------- /python-to-rust/src/main.py: -------------------------------------------------------------------------------- 1 | from ctypes import cdll 2 | from sys import platform 3 | 4 | if platform == 'darwin': 5 | prefix = 'lib' 6 | ext = 'dylib' 7 | elif platform == 'win32': 8 | prefix = '' 9 | ext = 'dll' 10 | else: 11 | prefix = 'lib' 12 | ext = 'so' 13 | 14 | lib = cdll.LoadLibrary('target/debug/{}double_input.{}'.format(prefix, ext)) 15 | double_input = lib.double_input 16 | 17 | input = 4 18 | output = double_input(input) 19 | print('{} * 2 = {}'.format(input, output)) 20 | -------------------------------------------------------------------------------- /ruby-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ruby-to-rust" 3 | version = "0.1.0" 4 | authors = ["Alex Crichton "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["dylib"] 9 | -------------------------------------------------------------------------------- /ruby-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) 8 | ruby src/main.rb 9 | 10 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 11 | cargo build 12 | 13 | clean: 14 | rm -rf target 15 | -------------------------------------------------------------------------------- /ruby-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } 5 | -------------------------------------------------------------------------------- /ruby-to-rust/src/main.rb: -------------------------------------------------------------------------------- 1 | require 'ffi' 2 | 3 | module Hello 4 | extend FFI::Library 5 | ffi_lib 'target/debug/libdouble_input.' + FFI::Platform::LIBSUFFIX 6 | attach_function :double_input, [ :int ], :int 7 | end 8 | 9 | input = 4 10 | output = Hello.double_input(input) 11 | puts "#{input} * 2 = #{output}" 12 | -------------------------------------------------------------------------------- /rust-to-c/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-to-c" 3 | version = "0.1.0" 4 | authors = ["Alex Crichton "] 5 | build = "build.rs" 6 | 7 | [dependencies] 8 | libc = "0.2" 9 | 10 | [build-dependencies] 11 | cc = "1.0" 12 | -------------------------------------------------------------------------------- /rust-to-c/build.rs: -------------------------------------------------------------------------------- 1 | extern crate cc; 2 | 3 | fn main() { 4 | cc::Build::new() 5 | .file("src/double.c") 6 | .compile("libdouble.a"); 7 | } 8 | -------------------------------------------------------------------------------- /rust-to-c/src/double.c: -------------------------------------------------------------------------------- 1 | int double_input(int input) { 2 | return input * 2; 3 | } 4 | -------------------------------------------------------------------------------- /rust-to-c/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate libc; 2 | 3 | extern { 4 | fn double_input(input: libc::c_int) -> libc::c_int; 5 | } 6 | 7 | fn main() { 8 | let input = 4; 9 | let output = unsafe { double_input(input) }; 10 | println!("{} * 2 = {}", input, output); 11 | } 12 | -------------------------------------------------------------------------------- /rust-to-cmake/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-to-cmake" 3 | version = "0.1.0" 4 | authors = ["Jörn-Michael Miehe "] 5 | build = "build.rs" 6 | 7 | [dependencies] 8 | libc = "0.2" 9 | 10 | [build-dependencies] 11 | cmake = "0.1.41" 12 | -------------------------------------------------------------------------------- /rust-to-cmake/build.rs: -------------------------------------------------------------------------------- 1 | extern crate cmake; 2 | 3 | fn main() { 4 | // Builds the project in the directory located in `libdouble`, installing it 5 | // into $OUT_DIR 6 | let dst = cmake::build("libdouble"); 7 | 8 | println!("cargo:rustc-link-search=native={}", dst.display()); 9 | println!("cargo:rustc-link-lib=static=double"); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /rust-to-cmake/libdouble/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library ( 2 | double 3 | STATIC 4 | double.c 5 | ) 6 | 7 | install (TARGETS double DESTINATION .) -------------------------------------------------------------------------------- /rust-to-cmake/libdouble/double.c: -------------------------------------------------------------------------------- 1 | int double_input(int input) { 2 | return input * 2; 3 | } 4 | -------------------------------------------------------------------------------- /rust-to-cmake/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate libc; 2 | 3 | extern { 4 | fn double_input(input: libc::c_int) -> libc::c_int; 5 | } 6 | 7 | fn main() { 8 | let input = 4; 9 | let output = unsafe { double_input(input) }; 10 | println!("{} * 2 = {}", input, output); 11 | } 12 | -------------------------------------------------------------------------------- /rust-to-cpp/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-to-cpp" 3 | version = "0.1.0" 4 | authors = ["Petr Kozelka "] 5 | build = "build.rs" 6 | 7 | [dependencies] 8 | libc = "0.2" 9 | 10 | [build-dependencies] 11 | cc = "1.0" -------------------------------------------------------------------------------- /rust-to-cpp/build.rs: -------------------------------------------------------------------------------- 1 | extern crate cc; 2 | 3 | fn main() { 4 | cc::Build::new() 5 | .file("src/triple.cpp") 6 | .cpp(true) 7 | .compile("libtriple.a"); 8 | } 9 | -------------------------------------------------------------------------------- /rust-to-cpp/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate libc; 2 | 3 | extern { 4 | fn triple_input(input: libc::c_int) -> libc::c_int; 5 | } 6 | 7 | fn main() { 8 | let input = 4; 9 | let output = unsafe { triple_input(input) }; 10 | println!("{} * 3 = {}", input, output); 11 | } 12 | -------------------------------------------------------------------------------- /rust-to-cpp/src/triple.cpp: -------------------------------------------------------------------------------- 1 | extern "C" 2 | int triple_input(int input) { 3 | return input * 3; 4 | } 5 | -------------------------------------------------------------------------------- /rust-to-fortran/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-to-fortran" 3 | version = "0.1.0" 4 | build = "build.rs" 5 | 6 | [dependencies] 7 | libc = "0.2" 8 | 9 | [build-dependencies] 10 | cc = "1.0" 11 | -------------------------------------------------------------------------------- /rust-to-fortran/build.rs: -------------------------------------------------------------------------------- 1 | extern crate cc; 2 | 3 | fn main() { 4 | cc::Build::new() 5 | .file("src/double.f90") 6 | .compile("libdouble.a"); 7 | } 8 | -------------------------------------------------------------------------------- /rust-to-fortran/src/double.f90: -------------------------------------------------------------------------------- 1 | module wrapper 2 | 3 | implicit none 4 | private 5 | public double_input 6 | 7 | contains 8 | 9 | function double_input(input) 10 | implicit none 11 | integer :: input, double_input 12 | 13 | double_input = 2*input 14 | end function 15 | 16 | end module wrapper 17 | -------------------------------------------------------------------------------- /rust-to-fortran/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate libc; 2 | 3 | // Consider using the `nm` utility on compiled Fortran files to get the function names 4 | 5 | extern { 6 | fn __wrapper_MOD_double_input(input: &libc::c_int) -> libc::c_int; 7 | } 8 | 9 | fn main() { 10 | let input = 4; 11 | let output = unsafe { __wrapper_MOD_double_input(&input) }; 12 | println!("{} * 2 = {}", input, output); 13 | } 14 | -------------------------------------------------------------------------------- /rust-to-go-dynamic/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-to-go" 3 | version = "0.1.0" 4 | authors = ["Andrey Voronkov "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | libc = "0.2" 11 | -------------------------------------------------------------------------------- /rust-to-go-dynamic/build.rs: -------------------------------------------------------------------------------- 1 | use std::process::Command; 2 | 3 | fn main() { 4 | println!(r"cargo:rustc-link-search=target/debug"); 5 | let os = Command::new("uname") 6 | .output().unwrap(); 7 | let ext = match String::from_utf8_lossy(os.stdout.as_slice()).into_owned().trim_end().as_ref() { 8 | "Darwin" => "dylib", 9 | _ => "so" 10 | }; 11 | Command::new("go").args(&["build", "-o", &format!("target/debug/libdouble_input.{}", ext), "-buildmode=c-shared", "src/double.go"]) 12 | .status().unwrap(); 13 | } 14 | -------------------------------------------------------------------------------- /rust-to-go-dynamic/src/double.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "C" 4 | 5 | //export DoubleInput 6 | func DoubleInput(input int32) int32 { 7 | return input * 2 8 | } 9 | 10 | func main() {} 11 | -------------------------------------------------------------------------------- /rust-to-go-dynamic/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate libc; 2 | 3 | #[link(name = "double_input")] 4 | extern { 5 | fn DoubleInput(input: libc::c_int) -> libc::c_int; 6 | } 7 | 8 | fn main() { 9 | let input = 2; 10 | let output = unsafe { DoubleInput(input) }; 11 | println!("{} * 2 = {}", input, output); 12 | } 13 | -------------------------------------------------------------------------------- /swift-to-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "swift-to-rust" 3 | version = "0.1.0" 4 | authors = ["Calvin Hill "] 5 | 6 | [lib] 7 | name = "double_input" 8 | crate-type = ["dylib"] -------------------------------------------------------------------------------- /swift-to-rust/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname),Darwin) 2 | EXT := dylib 3 | else 4 | EXT := so 5 | endif 6 | 7 | all: target/debug/libdouble_input.$(EXT) 8 | swiftc src/main.swift -import-objc-header src/double.h -L./target/debug -ldouble_input -o double 9 | LD_LIBRARY_PATH=./target/debug ./double 10 | 11 | target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml 12 | cargo build 13 | 14 | clean: 15 | rm -rf target double -------------------------------------------------------------------------------- /swift-to-rust/src/double.h: -------------------------------------------------------------------------------- 1 | int double_input(int input); -------------------------------------------------------------------------------- /swift-to-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern fn double_input(input: i32) -> i32 { 3 | input * 2 4 | } -------------------------------------------------------------------------------- /swift-to-rust/src/main.swift: -------------------------------------------------------------------------------- 1 | print(double_input(2)) --------------------------------------------------------------------------------