| Crates.io | matchr |
| lib.rs | matchr |
| version | 0.2.5 |
| created_at | 2025-07-01 16:59:18.997234+00 |
| updated_at | 2025-07-06 00:03:49.040087+00 |
| description | A fast fuzzy matcher library written in Rust for use in CLI tools and TUI apps. |
| homepage | |
| repository | https://github.com/0l3d/matchr |
| max_upload_size | |
| id | 1733423 |
| size | 20,149 |
matchr is a lightweight and efficient fuzzy matching library written in Rust.
It helps you score and sort candidate strings by how well they match a query,
with a focus on CLI tools, search features, and quick approximate matching.
Add this to your Cargo.toml:
[dependencies]
matchr = "0.2.5"
use matchr::score;
let query = "fefe";
let candidate = "feature";
let match_score = score(query, candidate);
println!("Score: {}", match_score);
use matchr::match_items;
let query = "xb";
let candidates = ["xbps-install", "xbps-remove", "xbps-query", "grep", "find"];
let results = match_items(query, &candidates);
for (item, score) in results {
println!("{} => score: {}", item, score);
}
// Output (sorted by score):
// xbps-install => score: 90
// xbps-remove => score: 85
// xbps-query => score: 82
// ...
score(query: &str, candi: &str) -> usizeScores how well a candidate string matches the query based on subsequence matching.
Parameters:
query - The search query stringcandi - The candidate string to match againstReturns: A score between 0 and 100, where higher means better match
Scoring Logic:
10 - positionscore / 10match_items<'a>(query: &str, items: &[&'a str]) -> Vec<(&'a str, usize)>Matches multiple items against a query and returns them sorted by score.
Parameters:
query - The search query stringitems - Slice of candidate stringsReturns: Vector of (item, score) tuples, sorted by descending score
use matchr::match_items;
fn search_commands(query: &str) -> Vec<String> {
let commands = ["git commit", "git push", "git pull", "git status", "grep"];
let results = match_items(query, &commands);
results
.into_iter()
.filter(|(_, score)| *score > 10)
.map(|(cmd, _)| cmd.to_string())
.collect()
}
let matches = search_commands("git");
// Returns commands containing "git" as subsequence
use matchr::match_items;
let query = "cfg";
let files = ["config.toml", "Cargo.toml", "src/cfg.rs", "README.md"];
let results = match_items(query, &files);
for (file, score) in results.iter().take(3) {
if *score > 0 {
println!("📁 {} (score: {})", file, score);
}
}
use matchr::match_items;
let query = "xb";
let packages = [
"xbps-install", "xbps-remove", "xbps-query",
"bash", "zsh", "fish", "curl", "wget"
];
let results = match_items(query, &packages);
// xbps-* packages will score highest due to early position matches
matchr is designed to be fast and memory-efficient:
Contributions are welcome! Please feel free to submit a Pull Request.
Licensed under either of
at your option.
Created by 0l3d