slugrs
Fast, flexible, locale-aware slugify library for Rust. Mirrors the behavior of Node.js’s slugify package.
Installation
On crates.io:
[]
= "0.3.4"
Local path (during development):
[]
= { = "../slugrs" }
Quick Start
Recommended: Fluent Slugifier
use ;
let slug = new
.separator
.locale
.max_length
.slugify;
assert_eq!;
Alternatives:
use slugify;
// Defaults: separator "-", lowercase, trim, apostrophe removal, emoji removal
assert_eq!;
use ;
// Simple customization (Options builder)
let slug = slugify_with_options;
assert_eq!;
let tr = default.locale;
assert_eq!;
Batch processing
use ;
// Sequential batch
let out = slugify_many;
assert_eq!;
// Auto batch (uses a heuristic: total bytes and count)
let auto = slugify_many_auto;
assert_eq!;
// Fluent
let s = new.locale;
let v2 = s.slugify_many_auto;
assert_eq!;
Parallel (optional feature)
Enable the dependency feature in your Cargo.toml:
[]
= { = "0.3.4", = ["parallel"] }
Then you can call the parallel helpers:
use ;
let out = par_slugify_many;
let s = new;
let v2 = s.par_slugify_many;
Heuristic (auto) rules:
- Chooses parallel when total_bytes ≥ 100_000 × logical_cores, or input_count ≥ 1000.
- Otherwise runs sequentially to avoid parallel overhead on small workloads.
API
slugify(input: &str) -> String: Slugify with default options.slugify_with_options(input: &str, options: &Options) -> String: Full control.slugify_many<'a, I: IntoIterator<Item=&'a str>>(inputs, options) -> Vec<String>slugify_many_auto(inputs: &[&str], options) -> Vec<String>par_slugify_many<'a, I: IntoParallelIterator<Item=&'a str>>(inputs, options) -> Vec<String>(feature:parallel)Slugifier: Helper type for repeated calls with the same options (includes batch and parallel methods).Locale: Language-specific conversions (De,Tr,Ar).
Options
| Field | Type | Default | Description |
|---|---|---|---|
separator |
String |
"-" |
Word separator. Consecutive separators are collapsed. |
locale |
Option<Locale> |
None |
Language-specific mappings like TR/DE/AR. |
remove |
Option<Regex> |
None |
Custom removal regex applied before processing. |
lowercase |
bool |
true |
Convert output to lowercase. |
trim |
bool |
true |
Trim leading/trailing separators. |
max_length |
Option<usize> |
None |
Max length; trims trailing separator after truncation. |
drop_emoji |
bool |
true |
Drop emoji and most symbol characters pre-processing. |
drop_apostrophes |
bool |
true |
Drop apostrophes to avoid inserting separators. |
Notes:
- Emoji and similar symbols are removed by default.
- Non-ASCII characters are transliterated via
deunicode. - Apostrophes ("'",
’) are removed by default to avoid extra separators.
Locale Support
Locale::Tr:İ/ı -> I/i,ğ -> g,ş -> s,ç -> c,ö -> o,ü -> u, etc.Locale::De:ä -> ae,ö -> oe,ü -> ue,ß -> ss, etc.Locale::Ar: basic Arabic to Latin mappings (hamza/diacritics dropped) to produce readable slugs. Example:- "الحرب على غزة مباشر إسرائيل" →
alharb-ealaa-ghazat-mubashir-iisrayiyl
- "الحرب على غزة مباشر إسرائيل" →
Locale transforms are applied before transliteration; remaining characters are then converted to ASCII.
Examples
use Regex;
use ;
assert_eq!;
assert_eq!;
let opts = default.separator.remove;
assert_eq!;
let s = new.separator.trim;
assert_eq!;
let limited = default.max_length;
assert_eq!;
let ar = default.locale;
assert_eq!;
Performance
- Single pass build after one-shot transliteration, minimized allocations.
- Optional parallel batch processing for large workloads.
Tests
License
MIT