Text of rust.docs.kernel.org › next › src › core › macros › mod.rs.html as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

mod.rs - source

Skip to main content core/macros/ mod.rs 1 #[doc = include_str! ( "panic.md" )] 2 #[macro_export] 3 #[rustc_builtin_macro(core_panic)] 4 #[allow_internal_unstable(edition_panic)] 5 #[stable(feature = "core" , since = "1.6.0" )] 6 #[rustc_diagnostic_item = "core_panic_macro" ] 7 macro_rules! panic { 8 // Expands to either `$crate::panic::panic_2015` or `$crate::panic::panic_2021` 9 // depending on the edition of the caller.

10 ($( $arg :tt) * ) => { 11 /* compiler built-in */ 12 }; 13 } 14 15 /// Asserts that two expressions are equal to each other (using [`PartialEq`]). 16 /// 17 /// Assertions are always checked in both debug and release builds, and cannot 18 /// be disabled. See [`debug_assert_eq!`] for assertions that are disabled in 19 /// release builds by default. 20 /// 21 /// [`debug_assert_eq!`]: crate::debug_assert_eq 22 /// 23 /// On panic, this macro will print the values of the expressions with their 24 /// debug representations.

25 /// 26 /// Like [`assert!`], this macro has a second form, where a custom 27 /// panic message can be provided. 28 /// 29 /// # Examples 30 /// 31 /// ``` 32 /// let a = 3; 33 /// let b = 1 + 2; 34 /// assert_eq!(a, b); 35 /// 36 /// assert_eq!(a, b, "we are testing addition with {} and {}", a, b); 37 /// ``` 38 #[macro_export] 39 #[stable(feature = "rust1" , since = "1.0.0" )] 40 #[rustc_diagnostic_item = "assert_eq_macro" ] 41 #[allow_internal_unstable(panic_internals)] 42 #[rustc_diagnostic_opaque] 43 macro_rules! assert_eq { 44 ( $left :expr, $right :expr $(,) ? ) => {{ 45 match ( & $left , & $right ) { 46 (left_val, right_val) => { 47 if !( * left_val == * right_val) { 48 let kind = $crate::panicking::AssertKind::Eq ; 49 // The reborrows below are intentional.

Without them, the stack slot for the 50 // borrow is initialized even before the values are compared, leading to a 51 // noticeable slow down. 52 $crate::panicking::assert_failed (kind, &* left_val, &* right_val, $crate::option::Option::None ); 53 } 54 } 55 } 56 }}; 57 ( $left :expr, $right :expr, $( $arg :tt)+) => {{ 58 match ( & $left , & $right ) { 59 (left_val, right_val) => { 60 if !( * left_val == * right_val) { 61 let kind = $crate::panicking::AssertKind::Eq ; 62 // The reborrows below are intentional.

Without them, the stack slot for the 63 // borrow is initialized even before the values are compared, leading to a 64 // noticeable slow down. 65 $crate::panicking::assert_failed (kind, &* left_val, &* right_val, $crate::option::Option::Some ( $crate::format_args !($( $arg )+))); 66 } 67 } 68 } 69 }}; 70 } 71 72 /// Asserts that two expressions are not equal to each other (using [`PartialEq`]). 73 /// 74 /// Assertions are always checked in both debug and release builds, and cannot 75 /// be disabled.

See [`debug_assert_ne!`] for assertions that are disabled in 76 /// release builds by default. 77 /// 78 /// [`debug_assert_ne!`]: crate::debug_assert_ne 79 /// 80 /// On panic, this macro will print the values of the expressions with their 81 /// debug representations. 82 /// 83 /// Like [`assert!`], this macro has a second form, where a custom 84 /// panic message can be provided. 85 /// 86 /// # Examples 87 /// 88 /// ``` 89 /// let a = 3; 90 /// let b = 2; 91 /// assert_ne!(a, b); 92 /// 93 /// assert_ne!(a, b, "we are testing that the values are not equal"); 94 /// ``` 95 #[macro_export] 96 #[stable(feature = "assert_ne" , since = "1.13.0" )] 97 #[rustc_diagnostic_item = "assert_ne_macro" ] 98 #[allow_internal_unstable(panic_internals)] 99 #[rustc_diagnostic_opaque] 100 macro_rules! assert_ne { 101 ( $left :expr, $right :expr $(,) ? ) => {{ 102 match ( & $left , & $right ) { 103 (left_val, right_val) => { 104 if * left_val == * right_val { 105 let kind = $crate::panicking::AssertKind::Ne ; 106 // The reborrows below are intentional.

