Skip to content

Rust core

Use strsimpy-core from Rust and understand the workspace behind the Python package.

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

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:

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

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

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:

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:

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

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

Run the Rust tests

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

cargo test -p strsimpy-core

To run the whole workspace, including the binding crate:

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 lists the full check suite.

Type to search…

↑↓ navigate↵ selectEsc close