Julia-lang interaction

Is there any way to interact between Julia and Rust, a way that allow:

  1. Calling Rust function from Julia, like ccall
  2. Calling Julia from Rust


I found the below way to solve it, and liked to share with the community:

Executing Julia from Rust

Use the Rust Command as below:

Create simple main.jl file, that contains:

`println("hello from Julia function")`

Create simple main.rs file, that contains:

use std::process::Command;

fn main() {
    println!("Hello from Rust");
    let mut cmd = Command::new("Julia");
    // cmd.args(&["main.jl", "arg1", "arg2"]);
    match cmd.output(){
                Ok(o) => unsafe {
                        println!("Output: {}", String::from_utf8_unchecked(o.stdout));
                Err(e) => {
                    println!("There was an error {}", e);

Then, by running cargo run you'll get the required output s below:

enter image description here

Executing Rust from Julia

Use the calling-c-and-fortran-code by ccall

Create rust shared library, simple lib.rs file, that contains:

pub extern fn double_input(input: i32) -> i32 {
    println!("Hello from Rust");
    input * 2

The Cargo.toml for building the library, is:

name = "julia_call_rust"
version = "0.1.0"
authors = ["hasan yousef]

name = "my_rust_lib"
crate-type = ["dylib"]

Create simple main.jl file, that contains:

println("Hello from Julia")
input = 10 #Int32(10)
output =  ccall(   #(:function or "function", "library"), Return type, (Input types,), arguments if any)
                Int32,          # Return type
                (Int32,),       # (Input types,)
                input)          # Arguments if any
println("As result of $input * 2 is: $output")

Then, by running cargo build to get the rust library built, and by running julia main.jl you'll get the required output s below:

enter image description here


Nice. You gonna link this post on the Julia discourse...? Or I'll do it for you since I'm there alot...?

1 Like

You want to use crate-type = ["cdylib"]

Done, I asked the same, and posted same answer,thanks :slight_smile:

1 Like

Please, check out [ANN] Embedding Rust Libraries in a Julia Package.