ShortId

Struct ShortId 

Source
pub struct ShortId(/* private fields */);
Expand description

A newtype wrapper around a short ID string.

Provides a typed interface for working with short IDs, with methods for generation and conversion. The inner string is always a valid 14-character URL-safe identifier.

§Examples

use short_id::ShortId;

// Generate a random ID
let id = ShortId::random();
assert_eq!(id.as_str().len(), 14);

// Convert to string
let s: String = id.into_string();

Implementations§

Source§

impl ShortId

Source

pub fn random() -> Self

Creates a new random short ID.

This is equivalent to calling short_id() but returns a typed ShortId.

§Examples
use short_id::ShortId;

let id = ShortId::random();
assert_eq!(id.as_str().len(), 14);
Examples found in repository?
examples/newtype.rs (line 5)
3fn main() {
4    // Create a random ID
5    let id1 = ShortId::random();
6    println!("Random ID: {}", id1);
7
8    // Create a time-ordered ID
9    let id2 = ShortId::ordered();
10    println!("Ordered ID: {}", id2);
11
12    // Access as string slice
13    let s: &str = id1.as_str();
14    println!("As str: {}", s);
15
16    // Use AsRef<str>
17    print_id(&id2);
18
19    // Convert to String
20    let string: String = id1.clone().into_string();
21    println!("Into String: {}", string);
22
23    // Create from String
24    let id3: ShortId = string.into();
25    println!("From String: {}", id3);
26
27    // Compare IDs (PartialEq, Ord)
28    let id4 = ShortId::random();
29    let id5 = ShortId::random();
30    println!("\nIDs are equal: {}", id4 == id5);
31    println!("ID4 < ID5: {}", id4 < id5);
32}
More examples
Hide additional examples
examples/basic.rs (line 34)
5fn main() {
6    println!("=== Functions ===");
7
8    // Generate a random ID using the function
9    let random_id = short_id();
10    println!("short_id():         {random_id}");
11
12    // Generate a time-ordered ID using the function
13    #[cfg(feature = "std")]
14    {
15        let ordered = short_id_ordered();
16        println!("short_id_ordered(): {ordered}");
17    }
18
19    println!("\n=== Macros ===");
20
21    // Generate IDs using macros (convenient shorthand)
22    let macro_id = id!();
23    println!("id!():              {macro_id}");
24
25    #[cfg(feature = "std")]
26    {
27        let macro_ordered = ordered_id!();
28        println!("ordered_id!():      {macro_ordered}");
29    }
30
31    println!("\n=== Typed Wrapper ===");
32
33    // Generate IDs using the ShortId type
34    let typed_random = ShortId::random();
35    println!("ShortId::random():  {typed_random}");
36
37    #[cfg(feature = "std")]
38    {
39        let typed_ordered = ShortId::ordered();
40        println!("ShortId::ordered(): {typed_ordered}");
41    }
42
43    // Demonstrate type conversions
44    let s: String = typed_random.clone().into();
45    println!("\nConverted to String: {s}");
46    println!("Using as_str():      {}", typed_random.as_str());
47    println!("Using AsRef<str>:    {}", typed_random.as_ref());
48}
Source

pub fn ordered() -> Self

Creates a new time-ordered short ID.

This is equivalent to calling short_id_ordered() but returns a typed ShortId. Requires the std feature (enabled by default).

§Examples
use short_id::ShortId;

let id = ShortId::ordered();
assert_eq!(id.as_str().len(), 14);
Examples found in repository?
examples/newtype.rs (line 9)
3fn main() {
4    // Create a random ID
5    let id1 = ShortId::random();
6    println!("Random ID: {}", id1);
7
8    // Create a time-ordered ID
9    let id2 = ShortId::ordered();
10    println!("Ordered ID: {}", id2);
11
12    // Access as string slice
13    let s: &str = id1.as_str();
14    println!("As str: {}", s);
15
16    // Use AsRef<str>
17    print_id(&id2);
18
19    // Convert to String
20    let string: String = id1.clone().into_string();
21    println!("Into String: {}", string);
22
23    // Create from String
24    let id3: ShortId = string.into();
25    println!("From String: {}", id3);
26
27    // Compare IDs (PartialEq, Ord)
28    let id4 = ShortId::random();
29    let id5 = ShortId::random();
30    println!("\nIDs are equal: {}", id4 == id5);
31    println!("ID4 < ID5: {}", id4 < id5);
32}
More examples
Hide additional examples
examples/basic.rs (line 39)
5fn main() {
6    println!("=== Functions ===");
7
8    // Generate a random ID using the function
9    let random_id = short_id();
10    println!("short_id():         {random_id}");
11
12    // Generate a time-ordered ID using the function
13    #[cfg(feature = "std")]
14    {
15        let ordered = short_id_ordered();
16        println!("short_id_ordered(): {ordered}");
17    }
18
19    println!("\n=== Macros ===");
20
21    // Generate IDs using macros (convenient shorthand)
22    let macro_id = id!();
23    println!("id!():              {macro_id}");
24
25    #[cfg(feature = "std")]
26    {
27        let macro_ordered = ordered_id!();
28        println!("ordered_id!():      {macro_ordered}");
29    }
30
31    println!("\n=== Typed Wrapper ===");
32
33    // Generate IDs using the ShortId type
34    let typed_random = ShortId::random();
35    println!("ShortId::random():  {typed_random}");
36
37    #[cfg(feature = "std")]
38    {
39        let typed_ordered = ShortId::ordered();
40        println!("ShortId::ordered(): {typed_ordered}");
41    }
42
43    // Demonstrate type conversions
44    let s: String = typed_random.clone().into();
45    println!("\nConverted to String: {s}");
46    println!("Using as_str():      {}", typed_random.as_str());
47    println!("Using AsRef<str>:    {}", typed_random.as_ref());
48}
Source

