Skip to content

Commit 7ec29c0

Browse files
authored
Unrolled build for rust-lang#121732
Rollup merge of rust-lang#121732 - Voultapher:improve-assert_matches-documentation, r=cuviper Improve assert_matches! documentation This new documentation tries to limit the impact of the conceptual pitfall, that the if guard relaxes the constraint, when really it tightens it. This is achieved by changing the text and examples. The previous documentation also chose a rather weird and non-representative example for the if guard, that made it needlessly complicated to understand.
2 parents d18480b + c45f0a9 commit 7ec29c0

File tree

1 file changed

+58
-32
lines changed

1 file changed

+58
-32
lines changed

library/core/src/macros/mod.rs

+58-32
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,19 @@ macro_rules! assert_ne {
112112
};
113113
}
114114

115-
/// Asserts that an expression matches any of the given patterns.
115+
/// Asserts that an expression matches the provided pattern.
116116
///
117-
/// Like in a `match` expression, the pattern can be optionally followed by `if`
118-
/// and a guard expression that has access to names bound by the pattern.
117+
/// This macro is generally preferable to `assert!(matches!(value, pattern))`, because it can print
118+
/// the debug representation of the actual value shape that did not meet expectations. In contrast,
119+
/// using [`assert!`] will only print that expectations were not met, but not why.
119120
///
120-
/// On panic, this macro will print the value of the expression with its
121-
/// debug representation.
121+
/// The pattern syntax is exactly the same as found in a match arm and the `matches!` macro. The
122+
/// optional if guard can be used to add additional checks that must be true for the matched value,
123+
/// otherwise this macro will panic.
122124
///
123-
/// Like [`assert!`], this macro has a second form, where a custom
124-
/// panic message can be provided.
125+
/// On panic, this macro will print the value of the expression with its debug representation.
126+
///
127+
/// Like [`assert!`], this macro has a second form, where a custom panic message can be provided.
125128
///
126129
/// # Examples
127130
///
@@ -130,13 +133,20 @@ macro_rules! assert_ne {
130133
///
131134
/// use std::assert_matches::assert_matches;
132135
///
133-
/// let a = 1u32.checked_add(2);
134-
/// let b = 1u32.checked_sub(2);
136+
/// let a = Some(345);
137+
/// let b = Some(56);
135138
/// assert_matches!(a, Some(_));
136-
/// assert_matches!(b, None);
139+
/// assert_matches!(b, Some(_));
140+
///
141+
/// assert_matches!(a, Some(345));
142+
/// assert_matches!(a, Some(345) | None);
143+
///
144+
/// // assert_matches!(a, None); // panics
145+
/// // assert_matches!(b, Some(345)); // panics
146+
/// // assert_matches!(b, Some(345) | None); // panics
137147
///
138-
/// let c = Ok("abc".to_string());
139-
/// assert_matches!(c, Ok(x) | Err(x) if x.len() < 100);
148+
/// assert_matches!(a, Some(x) if x > 100);
149+
/// // assert_matches!(a, Some(x) if x < 100); // panics
140150
/// ```
141151
#[unstable(feature = "assert_matches", issue = "82775")]
142152
#[allow_internal_unstable(panic_internals)]
@@ -369,21 +379,25 @@ macro_rules! debug_assert_ne {
369379
};
370380
}
371381

372-
/// Asserts that an expression matches any of the given patterns.
382+
/// Asserts that an expression matches the provided pattern.
373383
///
374-
/// Like in a `match` expression, the pattern can be optionally followed by `if`
375-
/// and a guard expression that has access to names bound by the pattern.
384+
/// This macro is generally preferable to `debug_assert!(matches!(value, pattern))`, because it can
385+
/// print the debug representation of the actual value shape that did not meet expectations. In
386+
/// contrast, using [`debug_assert!`] will only print that expectations were not met, but not why.
376387
///
377-
/// On panic, this macro will print the value of the expression with its
378-
/// debug representation.
388+
/// The pattern syntax is exactly the same as found in a match arm and the `matches!` macro. The
389+
/// optional if guard can be used to add additional checks that must be true for the matched value,
390+
/// otherwise this macro will panic.
379391
///
380-
/// Unlike [`assert_matches!`], `debug_assert_matches!` statements are only
381-
/// enabled in non optimized builds by default. An optimized build will not
382-
/// execute `debug_assert_matches!` statements unless `-C debug-assertions` is
383-
/// passed to the compiler. This makes `debug_assert_matches!` useful for
384-
/// checks that are too expensive to be present in a release build but may be
385-
/// helpful during development. The result of expanding `debug_assert_matches!`
386-
/// is always type checked.
392+
/// On panic, this macro will print the value of the expression with its debug representation.
393+
///
394+
/// Like [`assert!`], this macro has a second form, where a custom panic message can be provided.
395+
///
396+
/// Unlike [`assert_matches!`], `debug_assert_matches!` statements are only enabled in non optimized
397+
/// builds by default. An optimized build will not execute `debug_assert_matches!` statements unless
398+
/// `-C debug-assertions` is passed to the compiler. This makes `debug_assert_matches!` useful for
399+
/// checks that are too expensive to be present in a release build but may be helpful during
400+
/// development. The result of expanding `debug_assert_matches!` is always type checked.
387401
///
388402
/// # Examples
389403
///
@@ -392,13 +406,20 @@ macro_rules! debug_assert_ne {
392406
///
393407
/// use std::assert_matches::debug_assert_matches;
394408
///
395-
/// let a = 1u32.checked_add(2);
396-
/// let b = 1u32.checked_sub(2);
409+
/// let a = Some(345);
410+
/// let b = Some(56);
397411
/// debug_assert_matches!(a, Some(_));
398-
/// debug_assert_matches!(b, None);
412+
/// debug_assert_matches!(b, Some(_));
413+
///
414+
/// debug_assert_matches!(a, Some(345));
415+
/// debug_assert_matches!(a, Some(345) | None);
399416
///
400-
/// let c = Ok("abc".to_string());
401-
/// debug_assert_matches!(c, Ok(x) | Err(x) if x.len() < 100);
417+
/// // debug_assert_matches!(a, None); // panics
418+
/// // debug_assert_matches!(b, Some(345)); // panics
419+
/// // debug_assert_matches!(b, Some(345) | None); // panics
420+
///
421+
/// debug_assert_matches!(a, Some(x) if x > 100);
422+
/// // debug_assert_matches!(a, Some(x) if x < 100); // panics
402423
/// ```
403424
#[unstable(feature = "assert_matches", issue = "82775")]
404425
#[allow_internal_unstable(assert_matches)]
@@ -409,10 +430,15 @@ pub macro debug_assert_matches($($arg:tt)*) {
409430
}
410431
}
411432

412-
/// Returns whether the given expression matches any of the given patterns.
433+
/// Returns whether the given expression matches the provided pattern.
434+
///
435+
/// The pattern syntax is exactly the same as found in a match arm. The optional if guard can be
436+
/// used to add additional checks that must be true for the matched value, otherwise this macro will
437+
/// return `false`.
413438
///
414-
/// Like in a `match` expression, the pattern can be optionally followed by `if`
415-
/// and a guard expression that has access to names bound by the pattern.
439+
/// When testing that a value matches a pattern, it's generally preferable to use
440+
/// [`assert_matches!`] as it will print the debug representation of the value if the assertion
441+
/// fails.
416442
///
417443
/// # Examples
418444
///

0 commit comments

Comments
 (0)