]>
Commit | Line | Data |
---|---|---|
6a06907d | 1 | #[doc = include_str!("panic.md")] |
5869c6ff | 2 | #[macro_export] |
94222f64 | 3 | #[rustc_builtin_macro(core_panic)] |
5869c6ff XL |
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 | ||
041b39d2 | 15 | /// Asserts that two expressions are equal to each other (using [`PartialEq`]). |
1a4d82fc | 16 | /// |
c34b1796 AL |
17 | /// On panic, this macro will print the values of the expressions with their |
18 | /// debug representations. | |
1a4d82fc | 19 | /// |
041b39d2 | 20 | /// Like [`assert!`], this macro has a second form, where a custom |
32a655c1 SL |
21 | /// panic message can be provided. |
22 | /// | |
c34b1796 | 23 | /// # Examples |
1a4d82fc JJ |
24 | /// |
25 | /// ``` | |
85aaf69f SL |
26 | /// let a = 3; |
27 | /// let b = 1 + 2; | |
1a4d82fc | 28 | /// assert_eq!(a, b); |
32a655c1 SL |
29 | /// |
30 | /// assert_eq!(a, b, "we are testing addition with {} and {}", a, b); | |
1a4d82fc JJ |
31 | /// ``` |
32 | #[macro_export] | |
85aaf69f | 33 | #[stable(feature = "rust1", since = "1.0.0")] |
5099ac24 | 34 | #[cfg_attr(not(test), rustc_diagnostic_item = "assert_eq_macro")] |
6a06907d | 35 | #[allow_internal_unstable(core_panic)] |
1a4d82fc | 36 | macro_rules! assert_eq { |
5e7ed085 | 37 | ($left:expr, $right:expr $(,)?) => { |
a7813a04 | 38 | match (&$left, &$right) { |
1a4d82fc | 39 | (left_val, right_val) => { |
c34b1796 | 40 | if !(*left_val == *right_val) { |
6a06907d | 41 | let kind = $crate::panicking::AssertKind::Eq; |
9fa01778 XL |
42 | // The reborrows below are intentional. Without them, the stack slot for the |
43 | // borrow is initialized even before the values are compared, leading to a | |
44 | // noticeable slow down. | |
6a06907d | 45 | $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None); |
1a4d82fc JJ |
46 | } |
47 | } | |
48 | } | |
5e7ed085 FG |
49 | }; |
50 | ($left:expr, $right:expr, $($arg:tt)+) => { | |
6a06907d | 51 | match (&$left, &$right) { |
3157f602 XL |
52 | (left_val, right_val) => { |
53 | if !(*left_val == *right_val) { | |
6a06907d | 54 | let kind = $crate::panicking::AssertKind::Eq; |
9fa01778 XL |
55 | // The reborrows below are intentional. Without them, the stack slot for the |
56 | // borrow is initialized even before the values are compared, leading to a | |
57 | // noticeable slow down. | |
6a06907d | 58 | $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+))); |
3157f602 XL |
59 | } |
60 | } | |
61 | } | |
5e7ed085 | 62 | }; |
1a4d82fc JJ |
63 | } |
64 | ||
041b39d2 | 65 | /// Asserts that two expressions are not equal to each other (using [`PartialEq`]). |
9e0c209e SL |
66 | /// |
67 | /// On panic, this macro will print the values of the expressions with their | |
68 | /// debug representations. | |
69 | /// | |
041b39d2 | 70 | /// Like [`assert!`], this macro has a second form, where a custom |
32a655c1 SL |
71 | /// panic message can be provided. |
72 | /// | |
9e0c209e SL |
73 | /// # Examples |
74 | /// | |
75 | /// ``` | |
76 | /// let a = 3; | |
77 | /// let b = 2; | |
78 | /// assert_ne!(a, b); | |
32a655c1 SL |
79 | /// |
80 | /// assert_ne!(a, b, "we are testing that the values are not equal"); | |
9e0c209e SL |
81 | /// ``` |
82 | #[macro_export] | |
cc61c64b | 83 | #[stable(feature = "assert_ne", since = "1.13.0")] |
5099ac24 | 84 | #[cfg_attr(not(test), rustc_diagnostic_item = "assert_ne_macro")] |
6a06907d | 85 | #[allow_internal_unstable(core_panic)] |
9e0c209e | 86 | macro_rules! assert_ne { |
5e7ed085 | 87 | ($left:expr, $right:expr $(,)?) => { |
9e0c209e SL |
88 | match (&$left, &$right) { |
89 | (left_val, right_val) => { | |
90 | if *left_val == *right_val { | |
6a06907d | 91 | let kind = $crate::panicking::AssertKind::Ne; |
9fa01778 XL |
92 | // The reborrows below are intentional. Without them, the stack slot for the |
93 | // borrow is initialized even before the values are compared, leading to a | |
94 | // noticeable slow down. | |
6a06907d | 95 | $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None); |
9e0c209e SL |
96 | } |
97 | } | |
98 | } | |
5e7ed085 FG |
99 | }; |
100 | ($left:expr, $right:expr, $($arg:tt)+) => { | |
9e0c209e SL |
101 | match (&($left), &($right)) { |
102 | (left_val, right_val) => { | |
103 | if *left_val == *right_val { | |
6a06907d | 104 | let kind = $crate::panicking::AssertKind::Ne; |
9fa01778 XL |
105 | // The reborrows below are intentional. Without them, the stack slot for the |
106 | // borrow is initialized even before the values are compared, leading to a | |
107 | // noticeable slow down. | |
6a06907d | 108 | $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+))); |
9e0c209e SL |
109 | } |
110 | } | |
111 | } | |
5e7ed085 | 112 | }; |
9e0c209e SL |
113 | } |
114 | ||
17df50a5 XL |
115 | /// Asserts that an expression matches any of the given patterns. |
116 | /// | |
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. | |
119 | /// | |
120 | /// On panic, this macro will print the value of the expression with its | |
121 | /// debug representation. | |
122 | /// | |
123 | /// Like [`assert!`], this macro has a second form, where a custom | |
124 | /// panic message can be provided. | |
125 | /// | |
126 | /// # Examples | |
127 | /// | |
128 | /// ``` | |
129 | /// #![feature(assert_matches)] | |
130 | /// | |
131 | /// use std::assert_matches::assert_matches; | |
132 | /// | |
133 | /// let a = 1u32.checked_add(2); | |
134 | /// let b = 1u32.checked_sub(2); | |
135 | /// assert_matches!(a, Some(_)); | |
136 | /// assert_matches!(b, None); | |
137 | /// | |
138 | /// let c = Ok("abc".to_string()); | |
139 | /// assert_matches!(c, Ok(x) | Err(x) if x.len() < 100); | |
140 | /// ``` | |
141 | #[unstable(feature = "assert_matches", issue = "82775")] | |
142 | #[allow_internal_unstable(core_panic)] | |
143 | #[rustc_macro_transparency = "semitransparent"] | |
144 | pub macro assert_matches { | |
5e7ed085 | 145 | ($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => { |
17df50a5 XL |
146 | match $left { |
147 | $( $pattern )|+ $( if $guard )? => {} | |
148 | ref left_val => { | |
149 | $crate::panicking::assert_matches_failed( | |
150 | left_val, | |
151 | $crate::stringify!($($pattern)|+ $(if $guard)?), | |
152 | $crate::option::Option::None | |
153 | ); | |
154 | } | |
155 | } | |
5e7ed085 FG |
156 | }, |
157 | ($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => { | |
17df50a5 XL |
158 | match $left { |
159 | $( $pattern )|+ $( if $guard )? => {} | |
160 | ref left_val => { | |
161 | $crate::panicking::assert_matches_failed( | |
162 | left_val, | |
163 | $crate::stringify!($($pattern)|+ $(if $guard)?), | |
164 | $crate::option::Option::Some($crate::format_args!($($arg)+)) | |
165 | ); | |
166 | } | |
167 | } | |
5e7ed085 | 168 | }, |
17df50a5 XL |
169 | } |
170 | ||
532ac7d7 | 171 | /// Asserts that a boolean expression is `true` at runtime. |
1a4d82fc | 172 | /// |
8bb4bdeb | 173 | /// This will invoke the [`panic!`] macro if the provided expression cannot be |
1a4d82fc JJ |
174 | /// evaluated to `true` at runtime. |
175 | /// | |
8bb4bdeb | 176 | /// Like [`assert!`], this macro also has a second version, where a custom panic |
92a42be0 SL |
177 | /// message can be provided. |
178 | /// | |
041b39d2 XL |
179 | /// # Uses |
180 | /// | |
8bb4bdeb | 181 | /// Unlike [`assert!`], `debug_assert!` statements are only enabled in non |
416331ca | 182 | /// optimized builds by default. An optimized build will not execute |
c34b1796 AL |
183 | /// `debug_assert!` statements unless `-C debug-assertions` is passed to the |
184 | /// compiler. This makes `debug_assert!` useful for checks that are too | |
185 | /// expensive to be present in a release build but may be helpful during | |
416331ca | 186 | /// development. The result of expanding `debug_assert!` is always type checked. |
1a4d82fc | 187 | /// |
5bcae85e SL |
188 | /// An unchecked assertion allows a program in an inconsistent state to keep |
189 | /// running, which might have unexpected consequences but does not introduce | |
190 | /// unsafety as long as this only happens in safe code. The performance cost | |
f035d41b | 191 | /// of assertions, however, is not measurable in general. Replacing [`assert!`] |
5bcae85e SL |
192 | /// with `debug_assert!` is thus only encouraged after thorough profiling, and |
193 | /// more importantly, only in safe code! | |
194 | /// | |
c34b1796 | 195 | /// # Examples |
1a4d82fc JJ |
196 | /// |
197 | /// ``` | |
198 | /// // the panic message for these assertions is the stringified value of the | |
199 | /// // expression given. | |
200 | /// debug_assert!(true); | |
85aaf69f SL |
201 | /// |
202 | /// fn some_expensive_computation() -> bool { true } // a very simple function | |
1a4d82fc JJ |
203 | /// debug_assert!(some_expensive_computation()); |
204 | /// | |
205 | /// // assert with a custom message | |
85aaf69f | 206 | /// let x = true; |
1a4d82fc | 207 | /// debug_assert!(x, "x wasn't true!"); |
85aaf69f SL |
208 | /// |
209 | /// let a = 3; let b = 27; | |
1a4d82fc JJ |
210 | /// debug_assert!(a + b == 30, "a = {}, b = {}", a, b); |
211 | /// ``` | |
212 | #[macro_export] | |
85aaf69f | 213 | #[stable(feature = "rust1", since = "1.0.0")] |
5869c6ff | 214 | #[rustc_diagnostic_item = "debug_assert_macro"] |
dc3f5686 | 215 | #[allow_internal_unstable(edition_panic)] |
1a4d82fc | 216 | macro_rules! debug_assert { |
5e7ed085 FG |
217 | ($($arg:tt)*) => { |
218 | if $crate::cfg!(debug_assertions) { | |
219 | $crate::assert!($($arg)*); | |
220 | } | |
221 | }; | |
1a4d82fc JJ |
222 | } |
223 | ||
9cc50fc6 | 224 | /// Asserts that two expressions are equal to each other. |
1a4d82fc | 225 | /// |
9cc50fc6 SL |
226 | /// On panic, this macro will print the values of the expressions with their |
227 | /// debug representations. | |
1a4d82fc | 228 | /// |
ea8adc8c | 229 | /// Unlike [`assert_eq!`], `debug_assert_eq!` statements are only enabled in non |
416331ca | 230 | /// optimized builds by default. An optimized build will not execute |
c34b1796 AL |
231 | /// `debug_assert_eq!` statements unless `-C debug-assertions` is passed to the |
232 | /// compiler. This makes `debug_assert_eq!` useful for checks that are too | |
233 | /// expensive to be present in a release build but may be helpful during | |
416331ca | 234 | /// development. The result of expanding `debug_assert_eq!` is always type checked. |
1a4d82fc | 235 | /// |
c34b1796 | 236 | /// # Examples |
1a4d82fc JJ |
237 | /// |
238 | /// ``` | |
85aaf69f SL |
239 | /// let a = 3; |
240 | /// let b = 1 + 2; | |
1a4d82fc JJ |
241 | /// debug_assert_eq!(a, b); |
242 | /// ``` | |
243 | #[macro_export] | |
92a42be0 | 244 | #[stable(feature = "rust1", since = "1.0.0")] |
5099ac24 | 245 | #[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_eq_macro")] |
1a4d82fc | 246 | macro_rules! debug_assert_eq { |
5e7ed085 FG |
247 | ($($arg:tt)*) => { |
248 | if $crate::cfg!(debug_assertions) { | |
249 | $crate::assert_eq!($($arg)*); | |
250 | } | |
251 | }; | |
1a4d82fc JJ |
252 | } |
253 | ||
9e0c209e SL |
254 | /// Asserts that two expressions are not equal to each other. |
255 | /// | |
256 | /// On panic, this macro will print the values of the expressions with their | |
257 | /// debug representations. | |
258 | /// | |
ea8adc8c | 259 | /// Unlike [`assert_ne!`], `debug_assert_ne!` statements are only enabled in non |
416331ca | 260 | /// optimized builds by default. An optimized build will not execute |
9e0c209e SL |
261 | /// `debug_assert_ne!` statements unless `-C debug-assertions` is passed to the |
262 | /// compiler. This makes `debug_assert_ne!` useful for checks that are too | |
263 | /// expensive to be present in a release build but may be helpful during | |
416331ca | 264 | /// development. The result of expanding `debug_assert_ne!` is always type checked. |
9e0c209e SL |
265 | /// |
266 | /// # Examples | |
267 | /// | |
268 | /// ``` | |
269 | /// let a = 3; | |
270 | /// let b = 2; | |
271 | /// debug_assert_ne!(a, b); | |
272 | /// ``` | |
273 | #[macro_export] | |
cc61c64b | 274 | #[stable(feature = "assert_ne", since = "1.13.0")] |
5099ac24 | 275 | #[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_ne_macro")] |
9e0c209e | 276 | macro_rules! debug_assert_ne { |
5e7ed085 FG |
277 | ($($arg:tt)*) => { |
278 | if $crate::cfg!(debug_assertions) { | |
279 | $crate::assert_ne!($($arg)*); | |
280 | } | |
281 | }; | |
9e0c209e SL |
282 | } |
283 | ||
17df50a5 XL |
284 | /// Asserts that an expression matches any of the given patterns. |
285 | /// | |
286 | /// Like in a `match` expression, the pattern can be optionally followed by `if` | |
287 | /// and a guard expression that has access to names bound by the pattern. | |
288 | /// | |
289 | /// On panic, this macro will print the value of the expression with its | |
290 | /// debug representation. | |
291 | /// | |
292 | /// Unlike [`assert_matches!`], `debug_assert_matches!` statements are only | |
293 | /// enabled in non optimized builds by default. An optimized build will not | |
294 | /// execute `debug_assert_matches!` statements unless `-C debug-assertions` is | |
295 | /// passed to the compiler. This makes `debug_assert_matches!` useful for | |
296 | /// checks that are too expensive to be present in a release build but may be | |
297 | /// helpful during development. The result of expanding `debug_assert_matches!` | |
298 | /// is always type checked. | |
299 | /// | |
300 | /// # Examples | |
301 | /// | |
302 | /// ``` | |
303 | /// #![feature(assert_matches)] | |
304 | /// | |
305 | /// use std::assert_matches::debug_assert_matches; | |
306 | /// | |
307 | /// let a = 1u32.checked_add(2); | |
308 | /// let b = 1u32.checked_sub(2); | |
309 | /// debug_assert_matches!(a, Some(_)); | |
310 | /// debug_assert_matches!(b, None); | |
311 | /// | |
312 | /// let c = Ok("abc".to_string()); | |
313 | /// debug_assert_matches!(c, Ok(x) | Err(x) if x.len() < 100); | |
314 | /// ``` | |
315 | #[macro_export] | |
316 | #[unstable(feature = "assert_matches", issue = "82775")] | |
317 | #[allow_internal_unstable(assert_matches)] | |
318 | #[rustc_macro_transparency = "semitransparent"] | |
319 | pub macro debug_assert_matches($($arg:tt)*) { | |
5e7ed085 FG |
320 | if $crate::cfg!(debug_assertions) { |
321 | $crate::assert_matches::assert_matches!($($arg)*); | |
322 | } | |
17df50a5 XL |
323 | } |
324 | ||
e74abb32 XL |
325 | /// Returns whether the given expression matches any of the given patterns. |
326 | /// | |
327 | /// Like in a `match` expression, the pattern can be optionally followed by `if` | |
328 | /// and a guard expression that has access to names bound by the pattern. | |
329 | /// | |
330 | /// # Examples | |
331 | /// | |
332 | /// ``` | |
e74abb32 XL |
333 | /// let foo = 'f'; |
334 | /// assert!(matches!(foo, 'A'..='Z' | 'a'..='z')); | |
335 | /// | |
336 | /// let bar = Some(4); | |
337 | /// assert!(matches!(bar, Some(x) if x > 2)); | |
338 | /// ``` | |
339 | #[macro_export] | |
dfeec247 | 340 | #[stable(feature = "matches_macro", since = "1.42.0")] |
5099ac24 | 341 | #[cfg_attr(not(test), rustc_diagnostic_item = "matches_macro")] |
e74abb32 | 342 | macro_rules! matches { |
94222f64 | 343 | ($expression:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => { |
e74abb32 XL |
344 | match $expression { |
345 | $( $pattern )|+ $( if $guard )? => true, | |
346 | _ => false | |
347 | } | |
5e7ed085 | 348 | }; |
e74abb32 XL |
349 | } |
350 | ||
532ac7d7 | 351 | /// Unwraps a result or propagates its error. |
9e0c209e | 352 | /// |
f2b60f7d FG |
353 | /// The [`?` operator][propagating-errors] was added to replace `try!` |
354 | /// and should be used instead. Furthermore, `try` is a reserved word | |
355 | /// in Rust 2018, so if you must use it, you will need to use the | |
356 | /// [raw-identifier syntax][ris]: `r#try`. | |
0731742a | 357 | /// |
f2b60f7d | 358 | /// [propagating-errors]: https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator |
0731742a | 359 | /// [ris]: https://doc.rust-lang.org/nightly/rust-by-example/compatibility/raw_identifiers.html |
9e0c209e | 360 | /// |
ea8adc8c | 361 | /// `try!` matches the given [`Result`]. In case of the `Ok` variant, the |
9e0c209e SL |
362 | /// expression has the value of the wrapped value. |
363 | /// | |
364 | /// In case of the `Err` variant, it retrieves the inner error. `try!` then | |
365 | /// performs conversion using `From`. This provides automatic conversion | |
366 | /// between specialized errors and more general ones. The resulting | |
367 | /// error is then immediately returned. | |
368 | /// | |
369 | /// Because of the early return, `try!` can only be used in functions that | |
ea8adc8c XL |
370 | /// return [`Result`]. |
371 | /// | |
92a42be0 SL |
372 | /// # Examples |
373 | /// | |
374 | /// ``` | |
375 | /// use std::io; | |
376 | /// use std::fs::File; | |
377 | /// use std::io::prelude::*; | |
378 | /// | |
9e0c209e SL |
379 | /// enum MyError { |
380 | /// FileWriteError | |
381 | /// } | |
382 | /// | |
383 | /// impl From<io::Error> for MyError { | |
384 | /// fn from(e: io::Error) -> MyError { | |
385 | /// MyError::FileWriteError | |
386 | /// } | |
387 | /// } | |
388 | /// | |
2c00a5a8 | 389 | /// // The preferred method of quick returning Errors |
ea8adc8c XL |
390 | /// fn write_to_file_question() -> Result<(), MyError> { |
391 | /// let mut file = File::create("my_best_friends.txt")?; | |
2c00a5a8 | 392 | /// file.write_all(b"This is a list of my best friends.")?; |
ea8adc8c XL |
393 | /// Ok(()) |
394 | /// } | |
395 | /// | |
396 | /// // The previous method of quick returning Errors | |
9e0c209e | 397 | /// fn write_to_file_using_try() -> Result<(), MyError> { |
a1dfa0c6 XL |
398 | /// let mut file = r#try!(File::create("my_best_friends.txt")); |
399 | /// r#try!(file.write_all(b"This is a list of my best friends.")); | |
92a42be0 SL |
400 | /// Ok(()) |
401 | /// } | |
ea8adc8c | 402 | /// |
92a42be0 | 403 | /// // This is equivalent to: |
9e0c209e | 404 | /// fn write_to_file_using_match() -> Result<(), MyError> { |
a1dfa0c6 | 405 | /// let mut file = r#try!(File::create("my_best_friends.txt")); |
92a42be0 | 406 | /// match file.write_all(b"This is a list of my best friends.") { |
a7813a04 | 407 | /// Ok(v) => v, |
9e0c209e | 408 | /// Err(e) => return Err(From::from(e)), |
92a42be0 | 409 | /// } |
92a42be0 SL |
410 | /// Ok(()) |
411 | /// } | |
412 | /// ``` | |
1a4d82fc | 413 | #[macro_export] |
92a42be0 | 414 | #[stable(feature = "rust1", since = "1.0.0")] |
04454e1e | 415 | #[deprecated(since = "1.39.0", note = "use the `?` operator instead")] |
83c7162d | 416 | #[doc(alias = "?")] |
a1dfa0c6 | 417 | macro_rules! r#try { |
29967ef6 | 418 | ($expr:expr $(,)?) => { |
60c5eb7d XL |
419 | match $expr { |
420 | $crate::result::Result::Ok(val) => val, | |
421 | $crate::result::Result::Err(err) => { | |
422 | return $crate::result::Result::Err($crate::convert::From::from(err)); | |
423 | } | |
1a4d82fc | 424 | } |
60c5eb7d | 425 | }; |
1a4d82fc JJ |
426 | } |
427 | ||
532ac7d7 | 428 | /// Writes formatted data into a buffer. |
b039eaaf | 429 | /// |
29967ef6 | 430 | /// This macro accepts a 'writer', a format string, and a list of arguments. Arguments will be |
cc61c64b XL |
431 | /// formatted according to the specified format string and the result will be passed to the writer. |
432 | /// The writer may be any value with a `write_fmt` method; generally this comes from an | |
1b1a35ee XL |
433 | /// implementation of either the [`fmt::Write`] or the [`io::Write`] trait. The macro |
434 | /// returns whatever the `write_fmt` method returns; commonly a [`fmt::Result`], or an | |
cc61c64b | 435 | /// [`io::Result`]. |
b039eaaf | 436 | /// |
cc61c64b | 437 | /// See [`std::fmt`] for more information on the format string syntax. |
5bcae85e | 438 | /// |
29967ef6 | 439 | /// [`std::fmt`]: ../std/fmt/index.html |
1b1a35ee XL |
440 | /// [`fmt::Write`]: crate::fmt::Write |
441 | /// [`io::Write`]: ../std/io/trait.Write.html | |
442 | /// [`fmt::Result`]: crate::fmt::Result | |
cc61c64b | 443 | /// [`io::Result`]: ../std/io/type.Result.html |
1a4d82fc | 444 | /// |
c34b1796 | 445 | /// # Examples |
1a4d82fc JJ |
446 | /// |
447 | /// ``` | |
c34b1796 | 448 | /// use std::io::Write; |
1a4d82fc | 449 | /// |
416331ca XL |
450 | /// fn main() -> std::io::Result<()> { |
451 | /// let mut w = Vec::new(); | |
452 | /// write!(&mut w, "test")?; | |
453 | /// write!(&mut w, "formatted {}", "arguments")?; | |
b039eaaf | 454 | /// |
416331ca XL |
455 | /// assert_eq!(w, b"testformatted arguments"); |
456 | /// Ok(()) | |
457 | /// } | |
1a4d82fc | 458 | /// ``` |
476ff2be SL |
459 | /// |
460 | /// A module can import both `std::fmt::Write` and `std::io::Write` and call `write!` on objects | |
461 | /// implementing either, as objects do not typically implement both. However, the module must | |
f2b60f7d FG |
462 | /// avoid conflict between the trait names, such as by importing them as `_` or otherwise renaming |
463 | /// them: | |
476ff2be SL |
464 | /// |
465 | /// ``` | |
f2b60f7d FG |
466 | /// use std::fmt::Write as _; |
467 | /// use std::io::Write as _; | |
476ff2be | 468 | /// |
416331ca XL |
469 | /// fn main() -> Result<(), Box<dyn std::error::Error>> { |
470 | /// let mut s = String::new(); | |
471 | /// let mut v = Vec::new(); | |
472 | /// | |
473 | /// write!(&mut s, "{} {}", "abc", 123)?; // uses fmt::Write::write_fmt | |
474 | /// write!(&mut v, "s = {:?}", s)?; // uses io::Write::write_fmt | |
475 | /// assert_eq!(v, b"s = \"abc 123\""); | |
476 | /// Ok(()) | |
477 | /// } | |
476ff2be | 478 | /// ``` |
b7449926 | 479 | /// |
f2b60f7d FG |
480 | /// If you also need the trait names themselves, such as to implement one or both on your types, |
481 | /// import the containing module and then name them with a prefix: | |
482 | /// | |
483 | /// ``` | |
484 | /// # #![allow(unused_imports)] | |
485 | /// use std::fmt::{self, Write as _}; | |
486 | /// use std::io::{self, Write as _}; | |
487 | /// | |
488 | /// struct Example; | |
489 | /// | |
490 | /// impl fmt::Write for Example { | |
491 | /// fn write_str(&mut self, _s: &str) -> core::fmt::Result { | |
492 | /// unimplemented!(); | |
493 | /// } | |
494 | /// } | |
495 | /// ``` | |
496 | /// | |
a1dfa0c6 XL |
497 | /// Note: This macro can be used in `no_std` setups as well. |
498 | /// In a `no_std` setup you are responsible for the implementation details of the components. | |
b7449926 XL |
499 | /// |
500 | /// ```no_run | |
501 | /// # extern crate core; | |
502 | /// use core::fmt::Write; | |
503 | /// | |
504 | /// struct Example; | |
505 | /// | |
506 | /// impl Write for Example { | |
507 | /// fn write_str(&mut self, _s: &str) -> core::fmt::Result { | |
508 | /// unimplemented!(); | |
509 | /// } | |
510 | /// } | |
511 | /// | |
512 | /// let mut m = Example{}; | |
513 | /// write!(&mut m, "Hello World").expect("Not written"); | |
514 | /// ``` | |
1a4d82fc | 515 | #[macro_export] |
cc61c64b | 516 | #[stable(feature = "rust1", since = "1.0.0")] |
5099ac24 | 517 | #[cfg_attr(not(test), rustc_diagnostic_item = "write_macro")] |
1a4d82fc | 518 | macro_rules! write { |
5e7ed085 FG |
519 | ($dst:expr, $($arg:tt)*) => { |
520 | $dst.write_fmt($crate::format_args!($($arg)*)) | |
521 | }; | |
1a4d82fc JJ |
522 | } |
523 | ||
c30ab7b3 | 524 | /// Write formatted data into a buffer, with a newline appended. |
5bcae85e SL |
525 | /// |
526 | /// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`) alone | |
527 | /// (no additional CARRIAGE RETURN (`\r`/`U+000D`). | |
528 | /// | |
cc61c64b XL |
529 | /// For more information, see [`write!`]. For information on the format string syntax, see |
530 | /// [`std::fmt`]. | |
5bcae85e | 531 | /// |
5869c6ff | 532 | /// [`std::fmt`]: ../std/fmt/index.html |
b039eaaf SL |
533 | /// |
534 | /// # Examples | |
535 | /// | |
536 | /// ``` | |
416331ca | 537 | /// use std::io::{Write, Result}; |
b039eaaf | 538 | /// |
416331ca XL |
539 | /// fn main() -> Result<()> { |
540 | /// let mut w = Vec::new(); | |
541 | /// writeln!(&mut w)?; | |
542 | /// writeln!(&mut w, "test")?; | |
543 | /// writeln!(&mut w, "formatted {}", "arguments")?; | |
b039eaaf | 544 | /// |
416331ca XL |
545 | /// assert_eq!(&w[..], "\ntest\nformatted arguments\n".as_bytes()); |
546 | /// Ok(()) | |
547 | /// } | |
b039eaaf | 548 | /// ``` |
1a4d82fc | 549 | #[macro_export] |
85aaf69f | 550 | #[stable(feature = "rust1", since = "1.0.0")] |
5099ac24 | 551 | #[cfg_attr(not(test), rustc_diagnostic_item = "writeln_macro")] |
532ac7d7 | 552 | #[allow_internal_unstable(format_args_nl)] |
1a4d82fc | 553 | macro_rules! writeln { |
5e7ed085 | 554 | ($dst:expr $(,)?) => { |
48663c56 | 555 | $crate::write!($dst, "\n") |
5e7ed085 FG |
556 | }; |
557 | ($dst:expr, $($arg:tt)*) => { | |
e1599b0c | 558 | $dst.write_fmt($crate::format_args_nl!($($arg)*)) |
5e7ed085 | 559 | }; |
1a4d82fc JJ |
560 | } |
561 | ||
532ac7d7 | 562 | /// Indicates unreachable code. |
1a4d82fc JJ |
563 | /// |
564 | /// This is useful any time that the compiler can't determine that some code is unreachable. For | |
565 | /// example: | |
566 | /// | |
567 | /// * Match arms with guard conditions. | |
568 | /// * Loops that dynamically terminate. | |
569 | /// * Iterators that dynamically terminate. | |
570 | /// | |
ea8adc8c | 571 | /// If the determination that the code is unreachable proves incorrect, the |
dc9dc135 XL |
572 | /// program immediately terminates with a [`panic!`]. |
573 | /// | |
574 | /// The unsafe counterpart of this macro is the [`unreachable_unchecked`] function, which | |
575 | /// will cause undefined behavior if the code is reached. | |
ea8adc8c | 576 | /// |
3dfed10e | 577 | /// [`unreachable_unchecked`]: crate::hint::unreachable_unchecked |
ea8adc8c | 578 | /// |
1a4d82fc JJ |
579 | /// # Panics |
580 | /// | |
3c0e092e XL |
581 | /// This will always [`panic!`] because `unreachable!` is just a shorthand for `panic!` with a |
582 | /// fixed, specific message. | |
583 | /// | |
584 | /// Like `panic!`, this macro has a second form for displaying custom values. | |
1a4d82fc JJ |
585 | /// |
586 | /// # Examples | |
587 | /// | |
588 | /// Match arms: | |
589 | /// | |
c34b1796 | 590 | /// ``` |
92a42be0 | 591 | /// # #[allow(dead_code)] |
c34b1796 | 592 | /// fn foo(x: Option<i32>) { |
1a4d82fc JJ |
593 | /// match x { |
594 | /// Some(n) if n >= 0 => println!("Some(Non-negative)"), | |
595 | /// Some(n) if n < 0 => println!("Some(Negative)"), | |
596 | /// Some(_) => unreachable!(), // compile error if commented out | |
597 | /// None => println!("None") | |
598 | /// } | |
599 | /// } | |
600 | /// ``` | |
601 | /// | |
602 | /// Iterators: | |
603 | /// | |
c34b1796 | 604 | /// ``` |
92a42be0 | 605 | /// # #[allow(dead_code)] |
1a4d82fc | 606 | /// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3 |
c34b1796 | 607 | /// for i in 0.. { |
1a4d82fc JJ |
608 | /// if 3*i < i { panic!("u32 overflow"); } |
609 | /// if x < 3*i { return i-1; } | |
610 | /// } | |
3c0e092e | 611 | /// unreachable!("The loop should always return"); |
1a4d82fc JJ |
612 | /// } |
613 | /// ``` | |
a2a8927a XL |
614 | #[macro_export] |
615 | #[rustc_builtin_macro(unreachable)] | |
616 | #[allow_internal_unstable(edition_panic)] | |
617 | #[stable(feature = "rust1", since = "1.0.0")] | |
618 | #[cfg_attr(not(test), rustc_diagnostic_item = "unreachable_macro")] | |
619 | macro_rules! unreachable { | |
620 | // Expands to either `$crate::panic::unreachable_2015` or `$crate::panic::unreachable_2021` | |
621 | // depending on the edition of the caller. | |
622 | ($($arg:tt)*) => { | |
623 | /* compiler built-in */ | |
624 | }; | |
625 | } | |
626 | ||
dfeec247 | 627 | /// Indicates unimplemented code by panicking with a message of "not implemented". |
e9174d1e | 628 | /// |
dfeec247 | 629 | /// This allows your code to type-check, which is useful if you are prototyping or |
17df50a5 | 630 | /// implementing a trait that requires multiple methods which you don't plan to use all of. |
e74abb32 | 631 | /// |
fc512014 | 632 | /// The difference between `unimplemented!` and [`todo!`] is that while `todo!` |
dfeec247 XL |
633 | /// conveys an intent of implementing the functionality later and the message is "not yet |
634 | /// implemented", `unimplemented!` makes no such claims. Its message is "not implemented". | |
635 | /// Also some IDEs will mark `todo!`s. | |
e9174d1e | 636 | /// |
ea8adc8c XL |
637 | /// # Panics |
638 | /// | |
fc512014 XL |
639 | /// This will always [`panic!`] because `unimplemented!` is just a shorthand for `panic!` with a |
640 | /// fixed, specific message. | |
e74abb32 XL |
641 | /// |
642 | /// Like `panic!`, this macro has a second form for displaying custom values. | |
ea8adc8c | 643 | /// |
04454e1e FG |
644 | /// [`todo!`]: crate::todo |
645 | /// | |
e9174d1e SL |
646 | /// # Examples |
647 | /// | |
dfeec247 | 648 | /// Say we have a trait `Foo`: |
e9174d1e SL |
649 | /// |
650 | /// ``` | |
651 | /// trait Foo { | |
e74abb32 | 652 | /// fn bar(&self) -> u8; |
e9174d1e | 653 | /// fn baz(&self); |
e74abb32 | 654 | /// fn qux(&self) -> Result<u64, ()>; |
e9174d1e SL |
655 | /// } |
656 | /// ``` | |
657 | /// | |
dfeec247 XL |
658 | /// We want to implement `Foo` for 'MyStruct', but for some reason it only makes sense |
659 | /// to implement the `bar()` function. `baz()` and `qux()` will still need to be defined | |
e74abb32 XL |
660 | /// in our implementation of `Foo`, but we can use `unimplemented!` in their definitions |
661 | /// to allow our code to compile. | |
662 | /// | |
dfeec247 XL |
663 | /// We still want to have our program stop running if the unimplemented methods are |
664 | /// reached. | |
e9174d1e SL |
665 | /// |
666 | /// ``` | |
667 | /// # trait Foo { | |
e74abb32 | 668 | /// # fn bar(&self) -> u8; |
92a42be0 | 669 | /// # fn baz(&self); |
e74abb32 | 670 | /// # fn qux(&self) -> Result<u64, ()>; |
e9174d1e SL |
671 | /// # } |
672 | /// struct MyStruct; | |
673 | /// | |
674 | /// impl Foo for MyStruct { | |
e74abb32 XL |
675 | /// fn bar(&self) -> u8 { |
676 | /// 1 + 1 | |
e9174d1e SL |
677 | /// } |
678 | /// | |
92a42be0 | 679 | /// fn baz(&self) { |
dfeec247 XL |
680 | /// // It makes no sense to `baz` a `MyStruct`, so we have no logic here |
681 | /// // at all. | |
682 | /// // This will display "thread 'main' panicked at 'not implemented'". | |
e9174d1e SL |
683 | /// unimplemented!(); |
684 | /// } | |
e74abb32 XL |
685 | /// |
686 | /// fn qux(&self) -> Result<u64, ()> { | |
e74abb32 | 687 | /// // We have some logic here, |
dfeec247 | 688 | /// // We can add a message to unimplemented! to display our omission. |
e74abb32 | 689 | /// // This will display: |
dfeec247 XL |
690 | /// // "thread 'main' panicked at 'not implemented: MyStruct isn't quxable'". |
691 | /// unimplemented!("MyStruct isn't quxable"); | |
e74abb32 | 692 | /// } |
e9174d1e SL |
693 | /// } |
694 | /// | |
695 | /// fn main() { | |
696 | /// let s = MyStruct; | |
92a42be0 | 697 | /// s.bar(); |
e9174d1e SL |
698 | /// } |
699 | /// ``` | |
1a4d82fc | 700 | #[macro_export] |
cc61c64b | 701 | #[stable(feature = "rust1", since = "1.0.0")] |
5099ac24 | 702 | #[cfg_attr(not(test), rustc_diagnostic_item = "unimplemented_macro")] |
a2a8927a | 703 | #[allow_internal_unstable(core_panic)] |
1a4d82fc | 704 | macro_rules! unimplemented { |
5e7ed085 FG |
705 | () => { |
706 | $crate::panicking::panic("not implemented") | |
707 | }; | |
708 | ($($arg:tt)+) => { | |
709 | $crate::panic!("not implemented: {}", $crate::format_args!($($arg)+)) | |
710 | }; | |
1a4d82fc | 711 | } |
c30ab7b3 | 712 | |
532ac7d7 XL |
713 | /// Indicates unfinished code. |
714 | /// | |
715 | /// This can be useful if you are prototyping and are just looking to have your | |
e74abb32 XL |
716 | /// code typecheck. |
717 | /// | |
dfeec247 XL |
718 | /// The difference between [`unimplemented!`] and `todo!` is that while `todo!` conveys |
719 | /// an intent of implementing the functionality later and the message is "not yet | |
720 | /// implemented", `unimplemented!` makes no such claims. Its message is "not implemented". | |
721 | /// Also some IDEs will mark `todo!`s. | |
722 | /// | |
532ac7d7 XL |
723 | /// # Panics |
724 | /// | |
fc512014 | 725 | /// This will always [`panic!`]. |
532ac7d7 XL |
726 | /// |
727 | /// # Examples | |
728 | /// | |
729 | /// Here's an example of some in-progress code. We have a trait `Foo`: | |
730 | /// | |
731 | /// ``` | |
732 | /// trait Foo { | |
733 | /// fn bar(&self); | |
734 | /// fn baz(&self); | |
735 | /// } | |
736 | /// ``` | |
737 | /// | |
738 | /// We want to implement `Foo` on one of our types, but we also want to work on | |
739 | /// just `bar()` first. In order for our code to compile, we need to implement | |
740 | /// `baz()`, so we can use `todo!`: | |
741 | /// | |
742 | /// ``` | |
532ac7d7 XL |
743 | /// # trait Foo { |
744 | /// # fn bar(&self); | |
745 | /// # fn baz(&self); | |
746 | /// # } | |
747 | /// struct MyStruct; | |
748 | /// | |
749 | /// impl Foo for MyStruct { | |
750 | /// fn bar(&self) { | |
751 | /// // implementation goes here | |
752 | /// } | |
753 | /// | |
754 | /// fn baz(&self) { | |
755 | /// // let's not worry about implementing baz() for now | |
756 | /// todo!(); | |
757 | /// } | |
758 | /// } | |
759 | /// | |
760 | /// fn main() { | |
761 | /// let s = MyStruct; | |
762 | /// s.bar(); | |
763 | /// | |
dfeec247 | 764 | /// // we aren't even using baz(), so this is fine. |
532ac7d7 XL |
765 | /// } |
766 | /// ``` | |
767 | #[macro_export] | |
dfeec247 | 768 | #[stable(feature = "todo_macro", since = "1.40.0")] |
5099ac24 | 769 | #[cfg_attr(not(test), rustc_diagnostic_item = "todo_macro")] |
a2a8927a | 770 | #[allow_internal_unstable(core_panic)] |
532ac7d7 | 771 | macro_rules! todo { |
5e7ed085 FG |
772 | () => { |
773 | $crate::panicking::panic("not yet implemented") | |
774 | }; | |
775 | ($($arg:tt)+) => { | |
776 | $crate::panic!("not yet implemented: {}", $crate::format_args!($($arg)+)) | |
777 | }; | |
416331ca XL |
778 | } |
779 | ||
780 | /// Definitions of built-in macros. | |
c30ab7b3 | 781 | /// |
416331ca XL |
782 | /// Most of the macro properties (stability, visibility, etc.) are taken from the source code here, |
783 | /// with exception of expansion functions transforming macro inputs into outputs, | |
784 | /// those functions are provided by the compiler. | |
416331ca | 785 | pub(crate) mod builtin { |
041b39d2 | 786 | |
532ac7d7 | 787 | /// Causes compilation to fail with the given error message when encountered. |
041b39d2 | 788 | /// |
416331ca XL |
789 | /// This macro should be used when a crate uses a conditional compilation strategy to provide |
790 | /// better error messages for erroneous conditions. It's the compiler-level form of [`panic!`], | |
791 | /// but emits an error during *compilation* rather than at *runtime*. | |
792 | /// | |
793 | /// # Examples | |
794 | /// | |
795 | /// Two such examples are macros and `#[cfg]` environments. | |
796 | /// | |
923072b8 | 797 | /// Emit a better compiler error if a macro is passed invalid values. Without the final branch, |
416331ca XL |
798 | /// the compiler would still emit an error, but the error's message would not mention the two |
799 | /// valid values. | |
041b39d2 | 800 | /// |
416331ca XL |
801 | /// ```compile_fail |
802 | /// macro_rules! give_me_foo_or_bar { | |
803 | /// (foo) => {}; | |
804 | /// (bar) => {}; | |
805 | /// ($x:ident) => { | |
806 | /// compile_error!("This macro only accepts `foo` or `bar`"); | |
807 | /// } | |
808 | /// } | |
809 | /// | |
810 | /// give_me_foo_or_bar!(neither); | |
811 | /// // ^ will fail at compile time with message "This macro only accepts `foo` or `bar`" | |
812 | /// ``` | |
813 | /// | |
923072b8 | 814 | /// Emit a compiler error if one of a number of features isn't available. |
416331ca XL |
815 | /// |
816 | /// ```compile_fail | |
817 | /// #[cfg(not(any(feature = "foo", feature = "bar")))] | |
818 | /// compile_error!("Either feature \"foo\" or \"bar\" must be enabled for this crate."); | |
819 | /// ``` | |
041b39d2 | 820 | #[stable(feature = "compile_error_macro", since = "1.20.0")] |
416331ca XL |
821 | #[rustc_builtin_macro] |
822 | #[macro_export] | |
0531ce1d | 823 | macro_rules! compile_error { |
29967ef6 | 824 | ($msg:expr $(,)?) => {{ /* compiler built-in */ }}; |
0531ce1d | 825 | } |
041b39d2 | 826 | |
532ac7d7 | 827 | /// Constructs parameters for the other string-formatting macros. |
c30ab7b3 | 828 | /// |
416331ca XL |
829 | /// This macro functions by taking a formatting string literal containing |
830 | /// `{}` for each additional argument passed. `format_args!` prepares the | |
831 | /// additional parameters to ensure the output can be interpreted as a string | |
832 | /// and canonicalizes the arguments into a single type. Any value that implements | |
833 | /// the [`Display`] trait can be passed to `format_args!`, as can any | |
834 | /// [`Debug`] implementation be passed to a `{:?}` within the formatting string. | |
835 | /// | |
836 | /// This macro produces a value of type [`fmt::Arguments`]. This value can be | |
837 | /// passed to the macros within [`std::fmt`] for performing useful redirection. | |
838 | /// All other formatting macros ([`format!`], [`write!`], [`println!`], etc) are | |
839 | /// proxied through this one. `format_args!`, unlike its derived macros, avoids | |
840 | /// heap allocations. | |
841 | /// | |
842 | /// You can use the [`fmt::Arguments`] value that `format_args!` returns | |
843 | /// in `Debug` and `Display` contexts as seen below. The example also shows | |
844 | /// that `Debug` and `Display` format to the same thing: the interpolated | |
845 | /// format string in `format_args!`. | |
846 | /// | |
847 | /// ```rust | |
848 | /// let debug = format!("{:?}", format_args!("{} foo {:?}", 1, 2)); | |
849 | /// let display = format!("{}", format_args!("{} foo {:?}", 1, 2)); | |
850 | /// assert_eq!("1 foo 2", display); | |
851 | /// assert_eq!(display, debug); | |
852 | /// ``` | |
c30ab7b3 | 853 | /// |
416331ca XL |
854 | /// For more information, see the documentation in [`std::fmt`]. |
855 | /// | |
3dfed10e XL |
856 | /// [`Display`]: crate::fmt::Display |
857 | /// [`Debug`]: crate::fmt::Debug | |
858 | /// [`fmt::Arguments`]: crate::fmt::Arguments | |
5869c6ff | 859 | /// [`std::fmt`]: ../std/fmt/index.html |
416331ca | 860 | /// [`format!`]: ../std/macro.format.html |
416331ca XL |
861 | /// [`println!`]: ../std/macro.println.html |
862 | /// | |
863 | /// # Examples | |
864 | /// | |
865 | /// ``` | |
866 | /// use std::fmt; | |
867 | /// | |
868 | /// let s = fmt::format(format_args!("hello {}", "world")); | |
869 | /// assert_eq!(s, format!("hello {}", "world")); | |
870 | /// ``` | |
c30ab7b3 | 871 | #[stable(feature = "rust1", since = "1.0.0")] |
5099ac24 | 872 | #[cfg_attr(not(test), rustc_diagnostic_item = "format_args_macro")] |
94222f64 | 873 | #[allow_internal_unsafe] |
416331ca XL |
874 | #[allow_internal_unstable(fmt_internals)] |
875 | #[rustc_builtin_macro] | |
876 | #[macro_export] | |
ff7c6d11 | 877 | macro_rules! format_args { |
60c5eb7d XL |
878 | ($fmt:expr) => {{ /* compiler built-in */ }}; |
879 | ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }}; | |
416331ca XL |
880 | } |
881 | ||
5099ac24 | 882 | /// Same as [`format_args`], but can be used in some const contexts. |
94222f64 XL |
883 | /// |
884 | /// This macro is used by the panic macros for the `const_panic` feature. | |
885 | /// | |
886 | /// This macro will be removed once `format_args` is allowed in const contexts. | |
94222f64 XL |
887 | #[unstable(feature = "const_format_args", issue = "none")] |
888 | #[allow_internal_unstable(fmt_internals, const_fmt_arguments_new)] | |
889 | #[rustc_builtin_macro] | |
890 | #[macro_export] | |
891 | macro_rules! const_format_args { | |
892 | ($fmt:expr) => {{ /* compiler built-in */ }}; | |
893 | ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }}; | |
894 | } | |
895 | ||
5099ac24 | 896 | /// Same as [`format_args`], but adds a newline in the end. |
60c5eb7d XL |
897 | #[unstable( |
898 | feature = "format_args_nl", | |
dfeec247 | 899 | issue = "none", |
60c5eb7d XL |
900 | reason = "`format_args_nl` is only for internal \ |
901 | language use and is subject to change" | |
902 | )] | |
416331ca XL |
903 | #[allow_internal_unstable(fmt_internals)] |
904 | #[rustc_builtin_macro] | |
905 | #[macro_export] | |
416331ca | 906 | macro_rules! format_args_nl { |
60c5eb7d XL |
907 | ($fmt:expr) => {{ /* compiler built-in */ }}; |
908 | ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }}; | |
ff7c6d11 | 909 | } |
c30ab7b3 | 910 | |
532ac7d7 | 911 | /// Inspects an environment variable at compile time. |
c30ab7b3 | 912 | /// |
416331ca | 913 | /// This macro will expand to the value of the named environment variable at |
04454e1e FG |
914 | /// compile time, yielding an expression of type `&'static str`. Use |
915 | /// [`std::env::var`] instead if you want to read the value at runtime. | |
916 | /// | |
917 | /// [`std::env::var`]: ../std/env/fn.var.html | |
416331ca XL |
918 | /// |
919 | /// If the environment variable is not defined, then a compilation error | |
920 | /// will be emitted. To not emit a compile error, use the [`option_env!`] | |
921 | /// macro instead. | |
c30ab7b3 | 922 | /// |
416331ca XL |
923 | /// # Examples |
924 | /// | |
925 | /// ``` | |
926 | /// let path: &'static str = env!("PATH"); | |
5e7ed085 | 927 | /// println!("the $PATH variable at the time of compiling was: {path}"); |
416331ca XL |
928 | /// ``` |
929 | /// | |
930 | /// You can customize the error message by passing a string as the second | |
931 | /// parameter: | |
932 | /// | |
933 | /// ```compile_fail | |
934 | /// let doc: &'static str = env!("documentation", "what's that?!"); | |
935 | /// ``` | |
936 | /// | |
937 | /// If the `documentation` environment variable is not defined, you'll get | |
938 | /// the following error: | |
939 | /// | |
940 | /// ```text | |
941 | /// error: what's that?! | |
942 | /// ``` | |
c30ab7b3 | 943 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
944 | #[rustc_builtin_macro] |
945 | #[macro_export] | |
ff7c6d11 | 946 | macro_rules! env { |
29967ef6 | 947 | ($name:expr $(,)?) => {{ /* compiler built-in */ }}; |
6a06907d | 948 | ($name:expr, $error_msg:expr $(,)?) => {{ /* compiler built-in */ }}; |
ff7c6d11 | 949 | } |
c30ab7b3 | 950 | |
532ac7d7 | 951 | /// Optionally inspects an environment variable at compile time. |
c30ab7b3 | 952 | /// |
416331ca XL |
953 | /// If the named environment variable is present at compile time, this will |
954 | /// expand into an expression of type `Option<&'static str>` whose value is | |
955 | /// `Some` of the value of the environment variable. If the environment | |
956 | /// variable is not present, then this will expand to `None`. See | |
04454e1e FG |
957 | /// [`Option<T>`][Option] for more information on this type. Use |
958 | /// [`std::env::var`] instead if you want to read the value at runtime. | |
959 | /// | |
960 | /// [`std::env::var`]: ../std/env/fn.var.html | |
416331ca XL |
961 | /// |
962 | /// A compile time error is never emitted when using this macro regardless | |
963 | /// of whether the environment variable is present or not. | |
c30ab7b3 | 964 | /// |
416331ca XL |
965 | /// # Examples |
966 | /// | |
967 | /// ``` | |
968 | /// let key: Option<&'static str> = option_env!("SECRET_KEY"); | |
5e7ed085 | 969 | /// println!("the secret key might be: {key:?}"); |
416331ca | 970 | /// ``` |
c30ab7b3 | 971 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
972 | #[rustc_builtin_macro] |
973 | #[macro_export] | |
0531ce1d | 974 | macro_rules! option_env { |
29967ef6 | 975 | ($name:expr $(,)?) => {{ /* compiler built-in */ }}; |
0531ce1d | 976 | } |
c30ab7b3 | 977 | |
532ac7d7 | 978 | /// Concatenates identifiers into one identifier. |
c30ab7b3 | 979 | /// |
416331ca XL |
980 | /// This macro takes any number of comma-separated identifiers, and |
981 | /// concatenates them all into one, yielding an expression which is a new | |
982 | /// identifier. Note that hygiene makes it such that this macro cannot | |
983 | /// capture local variables. Also, as a general rule, macros are only | |
984 | /// allowed in item, statement or expression position. That means while | |
985 | /// you may use this macro for referring to existing variables, functions or | |
986 | /// modules etc, you cannot define a new one with it. | |
987 | /// | |
988 | /// # Examples | |
989 | /// | |
990 | /// ``` | |
991 | /// #![feature(concat_idents)] | |
c30ab7b3 | 992 | /// |
416331ca XL |
993 | /// # fn main() { |
994 | /// fn foobar() -> u32 { 23 } | |
995 | /// | |
996 | /// let f = concat_idents!(foo, bar); | |
997 | /// println!("{}", f()); | |
998 | /// | |
999 | /// // fn concat_idents!(new, fun, name) { } // not usable in this way! | |
1000 | /// # } | |
1001 | /// ``` | |
60c5eb7d XL |
1002 | #[unstable( |
1003 | feature = "concat_idents", | |
1004 | issue = "29599", | |
1005 | reason = "`concat_idents` is not stable enough for use and is subject to change" | |
1006 | )] | |
416331ca XL |
1007 | #[rustc_builtin_macro] |
1008 | #[macro_export] | |
c30ab7b3 | 1009 | macro_rules! concat_idents { |
29967ef6 | 1010 | ($($e:ident),+ $(,)?) => {{ /* compiler built-in */ }}; |
c30ab7b3 SL |
1011 | } |
1012 | ||
a2a8927a XL |
1013 | /// Concatenates literals into a byte slice. |
1014 | /// | |
1015 | /// This macro takes any number of comma-separated literals, and concatenates them all into | |
f2b60f7d | 1016 | /// one, yielding an expression of type `&[u8; _]`, which represents all of the literals |
a2a8927a XL |
1017 | /// concatenated left-to-right. The literals passed can be any combination of: |
1018 | /// | |
1019 | /// - byte literals (`b'r'`) | |
1020 | /// - byte strings (`b"Rust"`) | |
1021 | /// - arrays of bytes/numbers (`[b'A', 66, b'C']`) | |
1022 | /// | |
1023 | /// # Examples | |
1024 | /// | |
1025 | /// ``` | |
1026 | /// #![feature(concat_bytes)] | |
1027 | /// | |
1028 | /// # fn main() { | |
1029 | /// let s: &[u8; 6] = concat_bytes!(b'A', b"BC", [68, b'E', 70]); | |
1030 | /// assert_eq!(s, b"ABCDEF"); | |
1031 | /// # } | |
1032 | /// ``` | |
a2a8927a XL |
1033 | #[unstable(feature = "concat_bytes", issue = "87555")] |
1034 | #[rustc_builtin_macro] | |
1035 | #[macro_export] | |
1036 | macro_rules! concat_bytes { | |
1037 | ($($e:literal),+ $(,)?) => {{ /* compiler built-in */ }}; | |
1038 | } | |
1039 | ||
c30ab7b3 SL |
1040 | /// Concatenates literals into a static string slice. |
1041 | /// | |
416331ca XL |
1042 | /// This macro takes any number of comma-separated literals, yielding an |
1043 | /// expression of type `&'static str` which represents all of the literals | |
1044 | /// concatenated left-to-right. | |
1045 | /// | |
1046 | /// Integer and floating point literals are stringified in order to be | |
1047 | /// concatenated. | |
c30ab7b3 | 1048 | /// |
416331ca XL |
1049 | /// # Examples |
1050 | /// | |
1051 | /// ``` | |
1052 | /// let s = concat!("test", 10, 'b', true); | |
1053 | /// assert_eq!(s, "test10btrue"); | |
1054 | /// ``` | |
c30ab7b3 | 1055 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
1056 | #[rustc_builtin_macro] |
1057 | #[macro_export] | |
ff7c6d11 | 1058 | macro_rules! concat { |
29967ef6 | 1059 | ($($e:expr),* $(,)?) => {{ /* compiler built-in */ }}; |
ff7c6d11 | 1060 | } |
c30ab7b3 | 1061 | |
532ac7d7 | 1062 | /// Expands to the line number on which it was invoked. |
c30ab7b3 | 1063 | /// |
416331ca XL |
1064 | /// With [`column!`] and [`file!`], these macros provide debugging information for |
1065 | /// developers about the location within the source. | |
1066 | /// | |
1067 | /// The expanded expression has type `u32` and is 1-based, so the first line | |
1068 | /// in each file evaluates to 1, the second to 2, etc. This is consistent | |
1069 | /// with error messages by common compilers or popular editors. | |
1070 | /// The returned line is *not necessarily* the line of the `line!` invocation itself, | |
1071 | /// but rather the first macro invocation leading up to the invocation | |
1072 | /// of the `line!` macro. | |
1073 | /// | |
416331ca | 1074 | /// # Examples |
c30ab7b3 | 1075 | /// |
416331ca XL |
1076 | /// ``` |
1077 | /// let current_line = line!(); | |
5e7ed085 | 1078 | /// println!("defined on line: {current_line}"); |
416331ca | 1079 | /// ``` |
c30ab7b3 | 1080 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
1081 | #[rustc_builtin_macro] |
1082 | #[macro_export] | |
60c5eb7d XL |
1083 | macro_rules! line { |
1084 | () => { | |
1085 | /* compiler built-in */ | |
1086 | }; | |
1087 | } | |
c30ab7b3 | 1088 | |
416331ca XL |
1089 | /// Expands to the column number at which it was invoked. |
1090 | /// | |
1091 | /// With [`line!`] and [`file!`], these macros provide debugging information for | |
1092 | /// developers about the location within the source. | |
1093 | /// | |
1094 | /// The expanded expression has type `u32` and is 1-based, so the first column | |
1095 | /// in each line evaluates to 1, the second to 2, etc. This is consistent | |
1096 | /// with error messages by common compilers or popular editors. | |
1097 | /// The returned column is *not necessarily* the line of the `column!` invocation itself, | |
1098 | /// but rather the first macro invocation leading up to the invocation | |
1099 | /// of the `column!` macro. | |
1100 | /// | |
416331ca | 1101 | /// # Examples |
c30ab7b3 | 1102 | /// |
416331ca XL |
1103 | /// ``` |
1104 | /// let current_col = column!(); | |
5e7ed085 | 1105 | /// println!("defined on column: {current_col}"); |
416331ca | 1106 | /// ``` |
a2a8927a XL |
1107 | /// |
1108 | /// `column!` counts Unicode code points, not bytes or graphemes. As a result, the first two | |
1109 | /// invocations return the same value, but the third does not. | |
1110 | /// | |
1111 | /// ``` | |
1112 | /// let a = ("foobar", column!()).1; | |
1113 | /// let b = ("人之初性本善", column!()).1; | |
1114 | /// let c = ("f̅o̅o̅b̅a̅r̅", column!()).1; // Uses combining overline (U+0305) | |
1115 | /// | |
1116 | /// assert_eq!(a, b); | |
1117 | /// assert_ne!(b, c); | |
1118 | /// ``` | |
c30ab7b3 | 1119 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
1120 | #[rustc_builtin_macro] |
1121 | #[macro_export] | |
60c5eb7d XL |
1122 | macro_rules! column { |
1123 | () => { | |
1124 | /* compiler built-in */ | |
1125 | }; | |
1126 | } | |
c30ab7b3 | 1127 | |
416331ca XL |
1128 | /// Expands to the file name in which it was invoked. |
1129 | /// | |
1130 | /// With [`line!`] and [`column!`], these macros provide debugging information for | |
1131 | /// developers about the location within the source. | |
1132 | /// | |
416331ca XL |
1133 | /// The expanded expression has type `&'static str`, and the returned file |
1134 | /// is not the invocation of the `file!` macro itself, but rather the | |
1135 | /// first macro invocation leading up to the invocation of the `file!` | |
1136 | /// macro. | |
c30ab7b3 | 1137 | /// |
416331ca XL |
1138 | /// # Examples |
1139 | /// | |
1140 | /// ``` | |
1141 | /// let this_file = file!(); | |
5e7ed085 | 1142 | /// println!("defined in file: {this_file}"); |
416331ca | 1143 | /// ``` |
c30ab7b3 | 1144 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
1145 | #[rustc_builtin_macro] |
1146 | #[macro_export] | |
60c5eb7d XL |
1147 | macro_rules! file { |
1148 | () => { | |
1149 | /* compiler built-in */ | |
1150 | }; | |
1151 | } | |
c30ab7b3 | 1152 | |
532ac7d7 | 1153 | /// Stringifies its arguments. |
c30ab7b3 | 1154 | /// |
416331ca XL |
1155 | /// This macro will yield an expression of type `&'static str` which is the |
1156 | /// stringification of all the tokens passed to the macro. No restrictions | |
1157 | /// are placed on the syntax of the macro invocation itself. | |
1158 | /// | |
1159 | /// Note that the expanded results of the input tokens may change in the | |
1160 | /// future. You should be careful if you rely on the output. | |
c30ab7b3 | 1161 | /// |
416331ca XL |
1162 | /// # Examples |
1163 | /// | |
1164 | /// ``` | |
1165 | /// let one_plus_one = stringify!(1 + 1); | |
1166 | /// assert_eq!(one_plus_one, "1 + 1"); | |
1167 | /// ``` | |
c30ab7b3 | 1168 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
1169 | #[rustc_builtin_macro] |
1170 | #[macro_export] | |
60c5eb7d XL |
1171 | macro_rules! stringify { |
1172 | ($($t:tt)*) => { | |
1173 | /* compiler built-in */ | |
1174 | }; | |
1175 | } | |
c30ab7b3 | 1176 | |
3dfed10e | 1177 | /// Includes a UTF-8 encoded file as a string. |
c30ab7b3 | 1178 | /// |
ba9703b0 XL |
1179 | /// The file is located relative to the current file (similarly to how |
1180 | /// modules are found). The provided path is interpreted in a platform-specific | |
1181 | /// way at compile time. So, for instance, an invocation with a Windows path | |
1182 | /// containing backslashes `\` would not compile correctly on Unix. | |
416331ca XL |
1183 | /// |
1184 | /// This macro will yield an expression of type `&'static str` which is the | |
1185 | /// contents of the file. | |
c30ab7b3 | 1186 | /// |
416331ca XL |
1187 | /// # Examples |
1188 | /// | |
1189 | /// Assume there are two files in the same directory with the following | |
1190 | /// contents: | |
1191 | /// | |
1192 | /// File 'spanish.in': | |
1193 | /// | |
1194 | /// ```text | |
1195 | /// adiós | |
1196 | /// ``` | |
1197 | /// | |
1198 | /// File 'main.rs': | |
1199 | /// | |
1200 | /// ```ignore (cannot-doctest-external-file-dependency) | |
1201 | /// fn main() { | |
1202 | /// let my_str = include_str!("spanish.in"); | |
1203 | /// assert_eq!(my_str, "adiós\n"); | |
5e7ed085 | 1204 | /// print!("{my_str}"); |
416331ca XL |
1205 | /// } |
1206 | /// ``` | |
1207 | /// | |
1208 | /// Compiling 'main.rs' and running the resulting binary will print "adiós". | |
c30ab7b3 | 1209 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
1210 | #[rustc_builtin_macro] |
1211 | #[macro_export] | |
5099ac24 | 1212 | #[cfg_attr(not(test), rustc_diagnostic_item = "include_str_macro")] |
0531ce1d | 1213 | macro_rules! include_str { |
29967ef6 | 1214 | ($file:expr $(,)?) => {{ /* compiler built-in */ }}; |
0531ce1d | 1215 | } |
c30ab7b3 SL |
1216 | |
1217 | /// Includes a file as a reference to a byte array. | |
1218 | /// | |
ba9703b0 XL |
1219 | /// The file is located relative to the current file (similarly to how |
1220 | /// modules are found). The provided path is interpreted in a platform-specific | |
1221 | /// way at compile time. So, for instance, an invocation with a Windows path | |
1222 | /// containing backslashes `\` would not compile correctly on Unix. | |
416331ca XL |
1223 | /// |
1224 | /// This macro will yield an expression of type `&'static [u8; N]` which is | |
1225 | /// the contents of the file. | |
c30ab7b3 | 1226 | /// |
416331ca XL |
1227 | /// # Examples |
1228 | /// | |
1229 | /// Assume there are two files in the same directory with the following | |
1230 | /// contents: | |
1231 | /// | |
1232 | /// File 'spanish.in': | |
1233 | /// | |
1234 | /// ```text | |
1235 | /// adiós | |
1236 | /// ``` | |
1237 | /// | |
1238 | /// File 'main.rs': | |
1239 | /// | |
1240 | /// ```ignore (cannot-doctest-external-file-dependency) | |
1241 | /// fn main() { | |
1242 | /// let bytes = include_bytes!("spanish.in"); | |
1243 | /// assert_eq!(bytes, b"adi\xc3\xb3s\n"); | |
1244 | /// print!("{}", String::from_utf8_lossy(bytes)); | |
1245 | /// } | |
1246 | /// ``` | |
1247 | /// | |
1248 | /// Compiling 'main.rs' and running the resulting binary will print "adiós". | |
c30ab7b3 | 1249 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
1250 | #[rustc_builtin_macro] |
1251 | #[macro_export] | |
5099ac24 | 1252 | #[cfg_attr(not(test), rustc_diagnostic_item = "include_bytes_macro")] |
0531ce1d | 1253 | macro_rules! include_bytes { |
29967ef6 | 1254 | ($file:expr $(,)?) => {{ /* compiler built-in */ }}; |
0531ce1d | 1255 | } |
c30ab7b3 SL |
1256 | |
1257 | /// Expands to a string that represents the current module path. | |
1258 | /// | |
416331ca XL |
1259 | /// The current module path can be thought of as the hierarchy of modules |
1260 | /// leading back up to the crate root. The first component of the path | |
1261 | /// returned is the name of the crate currently being compiled. | |
1262 | /// | |
1263 | /// # Examples | |
1264 | /// | |
1265 | /// ``` | |
1266 | /// mod test { | |
1267 | /// pub fn foo() { | |
1268 | /// assert!(module_path!().ends_with("test")); | |
1269 | /// } | |
1270 | /// } | |
c30ab7b3 | 1271 | /// |
416331ca XL |
1272 | /// test::foo(); |
1273 | /// ``` | |
c30ab7b3 | 1274 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
1275 | #[rustc_builtin_macro] |
1276 | #[macro_export] | |
60c5eb7d XL |
1277 | macro_rules! module_path { |
1278 | () => { | |
1279 | /* compiler built-in */ | |
1280 | }; | |
1281 | } | |
c30ab7b3 | 1282 | |
416331ca | 1283 | /// Evaluates boolean combinations of configuration flags at compile-time. |
c30ab7b3 | 1284 | /// |
416331ca XL |
1285 | /// In addition to the `#[cfg]` attribute, this macro is provided to allow |
1286 | /// boolean expression evaluation of configuration flags. This frequently | |
1287 | /// leads to less duplicated code. | |
c30ab7b3 | 1288 | /// |
416331ca XL |
1289 | /// The syntax given to this macro is the same syntax as the [`cfg`] |
1290 | /// attribute. | |
1291 | /// | |
f9f354fc XL |
1292 | /// `cfg!`, unlike `#[cfg]`, does not remove any code and only evaluates to true or false. For |
1293 | /// example, all blocks in an if/else expression need to be valid when `cfg!` is used for | |
1294 | /// the condition, regardless of what `cfg!` is evaluating. | |
1295 | /// | |
416331ca XL |
1296 | /// [`cfg`]: ../reference/conditional-compilation.html#the-cfg-attribute |
1297 | /// | |
1298 | /// # Examples | |
1299 | /// | |
1300 | /// ``` | |
1301 | /// let my_directory = if cfg!(windows) { | |
1302 | /// "windows-specific-directory" | |
1303 | /// } else { | |
1304 | /// "unix-directory" | |
1305 | /// }; | |
1306 | /// ``` | |
c30ab7b3 | 1307 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
1308 | #[rustc_builtin_macro] |
1309 | #[macro_export] | |
60c5eb7d XL |
1310 | macro_rules! cfg { |
1311 | ($($cfg:tt)*) => { | |
1312 | /* compiler built-in */ | |
1313 | }; | |
1314 | } | |
c30ab7b3 | 1315 | |
532ac7d7 | 1316 | /// Parses a file as an expression or an item according to the context. |
c30ab7b3 | 1317 | /// |
416331ca | 1318 | /// The file is located relative to the current file (similarly to how |
ba9703b0 XL |
1319 | /// modules are found). The provided path is interpreted in a platform-specific |
1320 | /// way at compile time. So, for instance, an invocation with a Windows path | |
1321 | /// containing backslashes `\` would not compile correctly on Unix. | |
416331ca XL |
1322 | /// |
1323 | /// Using this macro is often a bad idea, because if the file is | |
1324 | /// parsed as an expression, it is going to be placed in the | |
1325 | /// surrounding code unhygienically. This could result in variables | |
1326 | /// or functions being different from what the file expected if | |
1327 | /// there are variables or functions that have the same name in | |
1328 | /// the current file. | |
1329 | /// | |
1330 | /// # Examples | |
1331 | /// | |
1332 | /// Assume there are two files in the same directory with the following | |
1333 | /// contents: | |
1334 | /// | |
1335 | /// File 'monkeys.in': | |
c30ab7b3 | 1336 | /// |
416331ca XL |
1337 | /// ```ignore (only-for-syntax-highlight) |
1338 | /// ['🙈', '🙊', '🙉'] | |
1339 | /// .iter() | |
1340 | /// .cycle() | |
1341 | /// .take(6) | |
1342 | /// .collect::<String>() | |
1343 | /// ``` | |
1344 | /// | |
1345 | /// File 'main.rs': | |
1346 | /// | |
1347 | /// ```ignore (cannot-doctest-external-file-dependency) | |
1348 | /// fn main() { | |
1349 | /// let my_string = include!("monkeys.in"); | |
1350 | /// assert_eq!("🙈🙊🙉🙈🙊🙉", my_string); | |
5e7ed085 | 1351 | /// println!("{my_string}"); |
416331ca XL |
1352 | /// } |
1353 | /// ``` | |
1354 | /// | |
1355 | /// Compiling 'main.rs' and running the resulting binary will print | |
1356 | /// "🙈🙊🙉🙈🙊🙉". | |
c30ab7b3 | 1357 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
1358 | #[rustc_builtin_macro] |
1359 | #[macro_export] | |
0531ce1d | 1360 | macro_rules! include { |
29967ef6 | 1361 | ($file:expr $(,)?) => {{ /* compiler built-in */ }}; |
0531ce1d XL |
1362 | } |
1363 | ||
532ac7d7 | 1364 | /// Asserts that a boolean expression is `true` at runtime. |
0531ce1d | 1365 | /// |
416331ca XL |
1366 | /// This will invoke the [`panic!`] macro if the provided expression cannot be |
1367 | /// evaluated to `true` at runtime. | |
1368 | /// | |
1369 | /// # Uses | |
1370 | /// | |
1371 | /// Assertions are always checked in both debug and release builds, and cannot | |
1372 | /// be disabled. See [`debug_assert!`] for assertions that are not enabled in | |
1373 | /// release builds by default. | |
1374 | /// | |
f9f354fc | 1375 | /// Unsafe code may rely on `assert!` to enforce run-time invariants that, if |
416331ca XL |
1376 | /// violated could lead to unsafety. |
1377 | /// | |
1378 | /// Other use-cases of `assert!` include testing and enforcing run-time | |
1379 | /// invariants in safe code (whose violation cannot result in unsafety). | |
1380 | /// | |
1381 | /// # Custom Messages | |
1382 | /// | |
1383 | /// This macro has a second form, where a custom panic message can | |
1384 | /// be provided with or without arguments for formatting. See [`std::fmt`] | |
6a06907d XL |
1385 | /// for syntax for this form. Expressions used as format arguments will only |
1386 | /// be evaluated if the assertion fails. | |
416331ca | 1387 | /// |
5869c6ff | 1388 | /// [`std::fmt`]: ../std/fmt/index.html |
416331ca XL |
1389 | /// |
1390 | /// # Examples | |
1391 | /// | |
1392 | /// ``` | |
1393 | /// // the panic message for these assertions is the stringified value of the | |
1394 | /// // expression given. | |
1395 | /// assert!(true); | |
1396 | /// | |
1397 | /// fn some_computation() -> bool { true } // a very simple function | |
1398 | /// | |
1399 | /// assert!(some_computation()); | |
1400 | /// | |
1401 | /// // assert with a custom message | |
1402 | /// let x = true; | |
1403 | /// assert!(x, "x wasn't true!"); | |
0531ce1d | 1404 | /// |
416331ca XL |
1405 | /// let a = 3; let b = 27; |
1406 | /// assert!(a + b == 30, "a = {}, b = {}", a, b); | |
1407 | /// ``` | |
0531ce1d | 1408 | #[stable(feature = "rust1", since = "1.0.0")] |
416331ca XL |
1409 | #[rustc_builtin_macro] |
1410 | #[macro_export] | |
5869c6ff XL |
1411 | #[rustc_diagnostic_item = "assert_macro"] |
1412 | #[allow_internal_unstable(core_panic, edition_panic)] | |
0531ce1d | 1413 | macro_rules! assert { |
29967ef6 | 1414 | ($cond:expr $(,)?) => {{ /* compiler built-in */ }}; |
60c5eb7d | 1415 | ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }}; |
0531ce1d | 1416 | } |
416331ca | 1417 | |
416331ca | 1418 | /// Prints passed tokens into the standard output. |
60c5eb7d XL |
1419 | #[unstable( |
1420 | feature = "log_syntax", | |
1421 | issue = "29598", | |
1422 | reason = "`log_syntax!` is not stable enough for use and is subject to change" | |
1423 | )] | |
416331ca XL |
1424 | #[rustc_builtin_macro] |
1425 | #[macro_export] | |
60c5eb7d XL |
1426 | macro_rules! log_syntax { |
1427 | ($($arg:tt)*) => { | |
1428 | /* compiler built-in */ | |
1429 | }; | |
1430 | } | |
416331ca XL |
1431 | |
1432 | /// Enables or disables tracing functionality used for debugging other macros. | |
60c5eb7d XL |
1433 | #[unstable( |
1434 | feature = "trace_macros", | |
1435 | issue = "29598", | |
1436 | reason = "`trace_macros` is not stable enough for use and is subject to change" | |
1437 | )] | |
416331ca XL |
1438 | #[rustc_builtin_macro] |
1439 | #[macro_export] | |
1440 | macro_rules! trace_macros { | |
60c5eb7d XL |
1441 | (true) => {{ /* compiler built-in */ }}; |
1442 | (false) => {{ /* compiler built-in */ }}; | |
416331ca XL |
1443 | } |
1444 | ||
6a06907d | 1445 | /// Attribute macro used to apply derive macros. |
a2a8927a XL |
1446 | /// |
1447 | /// See [the reference] for more info. | |
1448 | /// | |
1449 | /// [the reference]: ../../../reference/attributes/derive.html | |
6a06907d XL |
1450 | #[stable(feature = "rust1", since = "1.0.0")] |
1451 | #[rustc_builtin_macro] | |
1452 | pub macro derive($item:item) { | |
1453 | /* compiler built-in */ | |
1454 | } | |
1455 | ||
487cf647 FG |
1456 | /// Attribute macro used to apply derive macros for implementing traits |
1457 | /// in a const context. | |
1458 | /// | |
1459 | /// See [the reference] for more info. | |
1460 | /// | |
1461 | /// [the reference]: ../../../reference/attributes/derive.html | |
1462 | #[unstable(feature = "derive_const", issue = "none")] | |
1463 | #[rustc_builtin_macro] | |
1464 | #[cfg(not(bootstrap))] | |
1465 | pub macro derive_const($item:item) { | |
1466 | /* compiler built-in */ | |
1467 | } | |
1468 | ||
416331ca | 1469 | /// Attribute macro applied to a function to turn it into a unit test. |
a2a8927a XL |
1470 | /// |
1471 | /// See [the reference] for more info. | |
1472 | /// | |
1473 | /// [the reference]: ../../../reference/attributes/testing.html#the-test-attribute | |
416331ca XL |
1474 | #[stable(feature = "rust1", since = "1.0.0")] |
1475 | #[allow_internal_unstable(test, rustc_attrs)] | |
1476 | #[rustc_builtin_macro] | |
60c5eb7d XL |
1477 | pub macro test($item:item) { |
1478 | /* compiler built-in */ | |
1479 | } | |
416331ca XL |
1480 | |
1481 | /// Attribute macro applied to a function to turn it into a benchmark test. | |
60c5eb7d XL |
1482 | #[unstable( |
1483 | feature = "test", | |
1484 | issue = "50297", | |
1485 | soft, | |
1486 | reason = "`bench` is a part of custom test frameworks which are unstable" | |
1487 | )] | |
416331ca XL |
1488 | #[allow_internal_unstable(test, rustc_attrs)] |
1489 | #[rustc_builtin_macro] | |
60c5eb7d XL |
1490 | pub macro bench($item:item) { |
1491 | /* compiler built-in */ | |
1492 | } | |
416331ca XL |
1493 | |
1494 | /// An implementation detail of the `#[test]` and `#[bench]` macros. | |
60c5eb7d XL |
1495 | #[unstable( |
1496 | feature = "custom_test_frameworks", | |
1497 | issue = "50297", | |
1498 | reason = "custom test frameworks are an unstable feature" | |
1499 | )] | |
416331ca XL |
1500 | #[allow_internal_unstable(test, rustc_attrs)] |
1501 | #[rustc_builtin_macro] | |
60c5eb7d XL |
1502 | pub macro test_case($item:item) { |
1503 | /* compiler built-in */ | |
1504 | } | |
416331ca XL |
1505 | |
1506 | /// Attribute macro applied to a static to register it as a global allocator. | |
29967ef6 | 1507 | /// |
a2a8927a | 1508 | /// See also [`std::alloc::GlobalAlloc`](../../../std/alloc/trait.GlobalAlloc.html). |
416331ca XL |
1509 | #[stable(feature = "global_allocator", since = "1.28.0")] |
1510 | #[allow_internal_unstable(rustc_attrs)] | |
1511 | #[rustc_builtin_macro] | |
60c5eb7d XL |
1512 | pub macro global_allocator($item:item) { |
1513 | /* compiler built-in */ | |
1514 | } | |
416331ca | 1515 | |
487cf647 FG |
1516 | /// Attribute macro applied to a function to register it as a handler for allocation failure. |
1517 | /// | |
1518 | /// See also [`std::alloc::handle_alloc_error`](../../../std/alloc/fn.handle_alloc_error.html). | |
1519 | #[cfg(not(bootstrap))] | |
1520 | #[unstable(feature = "alloc_error_handler", issue = "51540")] | |
1521 | #[allow_internal_unstable(rustc_attrs)] | |
1522 | #[rustc_builtin_macro] | |
1523 | pub macro alloc_error_handler($item:item) { | |
1524 | /* compiler built-in */ | |
1525 | } | |
1526 | ||
ba9703b0 | 1527 | /// Keeps the item it's applied to if the passed path is accessible, and removes it otherwise. |
ba9703b0 XL |
1528 | #[unstable( |
1529 | feature = "cfg_accessible", | |
1530 | issue = "64797", | |
1531 | reason = "`cfg_accessible` is not fully implemented" | |
1532 | )] | |
1533 | #[rustc_builtin_macro] | |
1534 | pub macro cfg_accessible($item:item) { | |
1535 | /* compiler built-in */ | |
1536 | } | |
1537 | ||
6a06907d | 1538 | /// Expands all `#[cfg]` and `#[cfg_attr]` attributes in the code fragment it's applied to. |
6a06907d XL |
1539 | #[unstable( |
1540 | feature = "cfg_eval", | |
1541 | issue = "82679", | |
1542 | reason = "`cfg_eval` is a recently implemented feature" | |
1543 | )] | |
1544 | #[rustc_builtin_macro] | |
1545 | pub macro cfg_eval($($tt:tt)*) { | |
1546 | /* compiler built-in */ | |
487cf647 FG |
1547 | } |
1548 | ||
1549 | /// Unstable placeholder for type ascription. | |
1550 | #[rustc_builtin_macro] | |
1551 | #[unstable( | |
1552 | feature = "type_ascription", | |
1553 | issue = "23416", | |
1554 | reason = "placeholder syntax for type ascription" | |
1555 | )] | |
1556 | #[cfg(not(bootstrap))] | |
1557 | pub macro type_ascribe($expr:expr, $ty:ty) { | |
1558 | /* compiler built-in */ | |
6a06907d XL |
1559 | } |
1560 | ||
416331ca XL |
1561 | /// Unstable implementation detail of the `rustc` compiler, do not use. |
1562 | #[rustc_builtin_macro] | |
416331ca | 1563 | #[stable(feature = "rust1", since = "1.0.0")] |
064997fb | 1564 | #[allow_internal_unstable(core_intrinsics, libstd_sys_internals, rt)] |
04454e1e | 1565 | #[deprecated(since = "1.52.0", note = "rustc-serialize is deprecated and no longer supported")] |
a2a8927a | 1566 | #[doc(hidden)] // While technically stable, using it is unstable, and deprecated. Hide it. |
60c5eb7d XL |
1567 | pub macro RustcDecodable($item:item) { |
1568 | /* compiler built-in */ | |
1569 | } | |
416331ca XL |
1570 | |
1571 | /// Unstable implementation detail of the `rustc` compiler, do not use. | |
1572 | #[rustc_builtin_macro] | |
416331ca | 1573 | #[stable(feature = "rust1", since = "1.0.0")] |
064997fb | 1574 | #[allow_internal_unstable(core_intrinsics, rt)] |
04454e1e | 1575 | #[deprecated(since = "1.52.0", note = "rustc-serialize is deprecated and no longer supported")] |
a2a8927a | 1576 | #[doc(hidden)] // While technically stable, using it is unstable, and deprecated. Hide it. |
60c5eb7d XL |
1577 | pub macro RustcEncodable($item:item) { |
1578 | /* compiler built-in */ | |
1579 | } | |
c30ab7b3 | 1580 | } |