Rust Interface#

See the BridgeStan Crate documentation on docs.rs


Installation#

The BridgeStan Rust client is available on crates.io and via cargo:

cargo add bridgestan

To build and use BridgeStan models, a copy of the BridgeStan C++ source code is required. Please follow the Getting Started guide or use the Rust client in tandem with an interface such as Python which automates this process.

STAN_THREADS=true needs to be specified when compiling a model, for more details see the API reference.

Example Program#

An example program is provided alongside the Rust crate in examples/example.rs:

Show example.rs
use bridgestan::{open_library, BridgeStanError, Model};
use std::ffi::CString;
use std::path::Path;

fn main() {
    // The path to the compiled model.
    // Get for instance from python `bridgestan.compile_model`
    let path = Path::new(env!["CARGO_MANIFEST_DIR"])
        .parent()
        .unwrap()
        .join("test_models/simple/simple_model.so");

    let lib = open_library(path).expect("Could not load compiled Stan model.");

    // The dataset as json
    let data = r#"{"N": 7}"#;
    let data = CString::new(data.to_string().into_bytes()).unwrap();

    // The seed is used in case the model contains a transformed data section
    // that uses rng functions.
    let seed = 42;

    let model = match Model::new(&lib, Some(data), seed) {
        Ok(model) => model,
        Err(BridgeStanError::ConstructFailed(msg)) => {
            panic!(
                "Model initialization failed. Error message from Stan was {}",
                msg
            )
        }
        _ => {
            panic!("Unexpected error")
        }
    };

    let n_dim = model.param_unc_num();
    assert_eq!(n_dim, 7);
    let point = vec![1f64; n_dim];
    let mut gradient_out = vec![0f64; n_dim];
    let logp = model
        .log_density_gradient(&point[..], true, true, &mut gradient_out[..])
        .expect("Stan failed to evaluate the logp function.");
    println!("logp: {}\ngrad: {:?}", logp, gradient_out);
}

API Reference#

See docs.rs for the full API reference: https://docs.rs/bridgestan