Skip to content

Commit a727679

Browse files
committed
Update docs for assert! and debug_assert!
1 parent 422ebd5 commit a727679

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/libcore/macros.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,19 @@ macro_rules! panic {
3232

3333
/// Ensure that a boolean expression is `true` at runtime.
3434
///
35-
/// This will invoke the `panic!` macro if the provided expression cannot be
36-
/// evaluated to `true` at runtime.
35+
/// This will ensure the termination of the program if the provided expression
36+
/// cannot be evaluated to `true` at runtime by means of an unrecoverable error
37+
/// (not necessarily a `panic!`, can also be an `abort`).
38+
///
39+
/// Assertions are always checked in both debug and release builds, and cannot
40+
/// be disabled.
41+
///
42+
/// Unsafe code relies on `assert!` to enforce run-time invariants that, if
43+
/// violated could lead to unsafety.
44+
///
45+
/// Other use-cases of `assert!` include
46+
/// [testing](https://doc.rust-lang.org/book/testing.html) and enforcing
47+
/// run-time invariants in safe code (whose violation cannot result in unsafety).
3748
///
3849
/// This macro has a second version, where a custom panic message can be provided.
3950
///
@@ -123,6 +134,13 @@ macro_rules! assert_eq {
123134
/// expensive to be present in a release build but may be helpful during
124135
/// development.
125136
///
137+
/// An unchecked assertion allows a program in an inconsistent state to keep
138+
/// running, which might have unexpected consequences but does not introduce
139+
/// unsafety as long as this only happens in safe code. The performance cost
140+
/// of assertions, is however, not measurable in general. Replacing `assert!`
141+
/// with `debug_assert!` is thus only encourage after thorough profiling, and
142+
/// more importantly, only in safe code!
143+
///
126144
/// # Examples
127145
///
128146
/// ```

0 commit comments

Comments
 (0)