# Rust-lang interaction

**URL:** <https://discourse.julialang.org/t/rust-lang-interaction/13711>\
**Category:** New to Julia\
**Tags:** question, rust\
**Created:** [August 19, 2018, 1:20pm UTC](https://discourse.julialang.org/t/rust-lang-interaction/13711 "2018-08-19T13:20:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [August 19, 2018, 1:20pm UTC](https://discourse.julialang.org/t/rust-lang-interaction/13711/1 "2018-08-19T13:20:16Z")

</div>

Hi…  
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

Thanks

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 19, 2018, 1:26pm UTC](https://discourse.julialang.org/t/rust-lang-interaction/13711/2 "2018-08-19T13:26:05Z")

</div>

There was some abandoned work in this direction:

> **[GitHub - iamed2/Rust.jl: Write Rust code in Julia](https://github.com/iamed2/Rust.jl)**
>
> Write Rust code in Julia. Contribute to iamed2/Rust.jl development by creating an account on GitHub.

And previous discussion:

> [@Interfacing rust?](https://discourse.julialang.org/t/interfacing-rust/6389/5):
>
> Yeah, but I gave up when I realized that LambdaInfo was removed in 0.6 and wasn’t clear what the work around should be.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [August 19, 2018, 5:54pm UTC](https://discourse.julialang.org/t/rust-lang-interaction/13711/3 "2018-08-19T17:54:35Z")

</div>

That package of Eric’s looks pretty cool, I’ve been wanting to see about Rust for some time now.  
Maybe that package should be resurrected.

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [August 19, 2018, 6:24pm UTC](https://discourse.julialang.org/t/rust-lang-interaction/13711/4 "2018-08-19T18:24:20Z")

</div>

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

**Executing `Julia` from `Rust`**

Use the [Rust Command](https://doc.rust-lang.org/std/process/struct.Command.html) as below:

Create simple `main.jl` file, that contains:

```
# __precompile__ () # If required to be kept precompiled for faster execution
# name = isempty(ARGS) ? "name" : ARGS[1] # To check input arguments
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.arg("main.jl");
    // 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:

**Executing `Rust` from `Julia`**

Use the [calling-c-and-fortran-code](https://docs.julialang.org/en/v0.6.1/manual/calling-c-and-fortran-code/) by [ccall](https://docs.julialang.org/en/v0.6.1/stdlib/c/#ccall)

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

```
#[no_mangle]
pub extern fn double_input(input: i32) -> i32 {
    println!("Hello from Rust");
    input * 2
}

```

The `Cargo.toml` for building the library, is:

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

[lib]
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)
                (:double_input,
                "target/debug/libmy_rust_lib"),
                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:

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 19, 2018, 7:53pm UTC](https://discourse.julialang.org/t/rust-lang-interaction/13711/5 "2018-08-19T19:53:14Z")

</div>

(once more, two ways please)  
This is the first I have seen where Julia and Rust get together with a light touch. Thank you for sharing it with the Julia Community via Discourse; this let’s other bringers of good programming see and utilize it. It would be of additional help to have each way (use Rust from within Julia, use Julia through Rust) separately described using an example with numeric values or a struct in addition to the nice writeup you already posted. Cudos.

---

<div class="post-metadata">

**Author:** ![felipenoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipenoris/32/553_2.png) [@felipenoris](https://discourse.julialang.org/u/felipenoris)\
**Post date:** [September 25, 2018, 2:12pm UTC](https://discourse.julialang.org/t/rust-lang-interaction/13711/6 "2018-09-25T14:12:54Z")

</div>

Please, check out: [[ANN] Embedding Rust Library in a Julia Package](https://discourse.julialang.org/t/ann-rust-library-embedded-in-a-julia-package/15470)
