35 releases (18 stable)

1.3.2 Oct 30, 2023
1.3.0 Mar 13, 2023
1.2.0 Oct 20, 2022
1.1.3 Jun 23, 2022
0.1.0 May 8, 2017

#7 in Testing

Download history 182165/week @ 2024-01-28 179254/week @ 2024-02-04 172351/week @ 2024-02-11 185531/week @ 2024-02-18 185783/week @ 2024-02-25 182040/week @ 2024-03-03 187047/week @ 2024-03-10 199597/week @ 2024-03-17 198388/week @ 2024-03-24 207253/week @ 2024-03-31 212670/week @ 2024-04-07 224926/week @ 2024-04-14 226155/week @ 2024-04-21 220248/week @ 2024-04-28 233096/week @ 2024-05-05 242450/week @ 2024-05-12

934,728 downloads per month
Used in 974 crates (361 directly)


1.5K SLoC


The trait for generating structured data from arbitrary, unstructured input.

GitHub Actions Status


The Arbitrary crate lets you construct arbitrary instances of a type.

This crate is primarily intended to be combined with a fuzzer like libFuzzer and cargo-fuzz or AFL, and to help you turn the raw, untyped byte buffers that they produce into well-typed, valid, structured values. This allows you to combine structure-aware test case generation with coverage-guided, mutation-based fuzzers.


Read the API documentation on docs.rs!


Say you're writing a color conversion library, and you have an Rgb struct to represent RGB colors. You might want to implement Arbitrary for Rgb so that you could take arbitrary Rgb instances in a test function that asserts some property (for example, asserting that RGB converted to HSL and converted back to RGB always ends up exactly where we started).

Automatically Deriving Arbitrary

Automatically deriving the Arbitrary trait is the recommended way to implement Arbitrary for your types.

Automatically deriving Arbitrary requires you to enable the "derive" cargo feature:

# Cargo.toml

arbitrary = { version = "1", features = ["derive"] }

And then you can simply add #[derive(Arbitrary)] annotations to your types:

// rgb.rs

use arbitrary::Arbitrary;

pub struct Rgb {
    pub r: u8,
    pub g: u8,
    pub b: u8,

Customizing single fields

This can be particular handy if your structure uses a type that does not implement Arbitrary or you want to have more customization for particular fields.

pub struct Rgba {
    // set `r` to Default::default()
    pub r: u8,

    // set `g` to 255
    #[arbitrary(value = 255)]
    pub g: u8,

    // Generate `b` with a custom function of type
    //    fn(&mut Unstructured) -> arbitrary::Result<T>
    // where `T` is the field's type.
    #[arbitrary(with = arbitrary_b)]
    pub b: u8,

    // Generate `a` with a custom closure (shortuct to avoid a custom function)
    #[arbitrary(with = |u: &mut Unstructured| u.int_in_range(0..=64))]
    pub a: u8,

fn arbitrary_b(u: &mut Unstructured) -> arbitrary::Result<u8> {

Implementing Arbitrary By Hand

Alternatively, you can write an Arbitrary implementation by hand:

// rgb.rs

use arbitrary::{Arbitrary, Result, Unstructured};

#[derive(Copy, Clone, Debug)]
pub struct Rgb {
    pub r: u8,
    pub g: u8,
    pub b: u8,

impl<'a> Arbitrary<'a> for Rgb {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let r = u8::arbitrary(u)?;
        let g = u8::arbitrary(u)?;
        let b = u8::arbitrary(u)?;
        Ok(Rgb { r, g, b })


Licensed under dual MIT or Apache-2.0 at your choice.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.