Without them, the stack slot for the 107 // borrow is initialized even before the values are compared, leading to a 108 // noticeable slow down. 109 $crate::panicking::assert_failed (kind, &* left_val, &* right_val, $crate::option::Option::None ); 110 } 111 } 112 } 113 }}; 114 ( $left :expr, $right :expr, $( $arg :tt)+) => {{ 115 match ( & ( $left ), & ( $right )) { 116 (left_val, right_val) => { 117 if * left_val == * right_val { 118 let kind = $crate::panicking::AssertKind::Ne ; 119 // The reborrows below are intentional.

Without them, the stack slot for the 120 // borrow is initialized even before the values are compared, leading to a 121 // noticeable slow down. 122 $crate::panicking::assert_failed (kind, &* left_val, &* right_val, $crate::option::Option::Some ( $crate::format_args !($( $arg )+))); 123 } 124 } 125 } 126 }}; 127 } 128 129 /// Asserts that an expression matches the provided pattern. 130 /// 131 /// This macro is generally preferable to `assert!(matches!(value, pattern))`, because it can print 132 /// the debug representation of the actual value shape that did not meet expectations.

In contrast, 133 /// using [`assert!`] will only print that expectations were not met, but not why. 134 /// 135 /// The pattern syntax is exactly the same as found in a match arm and the `matches!` macro. The 136 /// optional if guard can be used to add additional checks that must be true for the matched value, 137 /// otherwise this macro will panic. 138 /// 139 /// Assertions are always checked in both debug and release builds, and cannot 140 /// be disabled.

See [`debug_assert_matches!`] for assertions that are disabled in 141 /// release builds by default. 142 /// 143 /// [`debug_assert_matches!`]: crate::debug_assert_matches 144 /// 145 /// On panic, this macro will print the value of the expression with its debug representation. 146 /// 147 /// Like [`assert!`], this macro has a second form, where a custom panic message can be provided. 148 /// 149 /// # Examples 150 /// 151 /// ``` 152 /// use std::assert_matches; 153 /// 154 /// let a = Some(345); 155 /// let b = Some(56); 156 /// assert_matches!(a, Some(_)); 157 /// assert_matches!(b, Some(_)); 158 /// 159 /// assert_matches!(a, Some(345)); 160 /// assert_matches!(a, Some(345) | None); 161 /// 162 /// // assert_matches!(a, None); // panics 163 /// // assert_matches!(b, Some(345)); // panics 164 /// // assert_matches!(b, Some(345) | None); // panics 165 /// 166 /// assert_matches!(a, Some(x) if x > 100); 167 /// // assert_matches!(a, Some(x) if x < 100); // panics 168 /// ``` 169 #[stable(feature = "assert_matches" , since = "1.96.0" )] 170 #[allow_internal_unstable(panic_internals)] 171 #[rustc_macro_transparency = "semiopaque" ] 172 pub macro assert_matches { 173 ( $left :expr, $(|) ? $( $pattern :pat_param )|+ $( if $guard : expr ) ? $(,) ? ) => {{ 174 match $left { 175 $( $pattern )|+ $( if $guard ) ? => {} 176 ref left_val => { 177 $crate::panicking::assert_matches_failed ( 178 left_val, 179 $crate::stringify !($( $pattern )|+ $( if $guard ) ? ), 180 $crate::option::Option::None 181 ); 182 } 183 } 184 }}, 185 ( $left :expr, $(|) ? $( $pattern :pat_param )|+ $( if $guard : expr ) ? , $( $arg :tt)+) => {{ 186 match $left { 187 $( $pattern )|+ $( if $guard ) ? => {} 188 ref left_val => { 189 $crate::panicking::assert_matches_failed ( 190 left_val, 191 $crate::stringify !($( $pattern )|+ $( if $guard ) ? ), 192 $crate::option::Option::Some ( $crate::format_args !($( $arg )+)) 193 ); 194 } 195 } 196 }}, 197 } 198 199 /// Selects code at compile-time based on `cfg` predicates.

200 /// 201 /// This macro evaluates, at compile-time, a series of `cfg` predicates, 202 /// selects the first that is true, and emits the code guarded by that 203 /// predicate. The code guarded by other predicates is not emitted. 204 /// 205 /// An optional trailing `_` wildcard can be used to specify a fallback. If 206 /// none of the predicates are true, a [`compile_error`] is emitted. 207 /// 208 /// # Example 209 /// 210 /// ``` 211 /// cfg_select! { 212 /// unix => { 213 /// fn foo() { /* unix specific functionality */ } 214 /// } 215 /// target_pointer_width = "32" => { 216 ///

The rest of this page is on the live site.