sluggy
Types
A slug, along with its word count and length, computed alongside it.
Values of this type can only be constructed with from_string, which
guarantees words, length, and str always stay consistent with
each other.
pub opaque type Slug
Values
pub fn combine(s1: Slug, s2: Slug) -> Slug
Combines two Slug’s into a single one, summing their word counts
and lengths (plus one, for the hyphen joining them).
Note: This function does not enforce a max. length cap on the result.
Examples
let slug_1 = sluggy.from_string("Hello")
let slug_2 = sluggy.from_string("Sluggy !")
let combined = sluggy.combine(slug_1, slug_2)
assert sluggy.inspect(combined) == "Slug(words: 2, length: 12, str: hello-sluggy)"
pub fn from_string(str: String) -> Slug
Creates a new Slug from the provided string.
If you only need the slugified string and don’t want the words and length fields, use str_slugify instead.
Example
let slug = sluggy.from_string("New article is out.")
assert sluggy.inspect(slug) == "Slug(words: 4, length: 18, str: new-article-is-out)"
pub fn inspect(slug: Slug) -> String
Returns a human-readable, debug-style representation of a Slug,
showing its word count, length, and slug string.
Example
let slug = sluggy.from_string("New article is out.")
assert sluggy.inspect(slug) == "Slug(words: 4, length: 18, str: new-article-is-out)"
pub fn str_slugify(str: String) -> String
Converts a string to its slug representation, returning a plain
String instead of a whole Slug.
This is more convenient when you don’t need the word count or precomputed length; use from_string instead if you do.
Since this is SEO-oriented, the default length cap of the slug is 60 characters. If you want a custom length, use str_slugify_max_length instead.
Example
assert sluggy.str_slugify("Sluggy is awesome !") == "sluggy-is-awesome"
pub fn str_slugify_max_length(
str: String,
max_length: Int,
) -> String
Same as str_slugify, but with a custom max. length instead of the default of 60 characters.
Note that this doesn’t mean the resulting slug will be exactly
max_length characters long; only that it won’t exceed it.
Example
assert sluggy.str_slugify_max_length("Sluggy is awesome !", 8) == "sluggy"
assert sluggy.str_slugify_max_length("Sluggy is awesome !", 9) == "sluggy-is"
assert sluggy.str_slugify_max_length("Sluggy is awesome !", 20) == "sluggy-is-awesome"