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
impl ShortId
Sourcepub fn random() -> Self
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
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}Sourcepub fn ordered() -> Self
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
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}Sourcepub fn as_str(&self) -> &str
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
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}Sourcepub fn into_string(self) -> String
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 Ord for ShortId
impl Ord for ShortId
Source§impl PartialOrd for ShortId
impl PartialOrd for ShortId
impl Eq for ShortId
impl StructuralPartialEq for ShortId
Auto Trait Implementations§
impl Freeze for ShortId
impl RefUnwindSafe for ShortId
impl Send for ShortId
impl Sync for ShortId
impl Unpin for ShortId
impl UnwindSafe for ShortId
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more