14 stable releases

2.2.1 Jan 13, 2025
2.0.3 Nov 8, 2024
2.0.2 Sep 13, 2022
2.0.0 Jun 17, 2022
0.3.0 Oct 17, 2015

#17 in Web programming

Download history 144284/week @ 2024-12-05 161635/week @ 2024-12-12 65380/week @ 2024-12-19 36376/week @ 2024-12-26 138268/week @ 2025-01-02 169368/week @ 2025-01-09 125796/week @ 2025-01-16 142079/week @ 2025-01-23 140789/week @ 2025-01-30 155425/week @ 2025-02-06 139140/week @ 2025-02-13 189377/week @ 2025-02-20 186014/week @ 2025-02-27 200014/week @ 2025-03-06 175672/week @ 2025-03-13 149251/week @ 2025-03-20

759,618 downloads per month
Used in 98 crates (18 directly)

MIT license

43KB
839 lines

base62

A fast, zero-dependency base62 encoder/decoder library for Rust, typically used in URL shorteners. It supports both standard [0-9A-Za-z] and alternative [0-9a-zA-Z] variants.

Build status Crates.io Docs

Features

  • no_std compatible with optional alloc and std support
  • Encodes integers up to u128
  • Zero-copy decoding
  • Efficient string handling
  • Two encoding variants:
    • Standard [0-9A-Za-z]
    • Alternative [0-9a-zA-Z]

Usage

Add this to your Cargo.toml:

[dependencies]
base62 = "2"

Basic Example

use base62;

// Encoding
let encoded = base62::encode(1234567890);
assert_eq!(encoded, "1LY7VK");

// Decoding
let decoded = base62::decode("1LY7VK").unwrap();
assert_eq!(decoded, 1234567890);

No-std Usage

The crate works in no_std environments by default:

#![no_std]
use base62;

// Encode into a fixed buffer
let mut buf = [0u8; 22];  // Maximum size needed for u128
let len = base62::encode_bytes(1234567890, &mut buf).unwrap();
assert_eq!(&buf[..len], b"1LY7VK");

// Decode from bytes
let decoded = base62::decode(&buf[..len]).unwrap();
assert_eq!(decoded, 1234567890);

Feature Flags

  • alloc: Enables String allocation support (enabled by default)
  • std: Enables std::io traits support

Performance

The library is optimized for both encoding and decoding performance:

  • Zero-copy decoding
  • Efficient buffer management
  • Direct string manipulation for optimal performance when appending

License

Licensed under the MIT license. See LICENSE for details.

Dependencies