Skip to content

Commit 7676f13

Browse files
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.
1 parent 6f82698 commit 7676f13

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
@@ -1208,7 +1208,8 @@ pub(crate) mod builtin {
12081208
///
12091209
/// This macro has a second form, where a custom panic message can
12101210
/// be provided with or without arguments for formatting. See [`std::fmt`]
1211-
/// for syntax for this form.
1211+
/// for syntax for this form. Expressions used as format arguments will only
1212+
/// be evaluated if the assertion fails.
12121213
///
12131214
/// [`std::fmt`]: ../std/fmt/index.html
12141215
///

0 commit comments

Comments
 (0)