Expand description
A direct replacement for assert_eq for unordered collections
This macro is useful for any situation where the ordering of the collection doesn’t matter, even
if they are always in the same order. This is because the stdlib assert_eq shows the entire
collection for both left and right and leaves it up to the user to visually scan for differences.
In contrast, this crate only works with collections (types that implement IntoIterator) and
therefore can show only the differences (see below for an example of what the output looks like).
§Which Macro?
TL;DR - favor assert_eq_unordered_sort unless the trait requirements can’t be met.
Use the regular versions for collections, and the *_iter versions for iterators
(or differing collection types that don’t support direct equality comparison).
- assert_eq_unordered
- Requires only
DebugandPartialEqon the elements - Collection level equality check, and if unequal, falls back to item by item compare (O(n^2))
- Requires only
- assert_eq_unordered_sort
- Requires
DebugandOrdon the elements - Collection level equality check, and if unequal, sorts and then compares again, and if still unequal, falls back to item by item compare (O(n^2))
- Requires
- assert_eq_unordered_iter
- Requires only
DebugandPartialEqon the elements - Does only item by item compare (O(n^2))
- Requires only
- assert_eq_unordered_sort_iter
- Requires
DebugandOrdon the elements - Sorts and then compares. If unequal, falls back to item by item compare (O(n^2))
- Requires
§Example
use assert_unordered::assert_eq_unordered;
#[derive(Debug, PartialEq)]
struct MyType(i32);
let expected = vec![MyType(1), MyType(2), MyType(4), MyType(5)];
let actual = vec![MyType(2), MyType(0), MyType(4)];
assert_eq_unordered!(expected, actual);Output:

Macros§
- assert_
eq_ unordered - Assert that
$leftand$rightare “unordered” equal. That is, they contain the same elements, but not necessarily in the same order. If this assertion is false, a panic is raised, and the elements that are different between$leftand$rightare shown (when possible). - assert_
eq_ unordered_ iter - The same as assert_eq_unordered, but for types that implement Iterator or IntoIterator without requiring PartialEq. The left and right sides do not need to have the same type. It will be less efficient if the collections are equal, as it skips the initial equality check.
- assert_
eq_ unordered_ sort - Assert that
$leftand$rightare “unordered” equal. That is, they contain the same elements, but not necessarily in the same order. If this assertion is false, a panic is raised, and the elements that are different between$leftand$rightare shown (when possible). - assert_
eq_ unordered_ sort_ iter - The same as assert_eq_unordered_sort, but for types that implement Iterator or IntoIterator without requiring PartialEq. The left and right sides do not need to have the same type. It will be less efficient if the collections are equal, as it skips the initial equality check.