| Crates.io | rslug |
| lib.rs | rslug |
| version | 0.3.0 |
| created_at | 2025-10-14 04:28:27.626777+00 |
| updated_at | 2025-10-14 06:23:49.865908+00 |
| description | A simple, fast, and configurable library to create URL-friendly slugs from strings. |
| homepage | |
| repository | https://github.com/ezrantn/rslug |
| max_upload_size | |
| id | 1881654 |
| size | 22,761 |
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.
slugify! macro.Slugifier builder for custom separators, case control, and more.slugify_ascii method for maximum speed with ASCII-only input.rslug is tiny and has minimal dependencies.Add rslug to your Cargo.toml file:
[dependencies]
rslug = "0.3.0" # Check for the latest version on crates.io
Or
cargo add rslug
The easiest way to generate a slug is with the slugify! macro, which uses the default settings (hyphen separator, lowercase output).
use rslug::slugify;
// Basic usage
let text = "Hello World! This is a test... 123?";
let slug = slugify!(text);
assert_eq!(slug, "hello-world-this-is-a-test-123");
// Unicode support
let unicode_text = "你好世界 & Rust";
let unicode_slug = slugify!(unicode_text);
assert_eq!(unicode_slug, "nihaoshijie-rust");
For more control over the output, create a Slugifier instance using its builder pattern. This allows you to customize the slug generation rules.
use rslug::Slugifier;
// Example 1: Using an underscore as a separator
let slugifier_underscore = Slugifier::new()
.separator("_");
let text = "Custom Separator Example!";
let slug = slugifier_underscore.slugify(text);
assert_eq!(slug, "custom_separator_example");
// Example 2: Preserving the original case
let slugifier_case_sensitive = Slugifier::new()
.to_lowercase(false);
let text_with_case = "Keep The Case";
let slug_with_case = slugifier_case_sensitive.slugify(text_with_case);
assert_eq!(slug_with_case, "Keep-The-Case");
let slugifier_truncate = Slugifier::new()
.truncate(20);
let text_to_truncate = "this is a very long title that should be shortened";
let slug_with_truncate = slugifier_truncate.slugify(text_to_truncate);
assert_eq!(slug_with_truncate, "this-is-a-very-long");
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 rslug::Slugifier;
let slugifier = 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(ascii_text);
assert_eq!(slug, "this-is-ascii-only-so-it-can-be-faster");
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.
This library is open-source and available under the MIT License.