### Get default random bytes Source: https://docs.rs/nanoid/0.4.0/nanoid/rngs/fn.default.html Generates a vector of random bytes using the default random number generator. Specify the desired size of the byte vector. ```rust pub fn default(size: usize) -> Vec ⓘ ``` -------------------------------- ### Add nanoid dependency Source: https://docs.rs/nanoid/0.4.0/nanoid/index.html Include the nanoid crate in your Cargo.toml file. ```toml [dependencies] nanoid = "0.4.0" ``` -------------------------------- ### nanoid Library API Source: https://docs.rs/nanoid/0.4.0/nanoid/all.html Overview of the available macros, functions, and constants provided by the nanoid 0.4.0 crate. ```APIDOC ## Macros - **nanoid** - Generates a unique ID. ## Functions - **format** - Formats a unique ID. - **rngs::default** - Provides the default random number generator. - **rngs::non_secure** - Provides a non-secure random number generator. ## Constants - **alphabet::SAFE** - A constant representing a safe character set for ID generation. ``` -------------------------------- ### format Function API Source: https://docs.rs/nanoid/0.4.0/nanoid/fn.format.html Documentation for the `format` function, which generates a nanoid string. ```APIDOC ## format Function API ### Description Generates a nanoid string using a provided random byte generator, alphabet, and size. ### Function Signature ```rust pub fn format( random: fn(usize) -> Vec, alphabet: &[char], size: usize, ) -> String ``` ### Parameters #### `random` - `fn(usize) -> Vec` - Required - A function that takes a size and returns a vector of random bytes. #### `alphabet` - `&[char]` - Required - A slice of characters to use as the alphabet for the nanoid. #### `size` - `usize` - Required - The desired length of the nanoid string. ### Returns - `String` - The generated nanoid string. ``` -------------------------------- ### Nanoid Crate Usage Source: https://docs.rs/nanoid/0.4.0/nanoid/index.html This section covers the basic usage of the nanoid crate for generating unique IDs. ```APIDOC ## Crate nanoid A tiny, secure, URL-friendly, unique string ID generator. **Safe.** It uses cryptographically strong random APIs and guarantees a proper distribution of symbols. **Compact.** It uses a larger alphabet than UUID (`A-Za-z0-9_~`) and has a similar number of unique IDs in just 21 symbols instead of 36. ### Dependencies ``` [dependencies] nanoid = "0.4.0" ``` ### Basic Usage ```rust use nanoid::nanoid; fn main() { let id = nanoid!(); //=> "Yo1Tr9F3iF-LFHX9i9GvA" } ``` ``` -------------------------------- ### Custom Length ID Generation Source: https://docs.rs/nanoid/0.4.0/nanoid/index.html Demonstrates how to generate IDs with a custom length. ```APIDOC ### Custom Length If you want to reduce ID length (and increase collisions probability), you can pass the length as an argument to the generate function: ```rust use nanoid::nanoid; fn main() { let id = nanoid!(10); //=> "IRFa~VaY2b" } ``` ``` -------------------------------- ### Custom Alphabet or Length Source: https://docs.rs/nanoid/0.4.0/nanoid/index.html Shows how to use a custom alphabet and length for ID generation. ```APIDOC ### Custom Alphabet or Length If you want to change the ID’s alphabet or length you can use the low-level `custom` module. ```rust use nanoid::nanoid; fn main() { let alphabet: [char; 16] = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f' ]; let id = nanoid!(10, &alphabet); //=> "4f90d13a42" } ``` Alphabet must contain 256 symbols or less. Otherwise, the generator will not be secure. ``` -------------------------------- ### Generate a simple ID Source: https://docs.rs/nanoid/0.4.0/nanoid/index.html Generate a 21-character URL-friendly unique ID. ```rust use nanoid::nanoid; fn main() { let id = nanoid!(); //=> "Yo1Tr9F3iF-LFHX9i9GvA" } ``` -------------------------------- ### Custom Random Bytes Generator Source: https://docs.rs/nanoid/0.4.0/nanoid/index.html Explains how to replace the default random generator with a custom one. ```APIDOC ### Custom Random Bytes Generator You can replace the default safe random generator using the `complex` module. For instance, to use a seed-based generator. ```rust use nanoid::nanoid; fn random_byte () -> u8 { 0 } fn main() { fn random (size: usize) -> Vec { let mut bytes: Vec = vec![0; size]; for i in 0..size { bytes[i] = random_byte(); } bytes } nanoid!(10, &['a', 'b', 'c', 'd', 'e', 'f'], random); //=> "fbaefaadeb" } ``` `random` function must accept the array size and return a vector with random numbers. If you want to use the same URL-friendly symbols with `format`, you can get the default alphabet from the `url` module: ```rust use nanoid::nanoid; fn random (size: usize) -> Vec { let result: Vec = vec![0; size]; result } fn main() { nanoid!(10, &nanoid::alphabet::SAFE, random); //=> "93ce_Ltuub" } ``` ``` -------------------------------- ### Constants - SAFE Source: https://docs.rs/nanoid/0.4.0/nanoid/alphabet/index.html Documentation for the SAFE constant within the alphabet module. ```APIDOC ## Constant: SAFE ### Description Represents the set of URL-safe symbols used for generating unique IDs. ### Value - **SAFE** (string) - A predefined set of characters that are safe for use in URLs. ``` -------------------------------- ### Generate an ID with custom alphabet and length Source: https://docs.rs/nanoid/0.4.0/nanoid/index.html Use the custom module to define a specific alphabet and length. The alphabet must contain 256 symbols or less for security. ```rust use nanoid::nanoid; fn main() { let alphabet: [char; 16] = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f' ]; let id = nanoid!(10, &alphabet); //=> "4f90d13a42" } ``` -------------------------------- ### nanoid Macro Definition Source: https://docs.rs/nanoid/0.4.0/nanoid/macro.nanoid.html The macro supports varying arguments for size, alphabet, and random number generator customization. ```rust macro_rules! nanoid { () => { ... }; ($size:tt) => { ... }; ($size:tt, $alphabet:expr) => { ... }; ($size:tt, $alphabet:expr, $random:expr) => { ... }; } ``` -------------------------------- ### nanoid Macro Source: https://docs.rs/nanoid/0.4.0/nanoid/macro.nanoid.html The nanoid macro allows for the generation of unique IDs. It supports optional parameters for size, alphabet, and a custom random number generator. ```APIDOC ## Macro nanoid ### Description Generates a unique string ID. The macro can be invoked with different levels of configuration. ### Syntax - `nanoid!()` - Generates an ID with default settings. - `nanoid!($size)` - Generates an ID with a specific length. - `nanoid!($size, $alphabet)` - Generates an ID with a specific length and custom alphabet. - `nanoid!($size, $alphabet, $random)` - Generates an ID with a specific length, custom alphabet, and custom random number generator. ### Parameters - **$size** (tt) - Optional - The length of the generated ID. - **$alphabet** (expr) - Optional - The character set to use for the ID. - **$random** (expr) - Optional - A custom random number generator implementation. ``` -------------------------------- ### Generate ID with SAFE Alphabet Source: https://docs.rs/nanoid/0.4.0/nanoid/alphabet/constant.SAFE.html Use the SAFE alphabet to generate a nanoid of a specified length. This is the default alphabet for nanoid. ```rust let id = nanoid::nanoid!(10, &nanoid::alphabet::SAFE); ``` -------------------------------- ### Use a custom random bytes generator Source: https://docs.rs/nanoid/0.4.0/nanoid/index.html Replace the default random generator by providing a custom function that returns a vector of random bytes. ```rust use nanoid::nanoid; fn random_byte () -> u8 { 0 } fn main() { fn random (size: usize) -> Vec { let mut bytes: Vec = vec![0; size]; for i in 0..size { bytes[i] = random_byte(); } bytes } nanoid!(10, &['a', 'b', 'c', 'd', 'e', 'f'], random); //=> "fbaefaadeb" } ``` ```rust use nanoid::nanoid; fn random (size: usize) -> Vec { let result: Vec = vec![0; size]; result } fn main() { nanoid!(10, &nanoid::alphabet::SAFE, random); //=> "93ce_Ltuub" } ``` -------------------------------- ### Generate an ID with custom length Source: https://docs.rs/nanoid/0.4.0/nanoid/index.html Specify the length of the generated ID as an argument. ```rust use nanoid::nanoid; fn main() { let id = nanoid!(10); //=> "IRFa~VaY2b" } ``` -------------------------------- ### Nanoid RNG Functions Source: https://docs.rs/nanoid/0.4.0/nanoid/rngs/index.html Provides access to default and non-secure random number generators for nanoid. ```APIDOC ## Module rngs ### Functions #### `default()` - **Description**: Returns the default random number generator. - **Usage**: `nanoid::random::default()` #### `non_secure()` - **Description**: Returns a non-secure random number generator. Use with caution. - **Usage**: `nanoid::random::non_secure()` ``` -------------------------------- ### Function: non_secure Source: https://docs.rs/nanoid/0.4.0/nanoid/rngs/fn.non_secure.html Generates a vector of random bytes using a non-secure random number generator. ```APIDOC ## Function: non_secure ### Description Generates a vector of random bytes of a specified size using a non-cryptographically secure random number generator. ### Signature `pub fn non_secure(size: usize) -> Vec` ### Parameters - **size** (usize) - Required - The number of bytes to generate. ### Returns - **Vec** - A vector containing the generated random bytes. ``` -------------------------------- ### nanoid::alphabet::SAFE Source: https://docs.rs/nanoid/0.4.0/nanoid/alphabet/constant.SAFE.html The SAFE constant is an array of 64 characters that are safe for use in URLs, serving as the default alphabet for nanoid generation. ```APIDOC ## Constant: nanoid::alphabet::SAFE ### Description URL safe symbols. An array of characters which can be safely used in URLs. This is the default alphabet used by the nanoid function. ### Definition `pub const SAFE: [char; 64];` ### Usage Example ```rust let id = nanoid::nanoid!(10, &nanoid::alphabet::SAFE); ``` ``` -------------------------------- ### nanoid::rngs::default Function Source: https://docs.rs/nanoid/0.4.0/nanoid/rngs/fn.default.html The `default` function in `nanoid::rngs` generates a vector of bytes of a specified size using the default random number generator. This is a core utility for creating unique IDs. ```APIDOC ## default nanoid::rngs ### Function default Generates a vector of bytes of a specified size using the default random number generator. ### Signature ```rust pub fn default(size: usize) -> Vec ``` ### Parameters - **size** (usize) - The desired size of the byte vector to generate. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.