slugrs
Fast, flexible, locale-aware slugify library for Rust. Mirrors the behavior of Node.js’s slugify package.
Installation
On crates.io:
[]
= "0.3.2"
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!;
API
slugify(input: &str) -> String: Slugify with default options.slugify_with_options(input: &str, options: &Options) -> String: Full control.Options: Behavior configuration.Locale: Language-specific conversions (De,Tr,Ar).Slugifier: Helper type for repeated calls with the same options.
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
- Minimized use of regex and
deunicode. - Typically uses single-allocation string operations for speed.
Tests
License
MIT