Skip to content

Python syntax, Rust libraries

Rust libraries

Write application code in Python syntax and keep library code in Rust. Oxid provides designed Rust-backed APIs; importing an arbitrary crate or PyPI package is not automatic.

Designed APIs, not Python package compatibility

  • polars: supported data-frame operations backed by the Polars Rust crates.
  • oxid_server: HTTP routing, authentication, sessions and WebSockets.
  • oxid_data: a database API with SQL template strings, migrations and row policies.
  • Standard modules: supported APIs for strings, collections, JSON, files and more; target limits apply.

These are Oxid APIs, not a promise that an existing Python package's whole surface works. Host libraries such as the server and database are not browser APIs.

Export a small application-facing Rust API

oxid new rust creates a working application and library. Its rust/fast/src/lib.rs uses #[oxid_export::module] to mark the public boundary. The generated pyproject.toml maps fast to rust/fast under [tool.oxid.crates]. Cargo is required to build the library.

oxid new rust stats
cd stats
oxid run app/main.py
oxid build app/main.py -o stats

The scaffold includes this mean function. Replace app/main.py with the small caller below:

#[oxid_export::module]
pub mod api {
    pub fn mean(xs: &[f64]) -> f64 {
        xs.iter().sum::<f64>() / xs.len() as f64
    }
}
from fast import mean

print(mean([1.0, 2.0, 6.0]))

Run it to print 3.0; build it to link the library into a native executable. The application runs through the JIT during source run; compiling a Rust dependency does not turn source run into an application-compilation fallback.

Keep the boundary concrete

Generic Rust functions need a concrete exported wrapper; a crate's entire generic API does not cross automatically. Borrowed results do not cross: return an owned value rather than a reference into the library's memory.

Application int is i64 and float is f64. When a Rust library needs a narrower integer, the adapter checks the conversion and reports an error instead of truncating or wrapping. The library author chooses the boundary; no automatic binding generator is promised.