pub fn as_str(&self) -> &str

Returns the ID as a string slice.

§Examples
use short_id::ShortId;

let id = ShortId::random();
let s: &str = id.as_str();
assert_eq!(s.len(), 14);
Examples found in repository?
examples/newtype.rs (line 13)
3fn main() {
4    // Create a random ID
5    let id1 = ShortId::random();
6    println!("Random ID: {}", id1);
7
8    // Create a time-ordered ID
9    let id2 = ShortId::ordered();
10    println!("Ordered ID: {}", id2);
11
12    // Access as string slice
13    let s: &str = id1.as_str();
14    println!("As str: {}", s);
15
16    // Use AsRef<str>
17    print_id(&id2);
18
19    // Convert to String
20    let string: String = id1.clone().into_string();
21    println!("Into String: {}", string);
22
23    // Create from String
24    let id3: ShortId = string.into();
25    println!("From String: {}", id3);
26
27    // Compare IDs (PartialEq, Ord)
28    let id4 = ShortId::random();
29    let id5 = ShortId::random();
30    println!("\nIDs are equal: {}", id4 == id5);
31    println!("ID4 < ID5: {}", id4 < id5);
32}
More examples
Hide additional examples
examples/basic.rs (line 46)
5fn main() {
6    println!("=== Functions ===");
7
8    // Generate a random ID using the function
9    let random_id = short_id();
10    println!("short_id():         {random_id}");
11
12    // Generate a time-ordered ID using the function
13    #[cfg(feature = "std")]
14    {
15        let ordered = short_id_ordered();
16        println!("short_id_ordered(): {ordered}");
17    }
18
19    println!("\n=== Macros ===");
20
21    // Generate IDs using macros (convenient shorthand)
22    let macro_id = id!();
23    println!("id!():              {macro_id}");
24
25    #[cfg(feature = "std")]
26    {
27        let macro_ordered = ordered_id!();
28        println!("ordered_id!():      {macro_ordered}");
29    }
30
31    println!("\n=== Typed Wrapper ===");
32
33    // Generate IDs using the ShortId type
34    let typed_random = ShortId::random();
35    println!("ShortId::random():  {typed_random}");
36
37    #[cfg(feature = "std")]
38    {
39        let typed_ordered = ShortId::ordered();
40        println!("ShortId::ordered(): {typed_ordered}");
41    }
42
43    // Demonstrate type conversions
44    let s: String = typed_random.clone().into();
45    println!("\nConverted to String: {s}");
46    println!("Using as_str():      {}", typed_random.as_str());
47    println!("Using AsRef<str>:    {}", typed_random.as_ref());
48}
Source

pub fn into_string(self) -> String

Consumes the ShortId and returns the inner String.

§Examples
use short_id::ShortId;

let id = ShortId::random();
let s: String = id.into_string();
assert_eq!(s.len(), 14);
Examples found in repository?
examples/newtype.rs (line 20)
3fn main() {
4    // Create a random ID
5    let id1 = ShortId::random();
6    println!("Random ID: {}", id1);
7
8    // Create a time-ordered ID
9    let id2 = ShortId::ordered();
10    println!("Ordered ID: {}", id2);
11
12    // Access as string slice
13    let s: &str = id1.as_str();
14    println!("As str: {}", s);
15
16    // Use AsRef<str>
17    print_id(&id2);
18
19    // Convert to String
20    let string: String = id1.clone().into_string();
21    println!("Into String: {}", string);
22
23    // Create from String
24    let id3: ShortId = string.into();
25    println!("From String: {}", id3);
26
27    // Compare IDs (PartialEq, Ord)
28    let id4 = ShortId::random();
29    let id5 = ShortId::random();
30    println!("\nIDs are equal: {}", id4 == id5);
31    println!("ID4 < ID5: {}", id4 < id5);
32}

Trait Implementations§

Source§

impl AsRef<str> for ShortId

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for ShortId

Source§

fn clone(&self) -> ShortId

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ShortId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ShortId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<ShortId> for String

Source§

fn from(id: ShortId) -> Self

Converts to this type from the input type.
Source§

impl From<String> for ShortId

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl Hash for ShortId

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for ShortId

Source§

fn cmp(&self, other: &ShortId) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for ShortId

Source§

fn eq(&self, other: &ShortId) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for ShortId

Source§

fn partial_cmp(&self, other: &ShortId) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for ShortId

Source§

impl StructuralPartialEq for ShortId

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V