The initial implementation of the RFC 2011 has been merged
https://github.com/rust-lang/rust/pull/97665
Partially alleviates the lack of built-in helpers like assert_greater_than or assert_less_than but the overall motivation is improved readability and developer experience.
Please test the feature as much as possible to report any underlying issue. If everything goes well and performance is not impacted, assert_eq! and assert_ne! will be deprecated in favor of this new assert! according to the RFC.
The initial implementation only catches binary operations but soon the following snippet will be possible.
#![feature(generic_assert)]
enum SomeEnum {
Foo,
Bar
}
fn fancy_stuff(some_str: &str) -> bool {
!some_str.is_empty()
}
fn some_function(some_str: &str, some_enum: SomeEnum, some_number: i32) {
assert!(fancy_stuff(some_str) && matches!(some_enum, SomeEnum::Foo) && some_number <= 99);
// ...
// ...
// ...
}
fn main() {
some_function("I may be wrong but it is highly unlikely", SomeEnum::Foo, 101);
}
Assertion failed: fancy_stuff(some_str) && matches!(some_enum, SomeEnum::Foo) && some_number <= 99
With captures:
some_str = "I may be wrong but it is highly unlikely"
some_enum = SomeEnum::Foo
some_number = 101