#vec #compare #struct #debugging

compare_fields

Field-by-field comparisons for structs and vecs

2 releases

Uses new Rust 2024

new 0.1.1 Jan 14, 2026
0.1.0 Oct 11, 2025

#2531 in Algorithms

Download history 194/week @ 2025-10-08 20/week @ 2025-10-15 127/week @ 2025-10-22 1489/week @ 2025-10-29 1497/week @ 2025-11-05 710/week @ 2025-11-12 704/week @ 2025-11-19 1890/week @ 2025-11-26 1472/week @ 2025-12-03 1571/week @ 2025-12-10 1011/week @ 2025-12-17 188/week @ 2025-12-24 799/week @ 2025-12-31 1158/week @ 2026-01-07

3,557 downloads per month

Apache-2.0

8KB
100 lines

compare_fields

CI Crates.io Documentation

Field-by-field comparisons for structs and vecs.

Returns comparisons as data, without making assumptions about the desired equality (e.g., does not panic! on inequality).

See the documentation for usage examples.


lib.rs:

Provides field-by-field comparisons for structs and vecs.

Returns comparisons as data, without making assumptions about the desired equality (e.g., does not panic! on inequality).

Note: compare_fields_derive requires PartialEq and Debug implementations.

Example

use compare_fields::{CompareFields, Comparison, FieldComparison};

#[derive(PartialEq, Debug, CompareFields)]
pub struct Bar {
    a: u64,
    b: u16,
    #[compare_fields(as_slice)]
    c: Vec<Foo>
}

#[derive(Clone, PartialEq, Debug, CompareFields)]
pub struct Foo {
    d: String
}

let cat = Foo {d: "cat".to_string()};
let dog = Foo {d: "dog".to_string()};
let chicken = Foo {d: "chicken".to_string()};

let mut bar_a = Bar {
    a: 42,
    b: 12,
    c: vec![ cat.clone(), dog.clone() ],
};

let mut bar_b = Bar {
    a: 42,
    b: 99,
    c: vec![ chicken.clone(), dog.clone()]
};

let cat_dog = Comparison::Child(FieldComparison {
    field_name: "d".to_string(),
    equal: false,
    a: "\"cat\"".to_string(),
    b: "\"dog\"".to_string(),
});
assert_eq!(cat.compare_fields(&dog), vec![cat_dog]);

let bar_a_b = vec![
    Comparison::Child(FieldComparison {
        field_name: "a".to_string(),
        equal: true,
        a: "42".to_string(),
        b: "42".to_string(),
    }),
    Comparison::Child(FieldComparison {
        field_name: "b".to_string(),
        equal: false,
        a: "12".to_string(),
        b: "99".to_string(),
    }),
    Comparison::Parent{
        field_name: "c".to_string(),
        equal: false,
        children: vec![
            FieldComparison {
                field_name: "0".to_string(),
                equal: false,
                a: "Some(Foo { d: \"cat\" })".to_string(),
                b: "Some(Foo { d: \"chicken\" })".to_string(),
            },
            FieldComparison {
                field_name: "1".to_string(),
                equal: true,
                a: "Some(Foo { d: \"dog\" })".to_string(),
                b: "Some(Foo { d: \"dog\" })".to_string(),
            }
        ]
    }
];
assert_eq!(bar_a.compare_fields(&bar_b), bar_a_b);

Dependencies

~555KB
~11K SLoC