#random #fake-data #rand #data #fake

faker_rand

用于生成 lorem ipsum、姓名、电子邮件等假数据的生成器

2 个版本

0.1.1 2021年1月17日
0.1.0 2021年1月17日

#15#fake-data

Download history 314/week @ 2024-01-18 170/week @ 2024-01-25 96/week @ 2024-02-01 152/week @ 2024-02-08 76/week @ 2024-02-15 188/week @ 2024-02-22 81/week @ 2024-02-29 157/week @ 2024-03-07 100/week @ 2024-03-14 117/week @ 2024-03-21 179/week @ 2024-03-28 185/week @ 2024-04-04 119/week @ 2024-04-11 106/week @ 2024-04-18 2068/week @ 2024-04-25 2307/week @ 2024-05-02

4,610 每月下载量
3 crates 中使用

MIT 许可证

49KB
351

faker_rand Crates.io Docs.rs

faker_rand 是一个 Rust 包,允许您使用 rand 包轻松生成假数据。它还提供了宏,以便您可以轻松地基于这些内置的生成器构建自己的数据生成器。

安装

您可以通过在您的 Cargo.toml 中添加以下内容来在您的 Rust 项目中使用 faker_rand

faker_rand = "0.1"

使用

请参阅 docs.rs 上的文档 以获取更多详细信息,但大致上,以下是使用此包的方法

use rand::Rng;
use faker_rand::en_us::names::FirstName;

// you can display generators using "{}"
println!("random first name: {}", rand::random::<FirstName>());
println!("random first name: {}", rand::thread_rng().gen::<FirstName>());

// or, you can use to_string as well
let name = rand::random::<FirstName>().to_string();
println!("random first name: {}", name);

您也可以基于此包提供的生成器构建自己的生成器,例如,如果您已经有一个要生成的假数据文件

use faker_rand::faker_impl_from_file;

// First, declare your newtype wrapper around String.
struct Demo(String);

// Then, use the macro. data/lorem_words is a path to a file containing
// example words; you will need to change this path to suit your needs.
faker_impl_from_file!(Demo, "data/lorem_words");

use rand::{Rng, SeedableRng};
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0);

assert_eq!("impedit", rng.gen::<Demo>().to_string());

或者,如果您想要在子生成器上构建

use faker_rand::faker_impl_from_templates;

// First, declare your newtype wrapper around String.
struct Demo(String);

// Then, invoke the macro.
//
// Note well: all commas and semicolons in this example, even trailing
// semicolons, are strictly required.
faker_impl_from_templates! {
    // The type we're creating a generator implementation for.
    Demo;

    // The template patterns.
    "{}.{}", faker_rand::util::AsciiDigit, faker_rand::lorem::Word;
    "{} ~~~ {}", faker_rand::lorem::Word, faker_rand::util::AsciiDigit;
}

use rand::{Rng, SeedableRng};
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0);

assert_eq!("qui ~~~ 5", rng.gen::<Demo>().to_string());
assert_eq!("debitis ~~~ 5", rng.gen::<Demo>().to_string());
assert_eq!("5.aliquid", rng.gen::<Demo>().to_string());
assert_eq!("0.doloribus", rng.gen::<Demo>().to_string());

此包的大部分文档位于 Rust 文档中,而不是本 README。请查看 https://docs.rs/faker_rand 以获取可复制示例。

依赖项

~500KB