pub fn slug() -> Result<String, CuidError>
👎Deprecated since 1.3.0: slug() is based on cuid(), which is deprecated. Use cuid2() instead, or switch to the cuid2 crate. See https://github.com/paralleldrive/cuid2#improvements-over-cuid for more information
Expand description
Generate a CUID slug
CUID slugs are shorter, appropriate for short URLs or other uses where uniqueness is not the primary requirement.
Note that this library is capable of generating over 2 million CUID slugs per second on a single thread on a fast machine. If your use case involves generating slugs in loops across threads, it is very possible you’ll wind up with some non-unique slugs, given that the components of the slug are:
- Two characters from the millisecond timestamp as base 36
- Four characters from the atomic counter, which has only ~1.6 million unique values, as base 36
- The first and last character of the fingerprint (which is always the same on a given host)
- Two characters from a random block
For most use cases (i.e. generating fewer than a million slugs per second on a given host), slugs are very likely to be globally unique. However, please bear that limitation in mind, and use full CUIDs if you need a stronger guarantee of uniqueness.
Examples
extern crate cuid;
let slug = cuid::slug();
assert!(cuid::is_slug(slug.unwrap()));