---
title: "Rust core"
description: "Use strsimpy-core from Rust and understand the workspace behind the Python package."
---

> Documentation Index
> Fetch the complete documentation index at: https://luozhouyang.github.io/python-string-similarity/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust core

The algorithms live in `strsimpy-core`, a pure Rust crate with no Python dependency. The Python package is a thin layer on top: `strsimpy-py` wraps the core with PyO3, and `python/strsimpy/` preserves the historical Python API.

## Workspace layout

```text
python-string-similarity/
├── Cargo.toml                  # Workspace manifest and shared package metadata
├── crates/
│   ├── strsimpy-core/          # Pure Rust implementations of every algorithm
│   └── strsimpy-py/            # PyO3 bindings; builds strsimpy._strsimpy
├── python/strsimpy/            # Python package, base classes and module shims
└── tests/                      # Python integration tests
```

The workspace requires Rust 1.85 or newer. `strsimpy-core` has no dependency on PyO3 and can be built and tested on its own.

## Use the crate from Rust

Add the core crate as a path dependency, adjusting the path if your project lives elsewhere:

```toml
[dependencies]
strsimpy-core = { path = "crates/strsimpy-core" }
```

Free functions are the simplest entry point. This example calls the Levenshtein implementation directly:

```rust
use strsimpy_core::levenshtein;

fn main() {
    let distance = levenshtein::distance("kitten", "sitting");
    assert_eq!(distance, 3.0);
    println!("{distance}");
}
```

Most algorithms also expose a calculator struct that stores the parameters once:

```rust
use strsimpy_core::jaccard::Jaccard;

fn main() {
    let jaccard = Jaccard::new(2);
    let similarity = jaccard.similarity("night", "nacht");
    println!("{similarity}");
}
```

Algorithms with configuration follow the same pattern. For example, normalized compression distance takes a compressor:

```rust
use strsimpy_core::normalized_compression_distance::{self, Compressor};

fn main() {
    let distance = normalized_compression_distance::distance("aaaa", "aaab", Compressor::Rle);
    println!("{distance}");
}
```

> **Rust types differ from Python's compatibility layer**
>
> The core returns Rust-native types, so Levenshtein returns `f64` and QGram returns `usize`. The PyO3 bindings convert those values to preserve the historical Python behavior, including the mixed `int` and `float` return types described in the [quickstart](https://luozhouyang.github.io/python-string-similarity/getting-started/quickstart/).

## Run the Rust tests

The crate's unit tests live beside each module. Run them with:

```bash
cargo test -p strsimpy-core
```

To run the whole workspace, including the binding crate:

```bash
cargo test --workspace
```

## Unicode semantics

All algorithms operate on Unicode scalar values (code points), not bytes or grapheme clusters. This matches Python's `len(str)` and indexing semantics:

- `"上海"` has two characters.
- `"😀"` has one character.
- Combining marks count separately; text is not normalized to a grapheme-cluster representation.

Shingle-based measures first collapse runs of Python whitespace to a single ASCII space, then count overlapping windows of `k` characters.

## How the bindings fit in

`crates/strsimpy-py` compiles to the extension module `strsimpy._strsimpy` using PyO3 with the `abi3-py310` feature. The Python package in `python/strsimpy/` re-exports the classes, provides the base-class interfaces and keeps the per-module import paths working.

When you change a core function signature, update the corresponding wrapper in `crates/strsimpy-py/src` and any compatibility shim under `python/strsimpy/`. The [contributing guide](https://luozhouyang.github.io/python-string-similarity/contributing/) lists the full check suite.

Source: https://luozhouyang.github.io/python-string-similarity/getting-started/rust/index.mdx
