rslug 🐌
A simple, fast, and configurable Rust library for creating URL-friendly slugs from strings, with robust support for Unicode.
This library is inspired by popular slugify utilities in other languages and aims to be a lightweight and ergonomic solution for any Rust application.
Key Features
- Simple API: Get started instantly with the
slugify!macro. - Highly Configurable: Use the
Slugifierbuilder for custom separators, case control, and more. - Unicode Support: Non-ASCII characters are intelligently transliterated to their ASCII equivalents.
- Performance-Optimized ASCII: Includes a
slugify_asciimethod for maximum speed with ASCII-only input. - Lightweight:
rslugis tiny and has minimal dependencies. - Fast: Built for performance, ideal for web servers and static site generators.
Installation
Add rslug to your Cargo.toml file:
Or
Quick Start
The easiest way to generate a slug is with the slugify! macro, which uses the default settings (hyphen separator, lowercase output).
use slugify;
// Basic usage
let text = "Hello World! This is a test... 123?";
let slug = slugify!;
assert_eq!;
// Unicode support
let unicode_text = "你好世界 & Rust";
let unicode_slug = slugify!;
assert_eq!;
Advanced Configuration
For more control over the output, create a Slugifier instance using its builder pattern. This allows you to customize the slug generation rules.
use Slugifier;
// Example 1: Using an underscore as a separator
let slugifier_underscore = new
.separator;
let text = "Custom Separator Example!";
let slug = slugifier_underscore.slugify;
assert_eq!;
// Example 2: Preserving the original case
let slugifier_case_sensitive = new
.to_lowercase;
let text_with_case = "Keep The Case";
let slug_with_case = slugifier_case_sensitive.slugify;
assert_eq!;
Performance: ASCII-Only Slugs
For performance-critical scenarios where you can guarantee the input is ASCII, you can use the slugify_ascii method. It operates directly on bytes (&[u8]) and avoids the overhead of Unicode transliteration, making it significantly faster.
use Slugifier;
let slugifier = new;
// Note the `b` prefix for a byte string literal
let ascii_text = b"This is ASCII-only, so it can be faster!";
let slug = slugifier.slugify_ascii;
assert_eq!;
Contributing
Contributions are welcome! If you have a feature request, find a bug, or want to improve the code, please feel free to open an issue or submit a pull request.
License
This library is open-source and available under the MIT License.