Examples
A cookbook of small, complete examples. Each one shows the Rust and the Julia side together, and all of them except the few marked otherwise are run when this documentation is built, so the outputs are real.
The examples use #[julia], which lets you write ordinary Rust with ordinary Rust types. If you have not read the Tutorial yet, start there; if a piece of Rust syntax is unfamiliar, see Rust for Julia Users.
Numerical kernels
A loop
rust"""
#[julia]
fn power(x: f64, n: u32) -> f64 {
let mut result = 1.0;
for _ in 0..n {
result *= x;
}
result
}
"""
power(2.0, 10)1024.0for _ in 0..n runs n times; _ is Rust's "I do not need this variable".
Fibonacci
rust"""
#[julia]
fn fibonacci(n: u32) -> u64 {
if n <= 1 {
return n as u64;
}
let (mut a, mut b) = (0u64, 1u64);
for _ in 2..=n {
(a, b) = (b, a + b);
}
b
}
"""
Int.(fibonacci.(0:10))11-element Vector{Int64}:
0
1
1
2
3
5
8
13
21
34
550u64 is a literal with an explicit type, and n as u64 is an explicit conversion: Rust never converts between numeric types on its own. The Julia side broadcasts over the generated function like over any other.
Statistics over an array
A function can take a Julia array as a slice (&[f64]), without a copy. To give several values back, return a #[julia] struct and read its fields:
rust"""
#[julia]
pub struct Summary {
pub mean: f64,
pub variance: f64,
pub min: f64,
pub max: f64,
}
impl Summary {
pub fn new(xs: &[f64]) -> Self {
let n = xs.len() as f64;
let mean = xs.iter().sum::<f64>() / n;
let variance = xs.iter().map(|x| (x - mean) * (x - mean)).sum::<f64>() / n;
let min = xs.iter().copied().fold(f64::INFINITY, f64::min);
let max = xs.iter().copied().fold(f64::NEG_INFINITY, f64::max);
Self { mean, variance, min, max }
}
}
"""
s = Summary([1.0, 2.0, 3.0, 4.0, 5.0])
(mean = s.mean, std = sqrt(s.variance), min = s.min, max = s.max)(mean = 3.0, std = 1.4142135623730951, min = 1.0, max = 5.0)xs.iter() is an iterator, .map(|x| …) applies a closure (Julia's x -> …), and .sum::<f64>() names the result type explicitly, which Rust needs here. .fold(init, f) is Julia's foldl(f, xs; init).
Strings
&str is a borrowed UTF-8 string and String an owned one. Both are a Julia String on the Julia side.
Searching
rust"""
#[julia]
fn find_substring(haystack: &str, needle: &str) -> Option<usize> {
haystack.find(needle)
}
#[julia]
fn count_words(text: &str) -> usize {
text.split_whitespace().count()
}
"""
found = find_substring("hello world", "world")
missing = find_substring("hello world", "xyz")
RustCall.unwrap(found), RustCall.is_none(missing), count_words("The quick brown fox")(0x0000000000000006, true, 0x0000000000000004)str::find returns an Option: Some(position) or None, where Julia's findfirst returns a range or nothing. The position is a zero-based byte offset, so 6 here is Julia's index 7.
Returning a new string
rust"""
#[julia]
fn reverse_chars(s: &str) -> String {
s.chars().rev().collect()
}
#[julia]
fn shout(s: &str) -> String {
format!("{}!", s.to_uppercase())
}
"""
reverse_chars("こんにちは"), shout("hello")("はちにんこ", "HELLO!").chars() iterates over Unicode characters, not bytes, so the reversal is correct for non-ASCII text. .collect() gathers an iterator into the declared return type, here a String.
Validating input
rust"""
#[julia]
fn is_ipv4(text: &str) -> bool {
let parts: Vec<&str> = text.split('.').collect();
parts.len() == 4 && parts.iter().all(|part| part.parse::<u8>().is_ok())
}
"""
is_ipv4("192.168.1.1"), is_ipv4("999.1.1.1"), is_ipv4("1.2.3")(true, false, false)part.parse::<u8>() tries to read the text as an 8-bit unsigned integer and returns a Result, so "is a number from 0 to 255" is one expression. Vec<&str> is Rust's growable array; it is used freely inside a function, it just cannot be an argument or return type at the boundary.
Arrays
Reading
rust"""
#[julia]
fn sum_wide(xs: &[i32]) -> i64 {
xs.iter().map(|&x| x as i64).sum()
}
#[julia]
fn max_element(xs: &[i32]) -> Option<i32> {
xs.iter().copied().max()
}
"""
arr = Int32[1, 5, 3, 9, 2]
sum_wide(arr), RustCall.unwrap(max_element(arr)), RustCall.is_none(max_element(Int32[]))(20, 9, true)The maximum of an empty slice does not exist, and the Rust type says so: max() returns an Option. Summing into an i64 avoids overflowing i32.
Modifying in place
&mut [T] is a writable view of the Julia array:
rust"""
#[julia]
fn sort_in_place(xs: &mut [i32]) {
xs.sort();
}
#[julia]
fn normalize(xs: &mut [f64]) -> Result<(), String> {
let norm = xs.iter().map(|x| x * x).sum::<f64>().sqrt();
if norm == 0.0 {
return Err("cannot normalize a zero vector".to_string());
}
for x in xs.iter_mut() {
*x /= norm;
}
Ok(())
}
"""
a = Int32[5, 2, 8, 1, 9]
sort_in_place(a)
v = [3.0, 4.0]
normalize(v)
a, v(Int32[1, 2, 5, 8, 9], [0.6, 0.8])Result<(), String> is the type of "nothing to return, but it can fail". On failure the array is left untouched and Julia gets the message:
RustCall.result_to_exception(normalize([3.0, 4.0])) # Ok: returns nothing
r = normalize(zeros(3))
RustCall.is_err(r), r.value(true, "cannot normalize a zero vector")The element type must match exactly (Vector{Int32} for &[i32]); see Julia arrays as Rust slices for views, matrices and the aliasing rules.
Bytes
A hash function
rust"""
#[julia]
fn fnv1a(data: &[u8]) -> u64 {
let mut hash: u64 = 0xcbf29ce484222325;
for byte in data {
hash ^= *byte as u64;
hash = hash.wrapping_mul(0x100000001b3);
}
hash
}
"""
function fnv1a_julia(data::Vector{UInt8})
hash = 0xcbf29ce484222325
for byte in data
hash = (hash ⊻ byte) * 0x100000001b3
end
return hash
end
data = Vector{UInt8}("hello world")
fnv1a(data) == fnv1a_julia(data)trueJulia's integer arithmetic wraps on overflow silently. Rust makes you say so: wrapping_mul is the wrapping multiplication, and a plain * that overflows is treated as a bug.
Writing into a buffer the caller owns
A Rust function cannot hand a freshly allocated Vec<u8> to Julia directly. The usual pattern is that Julia allocates the output and Rust fills it and reports how much it used. Here is run-length encoding:
rust"""
#[julia]
fn rle_encode(input: &[u8], output: &mut [u8]) -> Result<usize, String> {
let mut written = 0;
let mut i = 0;
while i < input.len() {
let byte = input[i];
let mut run = 1;
while i + run < input.len() && input[i + run] == byte && run < 255 {
run += 1;
}
if written + 2 > output.len() {
return Err(format!("output buffer too small ({} bytes)", output.len()));
}
output[written] = run as u8;
output[written + 1] = byte;
written += 2;
i += run;
}
Ok(written)
}
"""
function rle(data::Vector{UInt8})
output = zeros(UInt8, 2 * length(data)) # worst case
n = RustCall.result_to_exception(rle_encode(data, output))
return resize!(output, n)
end
rle(Vector{UInt8}("aaabbbbcd"))8-element Vector{UInt8}:
0x03
0x61
0x04
0x62
0x01
0x63
0x01
0x64Indexing a slice (input[i]) is bounds-checked in Rust as in Julia. An out-of-range index is a panic, which RustCall turns into a RustCall.RustPanicError rather than a crash.
Objects that keep state
When Rust needs to own growing data, keep it inside a #[julia] struct. Julia holds a handle; the Vec never crosses the boundary.
rust"""
#[julia]
pub struct Recorder {
samples: Vec<f64>,
}
impl Recorder {
pub fn new() -> Self {
Self { samples: Vec::new() }
}
pub fn record(&mut self, value: f64) {
self.samples.push(value);
}
pub fn record_all(&mut self, values: &[f64]) {
self.samples.extend_from_slice(values);
}
pub fn count(&self) -> usize {
self.samples.len()
}
pub fn average(&self) -> Option<f64> {
if self.samples.is_empty() {
None
} else {
Some(self.samples.iter().sum::<f64>() / self.samples.len() as f64)
}
}
pub fn copy_into(&self, out: &mut [f64]) -> Result<(), String> {
if out.len() != self.samples.len() {
return Err(format!("need a buffer of length {}", self.samples.len()));
}
out.copy_from_slice(&self.samples);
Ok(())
}
}
"""
rec = Recorder()
record(rec, 1.5)
record_all(rec, [2.5, 3.5, 4.5])
out = zeros(Int(count(rec)))
copy_into(rec, out)
Int(count(rec)), RustCall.unwrap(average(rec)), out(4, 3.0, [1.5, 2.5, 3.5, 4.5])&mut self methods change the object, &self methods only read it. The Rust value is dropped automatically when the Julia object is garbage collected. count here is a new function in this module, not Base.count; see the note on method names in the Tutorial.
Errors with structure
Returning Result<T, String> gives Julia a message. Parsing and validation code reads naturally with the ? operator, which returns early on an error:
rust"""
#[julia]
fn parse_endpoint(text: &str) -> Result<u16, String> {
let (host, port) = text
.split_once(':')
.ok_or_else(|| format!("`{}` has no `:port`", text))?;
if host.is_empty() {
return Err("empty host".to_string());
}
let port: u16 = port
.parse()
.map_err(|e| format!("bad port `{}`: {}", port, e))?;
Ok(port)
}
"""
results = parse_endpoint.(["localhost:8080", "localhost", "localhost:http", ":80"])
[RustCall.is_ok(r) ? Int(RustCall.unwrap(r)) : r.value for r in results]4-element Vector{Any}:
8080
"`localhost` has no `:port`"
"bad port `http`: invalid digit found in string"
"empty host"Image processing
An image is a large, flat buffer of bytes, which is the ideal shape for a Rust kernel: Julia keeps the image types and the plotting, Rust does the per-pixel loop in place. This example uses Images.jl for display.
To run it locally, install Images first:
using Pkg
Pkg.add("Images")using RustCall
using Images
# Rust converts the buffer in place. `&mut [u8]` is the Julia vector's own memory.
rust"""
#[julia]
fn grayscale_image(pixels: &mut [u8]) {
// Interleaved RGB: three bytes per pixel.
for rgb in pixels.chunks_exact_mut(3) {
let r = rgb[0] as f32;
let g = rgb[1] as f32;
let b = rgb[2] as f32;
// Standard luminance formula (ITU-R BT.601)
let gray = (0.299 * r + 0.587 * g + 0.114 * b) as u8;
rgb[0] = gray;
rgb[1] = gray;
rgb[2] = gray;
}
}
"""
# Create a sample RGB image (gradient with colors)
function create_sample_image(width, height)
img = zeros(RGB{N0f8}, height, width)
for y in 1:height, x in 1:width
r = (x - 1) / (width - 1) # Red increases left to right
g = (y - 1) / (height - 1) # Green increases top to bottom
b = 0.5 # Constant blue
img[y, x] = RGB{N0f8}(r, g, b)
end
return img
end
# Convert Julia image to raw RGB bytes (row-major, interleaved RGB)
function image_to_bytes(img)
h, w = size(img)
pixels = Vector{UInt8}(undef, h * w * 3)
idx = 1
for y in 1:h, x in 1:w
pixel = img[y, x]
pixels[idx] = reinterpret(UInt8, red(pixel))
pixels[idx + 1] = reinterpret(UInt8, green(pixel))
pixels[idx + 2] = reinterpret(UInt8, blue(pixel))
idx += 3
end
return pixels
end
# Convert raw RGB bytes back to Julia image
function bytes_to_image(pixels, width, height)
img = zeros(RGB{N0f8}, height, width)
idx = 1
for y in 1:height, x in 1:width
r = reinterpret(N0f8, pixels[idx])
g = reinterpret(N0f8, pixels[idx + 1])
b = reinterpret(N0f8, pixels[idx + 2])
img[y, x] = RGB{N0f8}(r, g, b)
idx += 3
end
return img
end
# Create sample image
width, height = 256, 256
original_img = create_sample_image(width, height)ERROR: LoadError: InitError: could not load library "/home/runner/.julia/artifacts/288f492925988fedea29024bd6d31a41952bfeca/lib/libMagickWand-7.Q16HDRI.so"
libgs.so.9: cannot open shared object file: No such file or directory
Stacktrace:
[1] #dlopen#3
@ ./libdl.jl:120 [inlined]
[2] dlopen(s::String, flags::UInt32)
@ Base.Libc.Libdl ./libdl.jl:119
[3] macro expansion
@ ~/.julia/packages/JLLWrappers/Kp7TC/src/products/library_generators.jl:63 [inlined]
[4] __init__()
@ ImageMagick_jll ~/.julia/packages/ImageMagick_jll/exF23/src/wrappers/x86_64-linux-gnu-cxx11.jl:18
[5] run_module_init(mod::Module, i::Int64, init_reports::Nothing)
@ Base ./loading.jl:1495
[6] register_restored_modules(sv::Core.SimpleVector, pkg::Base.PkgId, path::String; init_reports::Nothing)
@ Base ./loading.jl:1482
[7] register_restored_modules
@ ./loading.jl:1458 [inlined]
[8] _include_from_serialized(pkg::Base.PkgId, path::String, ocachepath::String, depmods::Vector{Any}; register::Bool)
@ Base ./loading.jl:1367
[9] _include_from_serialized
@ ./loading.jl:1321 [inlined]
[10] _require_search_from_serialized(pkg::Base.PkgId, sourcepath::String, build_id::UInt128, stalecheck::Bool; reasons::Dict{String, Int64}, DEPOT_PATH::Vector{String})
@ Base ./loading.jl:2236
[11] _require_search_from_serialized
@ ./loading.jl:2122 [inlined]
[12] __require_prelocked(pkg::Base.PkgId, env::String)
@ Base ./loading.jl:2800
[13] _require_prelocked(uuidkey::Base.PkgId, env::String)
@ Base ./loading.jl:2660
[14] macro expansion
@ ./loading.jl:2578 [inlined]
[15] macro expansion
@ ./lock.jl:376 [inlined]
[16] __require(into::Module, mod::Symbol)
@ Base ./loading.jl:2542
[17] require
@ ./loading.jl:2518 [inlined]
[18] eval_import_path
@ ./module.jl:36 [inlined]
[19] eval_import_path_all(at::Module, path::Expr, keyword::String)
@ Base ./module.jl:60
[20] _eval_using(to::Module, path::Expr)
@ Base ./module.jl:137
[21] top-level scope
@ ~/.julia/packages/ImageMagick/KkM1b/src/ImageMagick.jl:6
[22] include(mod::Module, _path::String)
@ Base ./Base.jl:309
[23] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt128}}, source::Nothing, preresolved::Vector{Pair{Base.PkgId, String}})
@ Base ./loading.jl:3256
[24] top-level scope
@ stdin:5
[25] eval(m::Module, e::Any)
@ Core ./boot.jl:489
[26] include_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String)
@ Base ./loading.jl:3091
[27] include_string
@ ./loading.jl:3101 [inlined]
[28] exec_options(opts::Base.JLOptions)
@ Base ./client.jl:342
[29] _start()
@ Base ./client.jl:577
during initialization of module ImageMagick_jll
in expression starting at /home/runner/.julia/packages/ImageMagick/KkM1b/src/ImageMagick.jl:1
in expression starting at stdin:5Original Image (Color Gradient):
original_imgNow let's convert it to grayscale using Rust:
# Process the image with Rust
pixels = image_to_bytes(original_img)
grayscale_image(pixels)
grayscale_img = bytes_to_image(pixels, width, height)Grayscale Image (Processed by Rust):
grayscale_img
Let's verify the grayscale conversion worked correctly:
# Check that R, G, B are equal (grayscale property)
sample_pixel = grayscale_img[128, 128]
println("Sample pixel at (128, 128):")
println(" R = $(red(sample_pixel))")
println(" G = $(green(sample_pixel))")
println(" B = $(blue(sample_pixel))")
println(" Grayscale verified: ", red(sample_pixel) == green(sample_pixel) == blue(sample_pixel))Sample pixel at (128, 128):
R = 0.498N0f8
G = 0.498N0f8
B = 0.498N0f8
Grayscale verified: trueSide-by-side comparison:
# Create a side-by-side comparison image
comparison = [original_img grayscale_img]
For servedocs(), make sure to run it from the docs environment:
julia --project=docs -e 'using Pkg; Pkg.instantiate()'
julia --project=docs -e 'using LiveServer; servedocs()'Using crates from crates.io
Not run in this build: it downloads from the network.
A block can depend on published crates. Name them in a // cargo-deps: comment and RustCall builds the block with Cargo:
rust"""
// cargo-deps: regex = "1"
use regex::Regex;
#[julia]
fn count_matches(pattern: &str, text: &str) -> Result<usize, String> {
let re = Regex::new(pattern).map_err(|e| e.to_string())?;
Ok(re.find_iter(text).count())
}
"""
RustCall.unwrap(count_matches(raw"\d+", "a1 b22 c333")) # => 3Dependencies use Cargo's own syntax, so features and several crates work too:
// cargo-deps: serde = { version = "1.0", features = ["derive"] }, serde_json = "1.0"or, for a longer list, a manifest section in a documentation comment:
//! ```cargo
//! [dependencies]
//! rand = "0.8"
//! ndarray = "0.15"
//! ```The first build downloads and compiles the dependencies and can take a minute. After that the compiled library is cached.
The same thing by hand: extern "C" and @rust
Everything above can be written without #[julia], in the classic C-ABI style. You then pass raw pointers and lengths yourself, and the Rust side rebuilds the slice in unsafe code:
rust"""
#[no_mangle]
pub extern "C" fn sum_array(data: *const i32, len: usize) -> i64 {
let slice = unsafe { std::slice::from_raw_parts(data, len) };
slice.iter().map(|&x| x as i64).sum()
}
"""
function sum_by_hand(arr::Vector{Int32})
GC.@preserve arr begin
@rust sum_array(pointer(arr), UInt(length(arr)))::Int64
end
end
sum_by_hand(Int32[1, 5, 3, 9, 2])20Compared with sum_wide above, you are now responsible for three things that #[julia] did for you:
GC.@preserve.pointer(arr)is a bare address. Without theGC.@preserveblock Julia may free or movearrwhile Rust is reading it.- Exact types.
UInt(length(arr))must matchusize; nothing is converted. - Panics. A panic in a hand-written
extern "C"function aborts the Julia process.
Use this style to bind existing C-style Rust, and prefer #[julia] for new code.
Measuring performance
Not run in this build.
using BenchmarkTools
rust"""
#[julia]
fn sum_of_squares(xs: &[f64]) -> f64 {
xs.iter().map(|x| x * x).sum()
}
"""
julia_sum_of_squares(xs) = sum(x -> x * x, xs)
data = rand(10_000)
@btime julia_sum_of_squares($data)
@btime sum_of_squares($data)Three things to keep in mind when you read such numbers:
- Julia is already fast. For a simple numeric loop, well-typed Julia and Rust compile to similar machine code. Expect parity, not a miracle. Rust earns its place through libraries you do not want to rewrite, predictable memory use, and code whose safety you want the compiler to check.
- A call has a fixed cost. Crossing the boundary costs a few nanoseconds for a
#[julia]function. That is negligible for a function that does real work and dominant for one that adds two numbers. Move the loop to the Rust side and pass the whole array, rather than calling Rust once per element. - Annotate
@rustcalls.@rust f(x)::Tis a direct call;@rust f(x)without the annotation is resolved dynamically and is far slower.
The Performance Guide has measurements and details.
Ownership types
Not run in this build: these need the helper library built by Pkg.build("RustCall").
RustCall can also hold standalone Rust smart pointers and vectors. Most code never needs them, since #[julia] structs cover the common case, but they are useful for experimenting with Rust's ownership model from the REPL:
using RustCall
# RustBox: one owner, freed once.
box = RustCall.RustBox(Int32(42))
RustCall.is_valid(box) # => true
RustCall.drop!(box)
RustCall.is_dropped(box) # => true
# RustRc / RustArc: shared ownership by reference counting.
rc1 = RustCall.RustRc(Int32(100))
rc2 = RustCall.clone(rc1) # a second owner of the same value
RustCall.drop!(rc1)
RustCall.is_valid(rc2) # => true: the value lives until the last owner goes
RustCall.drop!(rc2)
# RustVec: a growable array whose memory Rust manages.
vec = RustCall.create_rust_vec(Int32[1, 2, 3])
vec[1] = 42
collect(vec) # => Int32[42, 2, 3]
RustCall.drop!(vec)RustArc is the thread-safe variant of RustRc. See Types, memory and ownership for the full API.
Checklist
- Prefer
#[julia]functions with&str,String,&[T],&mut [T],ResultandOptionin their signatures. They are checked and converted for you. - Pass whole arrays and loop in Rust. Do not call across the boundary per element.
- Return
Resultfor failures a caller should handle. Leave panics for bugs. - To return several values, return a
#[julia]struct. To return a large buffer, let Julia allocate it and have Rust fill a&mut [T]. - Keep growing or long-lived Rust data inside a
#[julia]struct. - When a block does not compile, read the
help:lines of the compiler message first. Common first errors translates the usual ones.
For a project of real size, continue with Safe Rust/Julia Integration.