Skip to main content

nanoid

Macro nanoid 

Source
macro_rules! nanoid {
    ($id:expr $(, $alphabet:ty)? $(,)?) => { ... };
}
Expand description

Parse Nanoids from strings at compile time.

This macro transforms a constant string into Nanoid at compile time. If the provided string is not a valid Nano ID, the program will not compile.

§Arguments

  • $id: The Nano ID string.
  • $alphabet: The alphabet used in the Nano ID. The default is Base64UrlAlphabet.

§Examples

use nid::{alphabet::Base62Alphabet, nanoid, Nanoid};

let id1 = nanoid!("F6JA-LPEbPpz71qxDjaId");
const ID1: Nanoid = nanoid!("F6JA-LPEbPpz71qxDjaId");

// With a different length.
let id2 = nanoid!("P2_LONIp4S");
const ID2: Nanoid<10> = nanoid!("P2_LONIp4S");

// With a different alphabet.
let id3 = nanoid!("F6JAzLPEbPpz71qxDjaId", Base62Alphabet);
const ID3: Nanoid<21, Base62Alphabet> = nanoid!("F6JAzLPEbPpz71qxDjaId", Base62Alphabet);

§Compilation errors

If the provided string is not a valid Nano ID, the program will not compile.

use nid::nanoid;
let id = nanoid!("abc###"); // Compilation error: the provided string has invalid character