Assert_eq! expected and actual


Just wondering what order expected and actual are supposed to go in the assert_eq! macro. The docs don’t say one way or the other. I know its a stupid question and in truth it really doesn’t matter which way they are listed, but I’d like to keep it standard in my tests.



It doesn’t say because they’re not “expected” and “actual”; they’re “left” and “right”.

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `0`,
 right: `1`', src/

Put them in whatever order you want. If you really can’t live with that, define your own macro that uses a different panic message.

Edit: maybe with a syntax like assert_equ!(exp: 0, got: 1) so it’s explicit.

P.S. I hate emoji. Stupid “smart” editor made it impossible to press enter after the 5 on that last line because it insisted on inserting a clock emoji. I will tell you when I want insipid tiny pictures in my post, I shall not suffer you to dictate such to me!

1 Like

I like to put the expected first, which leans towards the convention of “keep function parameters that are more known or less likely to be variable to the left”.

Something to also consider, hamcrest macros go the other way:

assert_that!(actual, contains(vec!["a", "b", "c"]));

I always write it as assert_eq!(real, expected); because that’s how I read them in my head, like: “let’s assert that <real result> is equal to <expected result>”. Rust docs also seem to use this convention consistently.

When I see them reversed it always reminds me of


I use actual, expected.

Reason 1 is convention: (actual, expected) matches the Rust documentation usage, as far as I see, and also matches the implementation of crates that I use, such as Diesel, and also matches popular testing tools in other languages that I use.

Reason 2 is extension: (actual, expected) is easier to read when I use assertions that are more than just simple equality. For example, I use assert_between!(actual, min, max), assert_in_delta!(actual, expected, delta), assert_matches!(actual, ...match expression...), etc.