Rust数据模拟库fakedata_generator的使用:快速生成测试数据的Rust插件库
Rust数据模拟库fakedata_generator的使用:快速生成测试数据的Rust插件库
关于
这个库提供了生成随机值(“假数据”)的功能。它还处于早期阶段,有些值还没有完全随机化。基本文档如下。
使用
将库作为依赖项添加到你的Cargo.toml
中。
[dependencies]
fakedata_generator = "0.4"
现在可以用use fakedata_generator::*
加载库。
extern crate fakedata_generator;
use fakedata_generator::*;
fn main() {
let random_word = gen_enum("some,random,words".to_string());
println!("Random word is: {}", random_word);
}
生成器
无参数生成器
返回一个随机电子邮件地址,它是用户名和域名生成器的组合。
函数签名
gen_email() -> String
示例调用
let email: String = gen_email();
// email = shaneIxD@we.net
username
返回一个随机用户名。
函数签名
gen_username() -> String
示例调用
let user: String = gen_username();
// user = ahmadajmi
domain
返回一个随机域名。
函数签名
gen_domain() -> String
示例调用
let domain: String = gen_domain();
// domain = "names.us"
http method
从定义的列表中返回一个随机HTTP方法。
函数签名
gen_http_method() -> String
示例调用
let method: String = gen_http_method();
// method = "GET"
ipv4
返回一个随机IP地址。
函数签名
gen_ipv4() -> String
示例调用
let ip: String = gen_ipv4();
// ip = "168.11.40.75"
gen_prime
随机返回前1000个素数之一。
函数签名
gen_prime() -> usize
示例调用
let prime: usize = gen_prime();
// prime = 6323
有参数生成器
enum
从指定的字符串集合中返回随机字符串。
函数签名
gen_enum(input: String) -> String
示例调用
let word: String = gen_enum("hello,hola,hallo".to_string());
// word = "hola"
int
返回范围内的随机整数。
函数签名
gen_int(input: String) -> String
示例调用
let num: String = gen_enum("1,100".to_string());
// num = "42"
private ipv4
在以下3个范围内创建私有IPv4地址。
函数签名
gen_private_ip(input: usize) -> String
示例调用
let private_ipv4: String = gen_private_ipv4(10);
// num = 10.64.197.255
passwords
无特殊字符
创建一个随机字符串。
函数签名
gen_password(input: usize) -> String
示例调用
let pw: String = gen_password(32);
// pw = "bNNpAmShvQYbKbMdhByK17lqaFcgarrF"
有特殊字符
创建一个带特殊字符的随机字符串。
函数签名
gen_password_with_special_chars(input: usize) -> String
示例调用
let pw: String = gen_password_with_special_chars(32);
// pw = "F=>:e+KX;Uu/Zg#i*MQN//6r%a^K?K°0"
Corpora生成器
gen_switch
函数从Corpora项目中获取JSON格式的数据。
函数签名
gen_switch(input: String) -> String
示例调用
let word: String = gen_switch("cat".to_string());
// word = "European Shorthair"
let fabric: String = gen_switch("fabric".to_string());
// word = "longcloth"
完整示例代码
extern crate fakedata_generator;
use fakedata_generator::*;
fn main() {
// 生成随机邮箱
let email = gen_email();
println!("生成的随机邮箱: {}", email);
// 生成随机用户名
let username = gen_username();
println!("生成的随机用户名: {}", username);
// 生成随机域名
let domain = gen_domain();
println!("生成的随机域名: {}", domain);
// 生成随机HTTP方法
let http_method = gen_http_method();
println!("生成的随机HTTP方法: {}", http_method);
// 生成随机IPv4地址
let ipv4 = gen_ipv4();
println!("生成的随机IPv4地址: {}", ipv4);
// 生成随机素数
let prime = gen_prime();
println!("生成的随机素数: {}", prime);
// 从给定选项中随机选择
let choice = gen_enum("选项A,选项B,选项C".to_string());
println!("随机选择的选项: {}", choice);
// 生成1-100范围内的随机整数
let random_num = gen_int("1,100".to_string());
println!("1-100范围内的随机数: {}", random_num);
// 生成私有IPv4地址
let private_ip = gen_private_ipv4(10);
println!("生成的私有IPv4地址: {}", private_ip);
// 生成16位随机密码(无特殊字符)
let password = gen_password(16);
println!("16位随机密码(无特殊字符): {}", password);
// 生成16位随机密码(含特殊字符)
let special_password = gen_password_with_special_chars(16);
println!("16位随机密码(含特殊字符): {}", special_password);
// 从Corpora数据集中获取随机值
let animal = gen_switch("cat".to_string());
println!("随机动物种类: {}", animal);
}
1 回复