| Crates.io | xgboost-rust |
| lib.rs | xgboost-rust |
| version | 0.1.0 |
| created_at | 2025-12-25 20:02:46.726812+00 |
| updated_at | 2025-12-25 20:02:46.726812+00 |
| description | Rust bindings for XGBoost, a gradient boosting library for machine learning. Downloads XGBoost binaries at build time for cross-platform compatibility. |
| homepage | https://github.com/aryehlev/xgboost-rust |
| repository | https://github.com/aryehlev/xgboost-rust |
| max_upload_size | |
| id | 2004899 |
| size | 153,623 |
Rust bindings for XGBoost, a gradient boosting library for machine learning.
XGBOOST_VERSION environment variableSend + Sync for XGBoost ≥ 1.4Add this to your Cargo.toml:
[dependencies]
xgboost-rust = "0.1.0"
macOS:
install_name_tool (included with Xcode Command Line Tools)Linux:
patchelf (for setting SONAME, but not required)Windows:
.lib) needed for MSVC linkinguse xgboost_rust::{Booster, XGBoostResult};
fn main() -> XGBoostResult<()> {
// Load a pre-trained model
let booster = Booster::load("model.json")?;
// Prepare your data (row-major: num_rows x num_features)
let data = vec![
1.0, 2.0, 3.0, // Row 1
4.0, 5.0, 6.0, // Row 2
];
let num_rows = 2;
let num_features = 3;
// Make predictions
let predictions = booster.predict(&data, num_rows, num_features, 0, false)?;
println!("Predictions: {:?}", predictions);
Ok(())
}
See the examples directory for more examples including:
By default, XGBoost version 3.1.1 is used. To use a different version, set the XGBOOST_VERSION environment variable before building:
export XGBOOST_VERSION=3.0.0
cargo build
This crate downloads the appropriate XGBoost Python wheel from PyPI during the build process, extracts the compiled library, and links against it. This approach ensures:
Thread safety is version-aware:
Booster implements Send + Sync and is thread-safe for predictions on tree models. You can safely share Arc<Booster> across threads.Booster does NOT implement Send + Sync. Use one booster per thread or wrap in Arc<Mutex<Booster>>.use std::sync::Arc;
use std::thread;
use xgboost_rust::Booster;
let booster = Arc::new(Booster::load("model.json")?);
let booster_clone = booster.clone();
thread::spawn(move || {
// Safe concurrent predictions with XGBoost ≥ 1.4
let predictions = booster_clone.predict(&data, rows, cols, 0, false)?;
});
The version check happens automatically at build time based on the XGBOOST_VERSION environment variable.
Run the basic example:
cargo run --example basic_usage
Run the advanced example:
cargo run --example advanced_usage
Apache-2.0
Inspired by catboost-rust and lightgbm-rust.