@@ -119,6 +119,44 @@ macro_rules! assert_eq {
119
119
} ) ;
120
120
}
121
121
122
+ /// Asserts that two expressions are not equal to each other.
123
+ ///
124
+ /// On panic, this macro will print the values of the expressions with their
125
+ /// debug representations.
126
+ ///
127
+ /// # Examples
128
+ ///
129
+ /// ```
130
+ /// let a = 3;
131
+ /// let b = 2;
132
+ /// assert_ne!(a, b);
133
+ /// ```
134
+ #[ macro_export]
135
+ #[ stable( feature = "assert_ne" , since = "1.12.0" ) ]
136
+ macro_rules! assert_ne {
137
+ ( $left: expr , $right: expr) => ( {
138
+ match ( & $left, & $right) {
139
+ ( left_val, right_val) => {
140
+ if * left_val == * right_val {
141
+ panic!( "assertion failed: `(left != right)` \
142
+ (left: `{:?}`, right: `{:?}`)", left_val, right_val)
143
+ }
144
+ }
145
+ }
146
+ } ) ;
147
+ ( $left: expr , $right: expr, $( $arg: tt) * ) => ( {
148
+ match ( & ( $left) , & ( $right) ) {
149
+ ( left_val, right_val) => {
150
+ if * left_val == * right_val {
151
+ panic!( "assertion failed: `(left != right)` \
152
+ (left: `{:?}`, right: `{:?}`): {}", left_val, right_val,
153
+ format_args!( $( $arg) * ) )
154
+ }
155
+ }
156
+ }
157
+ } ) ;
158
+ }
159
+
122
160
/// Ensure that a boolean expression is `true` at runtime.
123
161
///
124
162
/// This will invoke the `panic!` macro if the provided expression cannot be
@@ -189,6 +227,31 @@ macro_rules! debug_assert_eq {
189
227
( $( $arg: tt) * ) => ( if cfg!( debug_assertions) { assert_eq!( $( $arg) * ) ; } )
190
228
}
191
229
230
+ /// Asserts that two expressions are not equal to each other.
231
+ ///
232
+ /// On panic, this macro will print the values of the expressions with their
233
+ /// debug representations.
234
+ ///
235
+ /// Unlike `assert_ne!`, `debug_assert_ne!` statements are only enabled in non
236
+ /// optimized builds by default. An optimized build will omit all
237
+ /// `debug_assert_ne!` statements unless `-C debug-assertions` is passed to the
238
+ /// compiler. This makes `debug_assert_ne!` useful for checks that are too
239
+ /// expensive to be present in a release build but may be helpful during
240
+ /// development.
241
+ ///
242
+ /// # Examples
243
+ ///
244
+ /// ```
245
+ /// let a = 3;
246
+ /// let b = 2;
247
+ /// debug_assert_ne!(a, b);
248
+ /// ```
249
+ #[ macro_export]
250
+ #[ stable( feature = "assert_ne" , since = "1.12.0" ) ]
251
+ macro_rules! debug_assert_ne {
252
+ ( $( $arg: tt) * ) => ( if cfg!( debug_assertions) { assert_ne!( $( $arg) * ) ; } )
253
+ }
254
+
192
255
/// Helper macro for reducing boilerplate code for matching `Result` together
193
256
/// with converting downstream errors.
194
257
///
0 commit comments