# __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:
[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:
(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.
Hi…
Is there any way to interact between Julia and Rust, a way that allow:
ccall
Thanks
There was some abandoned work in this direction:
And previous discussion:
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.
I found the below way to solve it, and liked to share with the community:
Executing
Julia
fromRust
Use the Rust Command 34 as below:
Create simple
main.jl
file, that contains:Create simple
main.rs
file, that contains:Then, by running
cargo run
you’ll get the required output s below:40
Executing
Rust
fromJulia
Use the calling-c-and-fortran-code 8 by ccall 6
Create rust shared library, simple
lib.rs
file, that contains:The
Cargo.toml
for building the library, is:Create simple
main.jl
file, that contains:Then, by running
cargo build
to get the rust library built, and by runningjulia main.jl
you’ll get the required output s below:19
(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.