Troubleshooting
This guide covers common issues and solutions when using RustCall.jl.
Installation and Setup
Problem: no working rustc
Error message:
No working rustc found. RustCall.jl resolves the compiler through RustToolChain.jl, ...RustCall does not look up rustc itself. It asks RustToolChain.jl, which uses a rustc on PATH when there is one and otherwise downloads a toolchain through Julia's Artifacts system. The warning means neither could be run, so "not on PATH" is only one of the possible causes.
Solution:
Reproduce the resolution and read the underlying error:
RustCall.check_toolchain() # raises nothing; lists every problem it findsIt names the
rustcandcargoRustToolChain resolves, compares therustcrelease with the oldest supported one (RustCall.minimum_supported_rustc(), therust-versionofdeps/rustcall_extract/Cargo.toml), and checks that the extractor is built and speaks this release's manifest. To see RustToolChain's own error, run the resolution yourself: $using RustToolChain; run(`$(RustToolChain.rustc()) --version`)$.If you want a system Rust, install it from rustup.rs (or a package manager) and make sure
rustc --versionworks in the shell that starts Julia. ArustconPATHtakes precedence over the artifact.If you rely on the artifact toolchain, the usual causes are no network access or a read-only depot during the first download; fix that and retry. On Windows the artifact toolchain also needs the MSVC build tools (a linker); see the RustToolChain.jl README.
Check Julia itself: RustCall requires Julia 1.12 or later (
VERSION).
Problem: Rust helpers library build fails
Error message:
Rust helpers library not found. Ownership types (Box, Rc, Arc) will not work...Solution:
Run the build:
using Pkg Pkg.build("RustCall")Check Cargo availability:
cargo --versionCheck build log:
cat deps/build.logManual build:
cd deps/rustcall_helpers cargo build --release
Compilation Errors
Problem: Rust code syntax errors
Error message:
error: expected one of ...Solution:
Check Rust code syntax:
- Has
#[no_mangle]attribute - Correctly specifies
pub extern "C" - Function signature is correct
- Has
Correct example:
#[no_mangle] pub extern "C" fn my_function(x: i32) -> i32 { x * 2 }Check error message in detail:
# Clear cache and recompile RustCall.clear_cache() rust""" // Fixed code """
Problem: Linking errors
Error message:
undefined symbol: ...Solution:
- Check function name is correct (
#[no_mangle]required) - Verify library is loaded correctly
- Check platform-specific issues:
- macOS:
.dylibfile exists - Linux:
.sofile exists - Windows:
.dllfile exists
- macOS:
Problem: Type mismatch errors
Error message:
ERROR: type mismatchSolution:
Check Rust function signature:
pub extern "C" fn add(a: i32, b: i32) -> i32Use correct types on Julia side:
# Correct # @rust add(Int32(10), Int32(20))::Int32Review the type mapping table
Runtime Errors
Problem: Function not found
Error message:
Function 'my_function' not found in librarySolution:
- Check function name spelling
- Ensure
#[no_mangle]attribute is present - Verify library compiled correctly:
RustCall.clear_cache() rust""" #[no_mangle] pub extern "C" fn my_test_function() -> i32 { 42 } """
Problem: Segmentation fault
Error message:
signal (11): Segmentation faultSolution:
Check pointer validity:
# Warning: Don't use invalid or Julia-managed pointers with Rust ownership typesCheck array bounds
Check memory management (if using ownership types)
Memory Management Problems
Problem: Memory leak
Solution:
An ownership wrapper (
RustBox,RustRc,RustArc,RustVec) is released by its finalizer, but a finalizer runs whenever the GC gets to it. Release eagerly withRustCall.drop!when the lifetime matters:box = RustCall.RustBox(Int32(42)) try # use box finally RustCall.drop!(box) endRustCall.drop!is idempotent: a second call on the same wrapper is a no-op, so an eagerdrop!never conflicts with the finalizer.
Problem: Double free
Error message:
double free or corruptionSolution:
A wrapper's own drop! cannot double free (see above). A double free means the same allocation is owned twice: the one raw pointer was handed to two owning wrappers, or the Rust side freed what a Julia wrapper also owns. Give every allocation exactly one owner; on the Rust side, return ownership with Box::into_raw and never free it again.
Problem: Invalid pointer access
Solution:
RustCall.is_valid and RustCall.is_dropped report the wrapper's state only: is_valid is false once the wrapper was dropped or its pointer is null. Neither can tell where a pointer came from or whether the Rust side has already freed the allocation, so a wrapper built from an arbitrary or dangling raw pointer passes both checks and still segfaults or double frees. Use them to catch use-after-drop! on a wrapper whose ownership you established (one that RustCall allocated, or a pointer Rust handed over with Box::into_raw and never freed), not as a substitute for that ownership:
if RustCall.is_valid(box)
# not dropped on the Julia side; ownership is still your guarantee
end
RustCall.is_dropped(box) # true after drop!FAQ
Q: When should I clear the cache?
A: Normally never: the cache key covers the source, the toolchain and the build environment, so a change to any of them compiles fresh. Clearing (RustCall.clear_cache()) is a diagnostic step when a compiled artifact is suspected to be corrupt or when reclaiming disk space.
Q: Does it work on Windows?
A: Yes, on Windows, macOS and Linux, given a working Rust toolchain (see above) and, on Windows, the MSVC build tools. See Windows.
Q: Can I use multiple Rust libraries simultaneously?
A: Yes. You can define multiple functions in a single rust"" block or use multiple blocks:
# Multiple functions in one block
rust"""
#[no_mangle]
pub extern "C" fn calc_add(a: i32, b: i32) -> i32 { a + b }
#[no_mangle]
pub extern "C" fn calc_mul(a: i32, b: i32) -> i32 { a * b }
"""
result1 = @rust calc_add(Int32(10), Int32(20))::Int32
result2 = @rust calc_mul(Int32(3), Int32(4))::Int32
println("add result = $result1, mul result = $result2")add result = 30, mul result = 12Use unique function names across all rust"" blocks. If the same function name exists in multiple libraries, an ambiguity error will be raised.
Q: Can I use Rust generics?
A: Yes, with automatic monomorphization. See Generics for details.
Q: Best practices for error handling?
A:
- Use
Resulttype on Rust side - Use
result_to_exceptionon Julia side - Or use
unwrap_orfor default values
Debugging Tips
1. Enable debug logging
RustCall.jl uses Julia's built-in Logging module with @debug, @info, @warn, and @error macros.
Option A: Environment variable (recommended)
Run Julia with the JULIA_DEBUG environment variable:
JULIA_DEBUG=RustCall julia -e 'using RustCall; ...'Or set it within Julia before loading RustCall:
ENV["JULIA_DEBUG"] = "RustCall"
using RustCallOption B: Global logger
using Logging
global_logger(ConsoleLogger(stderr, Logging.Debug))
using RustCallDebug logging shows detailed information about:
- Rust code compilation
- Library loading and caching
- Generic function registration and monomorphization
- Function pointer resolution
- Error recovery attempts
2. Clear cache
RustCall.clear_cache()3. Check library status
# List cached libraries
RustCall.list_cached_libraries()
# Check cache size
RustCall.get_cache_size()4. Check type information
# Check type mapping
RustCall.rusttype_to_julia(:i32) # => Int32
RustCall.juliatype_to_rust(Int32) # => "i32"
# What the FFI contract says about a position, including its ABI, its ccall
# slots and who releases it
RustCall.ffi_describe("String"; direction = :return, abi = "string")5. "The FFI contract cannot describe the return type of ..."
RustCall no longer guesses a return type it cannot derive. The message names the signature and what the contract knows about the type; see The FFI Type Contract for the supported spellings.
Three fixes, in order of preference:
- change the Rust signature to a supported type — pass an aggregate behind a pointer (
*mut MyType) rather than by value; - annotate the Julia call site:
@rust f(x)::T, which bypasses inference entirely; - for
write_bindings_to_file, passstrict = :warnto fall back toAnyand keep the rest of the crate building while you deal with the offending type.Anyin accallslot is not well defined, so treat this as temporary.
6. "cannot call a Rust function without a return type"
@rust f(x) without ::T needs a return type from the manifest. A plain #[no_mangle] extern "C" function compiled outside a rust""" block has none; either annotate the call (@rust f(x)::Int32) or mark the function #[julia] so the extractor reports its signature.
RustCall used to guess here — the return type was taken from the type of the first argument, defaulting to Int64. That guess is not derivable from an argument and reading a return slot at the wrong width is undefined behaviour, so it was removed (#245, #246).