Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
osrm-binding
Rust bindings for OSRM (Open Source Routing Machine), providing an idiomatic and type-safe interface to access core OSRM functionalities (route, table, trip) from Rust.
π Features
- πΊοΈ Calculate routes, trips, and distance/duration tables using OSRM
- π¦ Safe Rust API backed by the native OSRM engine
- π‘ Embedded, high-performance routing without an HTTP server
- π§ͺ Route, table, trip, CH, MLD, car, and bicycle coverage
π¦ Installation
Add the crate to your Cargo.toml:
cargo add osrm-binding
Building Dependencies
This library requires OSRM to be built and linked. Below are instructions for setting up the dependencies.
Nix development shell
With Nix and flakes enabled, enter a shell containing Rust and all native OSRM build dependencies:
nix develop
cargo build
The first Cargo build downloads and compiles the OSRM source, so it can take a few minutes. To use a different OSRM version, set OSRM_BACKEND_REF as described below before running Cargo.
Local Installation (Ubuntu 24.04)
Install the required system dependencies:
sudo apt update
sudo apt install build-essential git cmake pkg-config \
libbz2-dev libxml2-dev libzip-dev libboost-all-dev \
lua5.2 liblua5.2-dev libtbb-dev libfmt-dev
Dockerfile
Use the following Dockerfile to build your application in a containerized environment:
FROM rust:1.88.0-bookworm AS builder
WORKDIR /usr/src/app
COPY Cargo.toml Cargo.lock ./
COPY ./src ./src
RUN apt-get update && \
apt-get -y --no-install-recommends --no-install-suggests install \
ca-certificates \
cmake \
g++ \
gcc \
git \
libboost1.81-all-dev \
libbz2-dev \
liblua5.4-dev \
libtbb-dev \
libxml2-dev \
libzip-dev \
lua5.4 \
make \
pkg-config \
libfmt-dev
RUN ls -la /usr/lib/x86_64-linux-gnu/libboost_thread*
RUN cargo build --release -vv
FROM debian:bookworm-slim
WORKDIR /usr/src/app
COPY --from=builder /usr/src/app/target/release/my-bin ./
RUN apt-get update && \
apt-get install -y --no-install-recommends --no-install-suggests \
expat \
libboost-date-time1.81.0 \
libboost-iostreams1.81.0 \
libboost-program-options1.81.0 \
libboost-thread1.81.0 \
liblua5.4-0 \
libtbb12 && \
rm -rf /var/lib/apt/lists/* && \
ldconfig /usr/local/lib
CMD ["./my-bin"]
Note: Replace
my-binwith your actual binary name. This Dockerfile installs OSRM build dependencies and runtime libraries.
OSRM version
By default this crate downloads and links osrm-backend v6.0.0. OSRM stamps a version fingerprint into the preprocessed .osrm.* files and refuses to load data prepared by a different version (File is incompatible with this version of OSRM ...). The version that prepared your data must match the version this crate links against.
If your .osrm files were generated with a different OSRM version, either regenerate them with v6.0.0, or build this crate against the matching version using the OSRM_BACKEND_REF environment variable (any git tag, branch, or commit hash from Project-OSRM/osrm-backend):
OSRM_BACKEND_REF=v5.27.1 cargo build
π οΈ Usage
Initialization
Initialize the OSRM engine with a preprocessed OSRM data file. The Algorithm you pass must match the preprocessing pipeline used to build the .osrm files:
- CH (Contraction Hierarchies):
osrm-extractβosrm-contract, then useAlgorithm::CH - MLD (Multi-Level Dijkstra):
osrm-extractβosrm-partitionβosrm-customize, then useAlgorithm::MLD
use ;
// Data prepared with osrm-contract:
let engine = new
.expect;
// Or, data prepared with osrm-partition + osrm-customize:
let engine = new
.expect;
Route Calculation
Build and execute a route request:
use ;
let request = builder
.points
.steps
.build
.unwrap;
let result = engine.route.unwrap;
println!;
Table (Distance/Duration Matrix)
Compute a distance/duration table:
use ;
let request = new;
let response = engine.table.unwrap;
println!;
Simple Route
For quick single-origin to single-destination routing:
use Point;
let result = engine.simple_route.unwrap;
println!;
Trip API
The transport profile is part of the preprocessed dataset rather than the request. Load the bicycle dataset in a separate engine, then optimize a round trip with multiple waypoints:
use ;
let bicycle_engine = new.unwrap;
let request = builder
.points
.steps
.build
.unwrap;
let response = bicycle_engine.trip.unwrap;
let trip = &response.trips;
println!;
for waypoint in response.waypoints
Trip requests are closed round trips and let OSRM choose the starting waypoint by default. For an open trip that preserves the first and last points, set .roundtrip(false), .source(TripSource::First), and .destination(TripDestination::Last) on the builder. Route and trip instructions are opt-in with .steps(true) so the default response stays smaller and faster to parse.
π¬ Tests
The test suite uses routes between Paris, Lyon, and Marseille. From the Nix development shell, download the current France extract, prepare OSRM v6.0.0 car and bicycle datasets for both MLD and CH, and populate .env automatically:
setup-test-data
cargo test
The France PBF is downloaded only once and shared by both profiles. Car and bicycle still need separate processed routing graphs, so expect substantial disk, memory, and processing-time requirements. Downloads resume if interrupted, and completed data is reused on subsequent runs. The generated files live in .test-data/ and are ignored by Git.
The generated .env contains OSRM_TEST_DATA_PATH_MLD and OSRM_TEST_DATA_PATH_CH for car routing, plus OSRM_TEST_DATA_PATH_BICYCLE_MLD and OSRM_TEST_DATA_PATH_BICYCLE_CH for bicycle routing. To use data you prepared yourself instead, set those paths manually. The dataset version must match the linked OSRM version, and each path must use the preprocessing pipeline matching its algorithm and transport profile.
π Performance
Native performance using cargo bench
calculate_multiple_routes_around_paris_10km_mld
time: [5.4872 ms 5.6545 ms 5.8246 ms]
calculate_multiple_routes_around_paris_100km_mld
time: [13.063 ms 13.877 ms 14.652 ms]
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) low mild
calculate_multiple_routes_around_paris_10km_ch
time: [3.8034 ms 3.8599 ms 3.9175 ms]
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high mild
calculate_multiple_routes_around_paris_100km_ch
time: [5.9891 ms 6.2444 ms 6.4946 ms]
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) low mild
π License
This project is licensed under the MIT License.
β¨ Contributions
Contributions are welcome! Feel free to open issues or pull requests to improve performance, add more OSRM API bindings, or enhance usability.
Made with β€οΈ in Rust.