< 返回版块

2019-05-15 11:26    责任编辑:jay

标签:rust,Julia,Try

Interaction Rust with Julia

Rust 和 Julia 的交互

Hasan Yousef's photo

Hasan Yousef

Published 5 months ago

RustJulia

Rust 和 Julia 都快速发展着也同样致力于效率的提高,两者之间的结合一定会有高回报。

让我们来尝试从 Rust调用下面的 Julia 代码:

# __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")

可以采用下面rust调用:

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);
        }
    }
}

采用 "cargo run" 来执行:

From the other other hand, let's say we have the below rust file: 从 Julia 调用下面的 Rust 代码:

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

首先定义cargo文件来并且运行“caogo buiild”:

[package]
name = "julia_call_rust"
version = "1.0.0"
authors = ["hasan yousef]

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

下面是调用rust库的Julia:

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")

好了, 先执行"cargo build" 之后运行 "julia mainl.jl" 能看到什么。


Interaction Rust with Julia

Hasan Yousef's photo

Hasan Yousef

Published 5 months ago

RustJulia

Rust and Julia are two promising languages focusing on speed, integrating them will help achieving great results.

Let's say we have the below Julia code, and we want to cal it from Rust code:

# __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")

Then we can call it using the below:

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, the rust file can be executed by running cargo run:

From the other other hand, let's say we have the below rust file:

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

First we need to define proper cargo file to create the library, and calling cargo build:

[package]
name = "julia_call_rust"
version = "1.0.0"
authors = ["hasan yousef]

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

Then the the functions in this lib can be called from Julia code as:

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.