Skip to content

Commit 69951d9

Browse files
Rollup merge of rust-lang#82169 - not-an-aardvark:assert-lazy-format-expressions, r=sfackler
Document that `assert!` format arguments are evaluated lazily It can be useful to do some computation in `assert!` format arguments, in order to get better error messages. For example: ```rust assert!( some_condition, "The state is invalid. Details: {}", expensive_call_to_get_debugging_info(), ); ``` It seems like `assert!` only evaluates the format arguments if the assertion fails, which is useful but doesn't appear to be documented anywhere. This PR documents the behavior and adds some tests.
2 parents faab609 + 7676f13 commit 69951d9

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

core/src/macros/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,8 @@ pub(crate) mod builtin {
12091209
///
12101210
/// This macro has a second form, where a custom panic message can
12111211
/// be provided with or without arguments for formatting. See [`std::fmt`]
1212-
/// for syntax for this form.
1212+
/// for syntax for this form. Expressions used as format arguments will only
1213+
/// be evaluated if the assertion fails.
12131214
///
12141215
/// [`std::fmt`]: ../std/fmt/index.html
12151216
///

0 commit comments

Comments
 (0)