Core Concepts
Type Mapping
Section titled “Type Mapping”BridgeRust automatically converts between Rust types and their target language equivalents.
| Rust Type | Python Type | Node.js Type | Notes |
|---|---|---|---|
String | str | string | Copied on conversion |
i32, f64, etc. | int, float | number | |
bool | bool | boolean | |
Vec<T> | List[T] | T[] | Converted recursively |
Option<T> | Optional[T] | T | null | None becomes None/null |
Result<T, E> | T (or raise) | T (or throw) | Err causes an exception |
struct (exported) | class | class | Must be marked with #[export] |
Async Support
Section titled “Async Support”BridgeRust has first-class support for async functions.
#[bridgerust::export]pub async fn fetch_data(url: String) -> Result<String> { // Standard Rust async code let resp = reqwest::get(url).await?.text().await?; Ok(resp)}- Python: Returns an
Awaitable(coroutine). Useawait fetch_data(...)in Python. - Node.js: Returns a
Promise. Useawait fetch_data(...)in JavaScript.
runtime requirement: For async functions to work, you generally need a runtime. BridgeRust assumes a Tokio runtime is available if you use async features.
Error Handling
Section titled “Error Handling”Return Result<T, E> from your functions. If E implements ToString or Display, it will be converted into:
- Python:
RuntimeError(or a custom exception if configured). - Node.js:
Errorobject (rejected Promise for async).
#[bridgerust::export]pub fn divide(a: f64, b: f64) -> Result<f64, String> { if b == 0.0 { return Err("Cannot divide by zero".to_string()); } Ok(a / b)}