18 · Modules
Rust with extern
Typed Rust functions, callbacks and locked Cargo dependencies.
On this page
Import rust from the plugin of the same name. A typed Zolo function becomes
a compiled Rust entry point. Call it like any other function. These examples
require the CLI and the installed Rust plugin.
Preserve exact integers, import a public Rust function and await a genuine Rust future.
use plugin rust::{rust}
extern rust fn exact(value: int) -> int {
value
}
extern rust fn upper(value: str) -> str {
value.to_uppercase()
}
extern rust async fn later(value: int) -> int {
async { value + 1 }.await
}
extern rust from "./rust_fixtures/math.rs" use {add}
print(exact(9007199254740993)) // expected: 9007199254740993
print(upper("hello Rust")) // expected: HELLO RUST
print(add(left: 20, right: 22)) // expected: 42
print(await later(41)) // expected: 42
Requires the Zolo CLI/host — open in the playground or run locally.
int maps to i64, float to f64, str to String, bytes
to Vec<u8>, arrays to Vec<T> and nullable values to Option<T>.
Public-field structs and string-keyed maps cross the boundary as copied data.
Native objects with retained identity, receiver methods, generic exports and
unsafe signatures require a concrete free-function adapter.
Native expressions capture visible Zolo variables. Specify the expected result type when it cannot be inferred from the surrounding Zolo code:
use plugin rust::{rust}
let value = 40
let answer: int = extern rust { value + 2 }
print(answer)
To capture a Zolo variable in a formatting macro, use
extern rust { format!("{value}", value = value) }. A name mentioned only
inside a string does not create a host capture. Parameters of a typed extern
function also support Rust's implicit formatting captures.
Cargo packages
Declare serde_json once and use it directly inside an extern function.
// Prepare with zolo install or opt into zolo run ... --install=auto.
use plugin rust::{rust}
extern rust deps {
"serde_json" = "1.0.145"
}
extern rust fn answer() -> int {
let value: serde_json::Value = serde_json::from_str(r#"{"answer":42}"#).unwrap();
value["answer"].as_i64().unwrap()
}
print(answer()) // expected: 42
Requires the Zolo CLI/host — open in the playground or run locally.
To select features globally for the provider:
extern rust deps {
"serde" = {
version: "1.0.228",
features: ["derive"],
default_features: false,
}
}
The corresponding project declaration is:
[foreign.rust.dependencies]
serde = { version = "1.0.228", features = ["derive"], default_features = false }
Run zolo install main.zolo before a project with manual installation policy.
Cargo resolves the graph and vendors its sources into an isolated environment.
Compilation uses its exact Cargo.lock with --locked --offline. Shared
--offline and --frozen policies also apply. Build scripts and proc macros
selected by a dependency may run during explicit compilation; editor analysis
keeps them disabled.
Callbacks, errors and deployment
A parameter fn(int) -> int becomes a call-scoped Rust closure.
It can call back into Zolo, including nested Rust calls. Do not store a borrowed
callback or call it from another native thread.
extern rust async fn compiles a genuine async function and adapts it to a
Zolo Future. The provider supplies a small standard-library executor; a Tokio
reactor or another executor-specific context needs an explicit wrapper.
Cancellation closes callback and result delivery and cannot stop arbitrary native
instructions. Panics that unwind and Result<T, E> failures become Zolo errors
with authored source information.
Preparation needs rustc, or Cargo when packages are declared.
Select them with ZOLO_RUSTC and ZOLO_CARGO. Native editor queries use
rust-analyzer through ZOLO_RUST_ANALYZER, preserve unsaved text and do not run
authored initializers. Compiled artifacts include machine code and source
snapshots; VM, native and LLVM can run them with dynamic, embedded or static
plugin packaging after removing source files and the Rust toolchain.
Artifacts match their target OS and architecture. Hosted wasm-aot execution is
unavailable. Acceptance currently covers Windows x64. panic=abort, native
memory corruption and explicit process exit retain native process behavior.
See also