3 个版本
0.1.2 | 2024年2月23日 |
---|---|
0.1.1 | 2024年2月23日 |
0.1.0 | 2024年2月23日 |
#293 in 数据结构
2,083 每月下载量
24KB
359 行
short-uuid
将标准 UUID 生成并转换为更短的或不同格式的格式,并返回。
JavaScript npm 包 short-uuid 的端口,感谢作者。
默认 flickrBase58 字母表中的短 uuid 字符串示例
mhvXdrZT4jP5T8vBxuvm75
入门
使用 cargo
安装包
cargo add short-uuid
或将它添加到你的 Cargo.toml
[dependencies]
short-uuid = "0.1.0"
示例
生成 flickrBase58 格式编码的短 uuidv4
use short_uuid::ShortUuid;
let shortened_uuid = ShortUuid::generate();
使用宏生成 flickrBase58 格式编码的短 uuidv4
use short_uuid::short;
let shortened_uuid = short!();
使用自定义字母表生成短 uuidv4
use short_uuid::{ShortUuidCustom, CustomTranslator};
let custom_alphabet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
let translator = CustomTranslator::new(custom_alphabet).unwrap();
let custom_short = ShortUuidCustom::generate(&translator);
let custom_short_string = custom_short.to_string();
从标准 uuid 获取缩短的 uuid
use short_uuid::ShortUuid;
// create normal uuid v4
let uuid = uuid::Uuid::new_v4();
let short = ShortUuid::from_uuid(&uuid);
使用自定义字母表从标准 uuid 获取缩短的 uuid
use short_uuid::{ShortUuidCustom, CustomTranslator};
let custom_alphabet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
let translator = CustomTranslator::new(custom_alphabet).unwrap();
let uuid = uuid::Uuid::new_v4();
let short_custom = ShortUuidCustom::from_uuid(&uuid, &translator);
let short_custom_string = short_custom.to_string();
从 uuid 字符串获取缩短的 uuid
use short_uuid::ShortUuid;
let uuid_str = "3cfb46e7-c391-42ef-90b8-0c1d9508e752";
let short_uuid = ShortUuid::from_uuid_str(&uuid_str);
使用自定义字母表从 uuid 字符串获取缩短的 uuid
use short_uuid::{ShortUuidCustom, CustomTranslator};
let custom_alphabet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
let translator = CustomTranslator::new(custom_alphabet).unwrap();
let uuid_str = "3cfb46e7-c391-42ef-90b8-0c1d9508e752";
let short_custom = ShortUuidCustom::from_uuid_str(&uuid_str, &translator).unwrap();
let short_custom_string = short_custom.to_string();
参考
依赖项
~225KB