macros.rs 23.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
10 11 12 13 14 15 16

//! Standard library macros
//!
//! This modules contains a set of macros which are exported from the standard
//! library. Each macro is available for use when linking against the standard
//! library.

17
/// The entry point for panic of Rust threads.
18
///
19 20 21 22 23 24 25 26 27
/// This allows a program to to terminate immediately and provide feedback
/// to the caller of the program. `panic!` should be used when a program reaches
/// an unrecoverable problem.
///
/// This macro is the perfect way to assert conditions in example code and in
/// tests.  `panic!` is closely tied with the `unwrap` method of both [`Option`]
/// and [`Result`][runwrap] enums.  Both implementations call `panic!` when they are set
/// to None or Err variants.
///
28
/// This macro is used to inject panic into a Rust thread, causing the thread to
29 30 31
/// panic entirely. Each thread's panic can be reaped as the `Box<Any>` type,
/// and the single-argument form of the `panic!` macro will be the value which
/// is transmitted.
32
///
33 34 35 36 37
/// [`Result`] enum is often a better solution for recovering from errors than
/// using the `panic!` macro.  This macro should be used to avoid proceeding using
/// incorrect values, such as from external sources.  Detailed information about
/// error handling is found in the [book].
///
38
/// The multi-argument form of this macro panics with a string and has the
39 40 41 42 43 44 45
/// [`format!`] syntax for building a string.
///
/// [runwrap]: ../std/result/enum.Result.html#method.unwrap
/// [`Option`]: ../std/option/enum.Option.html#method.unwrap
/// [`Result`]: ../std/result/enum.Result.html
/// [`format!`]: ../std/macro.format.html
/// [book]: ../book/second-edition/ch09-01-unrecoverable-errors-with-panic.html
46
///
47 48
/// # Current implementation
///
S
Stefan Schindler 已提交
49 50
/// If the main thread panics it will terminate all your threads and end your
/// program with code `101`.
51
///
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/// # Examples
///
/// ```should_panic
/// # #![allow(unreachable_code)]
/// panic!();
/// panic!("this is a terrible mistake!");
/// panic!(4); // panic with the value of 4 to be collected elsewhere
/// panic!("this is a {} {message}", "fancy", message = "message");
/// ```
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable]
macro_rules! panic {
    () => ({
        panic!("explicit panic")
    });
    ($msg:expr) => ({
69
        $crate::rt::begin_panic($msg, &(file!(), line!(), __rust_unstable_column!()))
70 71
    });
    ($fmt:expr, $($arg:tt)+) => ({
72 73
        $crate::rt::begin_panic_fmt(&format_args!($fmt, $($arg)+),
                                    &(file!(), line!(), __rust_unstable_column!()))
74 75 76
    });
}

77 78
/// Macro for printing to the standard output.
///
79
/// Equivalent to the [`println!`] macro except that a newline is not printed at
80
/// the end of the message.
81 82
///
/// Note that stdout is frequently line-buffered by default so it may be
83
/// necessary to use [`io::stdout().flush()`][flush] to ensure the output is emitted
84
/// immediately.
85
///
86
/// Use `print!` only for the primary output of your program.  Use
87 88 89 90 91
/// [`eprint!`] instead to print error and progress messages.
///
/// [`println!`]: ../std/macro.println.html
/// [flush]: ../std/io/trait.Write.html#tymethod.flush
/// [`eprint!`]: ../std/macro.eprint.html
92
///
93 94 95 96
/// # Panics
///
/// Panics if writing to `io::stdout()` fails.
///
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/// # Examples
///
/// ```
/// use std::io::{self, Write};
///
/// print!("this ");
/// print!("will ");
/// print!("be ");
/// print!("on ");
/// print!("the ");
/// print!("same ");
/// print!("line ");
///
/// io::stdout().flush().unwrap();
///
/// print!("this string has a newline, why not choose println! instead?\n");
///
/// io::stdout().flush().unwrap();
/// ```
116
#[macro_export]
B
Brian Anderson 已提交
117
#[stable(feature = "rust1", since = "1.0.0")]
118
#[allow_internal_unstable]
119
macro_rules! print {
120
    ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
121 122
}

123 124 125
/// Macro for printing to the standard output, with a newline.
///
/// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`) alone
126
/// (no additional CARRIAGE RETURN (`\r`/`U+000D`).
127
///
128 129
/// Use the [`format!`] syntax to write data to the standard output.
/// See [`std::fmt`] for more information.
130
///
131
/// Use `println!` only for the primary output of your program.  Use
132
/// [`eprintln!`] instead to print error and progress messages.
133
///
134 135
/// [`format!`]: ../std/macro.format.html
/// [`std::fmt`]: ../std/fmt/index.html
A
Andy Gauge 已提交
136
/// [`eprintln!`]: ../std/macro.eprint.html
137 138
/// # Panics
///
Z
Zack Weinberg 已提交
139
/// Panics if writing to `io::stdout` fails.
140
///
S
Steve Klabnik 已提交
141
/// # Examples
142 143
///
/// ```
144
/// println!(); // prints just a newline
145 146 147 148
/// println!("hello there!");
/// println!("format {} arguments", "some");
/// ```
#[macro_export]
B
Brian Anderson 已提交
149
#[stable(feature = "rust1", since = "1.0.0")]
150
macro_rules! println {
151
    () => (print!("\n"));
152 153
    ($fmt:expr) => (print!(concat!($fmt, "\n")));
    ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
154 155
}

156 157
/// Macro for printing to the standard error.
///
158 159
/// Equivalent to the [`print!`] macro, except that output goes to
/// [`io::stderr`] instead of `io::stdout`.  See [`print!`] for
160 161 162 163 164
/// example usage.
///
/// Use `eprint!` only for error and progress messages.  Use `print!`
/// instead for the primary output of your program.
///
165 166 167
/// [`io::stderr`]: ../std/io/struct.Stderr.html
/// [`print!`]: ../std/macro.print.html
///
168 169
/// # Panics
///
Z
Zack Weinberg 已提交
170
/// Panics if writing to `io::stderr` fails.
171 172 173 174
///
/// # Examples
///
/// ```
175
/// eprint!("Error: Could not complete task");
176
/// ```
177
#[macro_export]
178
#[stable(feature = "eprint", since = "1.19.0")]
179 180 181 182 183 184 185
#[allow_internal_unstable]
macro_rules! eprint {
    ($($arg:tt)*) => ($crate::io::_eprint(format_args!($($arg)*)));
}

/// Macro for printing to the standard error, with a newline.
///
186 187
/// Equivalent to the [`println!`] macro, except that output goes to
/// [`io::stderr`] instead of `io::stdout`.  See [`println!`] for
188 189 190 191 192
/// example usage.
///
/// Use `eprintln!` only for error and progress messages.  Use `println!`
/// instead for the primary output of your program.
///
193 194 195
/// [`io::stderr`]: ../std/io/struct.Stderr.html
/// [`println!`]: ../std/macro.println.html
///
196 197
/// # Panics
///
Z
Zack Weinberg 已提交
198
/// Panics if writing to `io::stderr` fails.
199 200 201 202
///
/// # Examples
///
/// ```
203
/// eprintln!("Error: Could not complete task");
204
/// ```
205
#[macro_export]
206
#[stable(feature = "eprint", since = "1.19.0")]
207 208 209 210 211 212
macro_rules! eprintln {
    () => (eprint!("\n"));
    ($fmt:expr) => (eprint!(concat!($fmt, "\n")));
    ($fmt:expr, $($arg:tt)*) => (eprint!(concat!($fmt, "\n"), $($arg)*));
}

213
/// A macro to select an event from a number of receivers.
A
Alex Crichton 已提交
214 215
///
/// This macro is used to wait for the first event to occur on a number of
216 217
/// receivers. It places no restrictions on the types of receivers given to
/// this macro, this can be viewed as a heterogeneous select.
A
Alex Crichton 已提交
218
///
S
Steve Klabnik 已提交
219
/// # Examples
A
Alex Crichton 已提交
220 221
///
/// ```
222 223
/// #![feature(mpsc_select)]
///
A
Aaron Turon 已提交
224
/// use std::thread;
S
Steve Klabnik 已提交
225 226 227
/// use std::sync::mpsc;
///
/// // two placeholder functions for now
228
/// fn long_running_thread() {}
S
Steve Klabnik 已提交
229
/// fn calculate_the_answer() -> u32 { 42 }
230
///
S
Steve Klabnik 已提交
231 232
/// let (tx1, rx1) = mpsc::channel();
/// let (tx2, rx2) = mpsc::channel();
A
Alex Crichton 已提交
233
///
234
/// thread::spawn(move|| { long_running_thread(); tx1.send(()).unwrap(); });
A
Aaron Turon 已提交
235
/// thread::spawn(move|| { tx2.send(calculate_the_answer()).unwrap(); });
A
Alex Crichton 已提交
236
///
237
/// select! {
238
///     _ = rx1.recv() => println!("the long running thread finished first"),
239
///     answer = rx2.recv() => {
240
///         println!("the answer was: {}", answer.unwrap());
A
Alex Crichton 已提交
241
///     }
242 243 244
/// }
/// # drop(rx1.recv());
/// # drop(rx2.recv());
A
Alex Crichton 已提交
245 246
/// ```
///
247
/// For more information about select, see the `std::sync::mpsc::Select` structure.
A
Alex Crichton 已提交
248
#[macro_export]
249
#[unstable(feature = "mpsc_select", issue = "27800")]
A
Alex Crichton 已提交
250 251
macro_rules! select {
    (
252
        $($name:pat = $rx:ident.$meth:ident() => $code:expr),+
A
Alex Crichton 已提交
253
    ) => ({
254
        use $crate::sync::mpsc::Select;
A
Alex Crichton 已提交
255
        let sel = Select::new();
256
        $( let mut $rx = sel.handle(&$rx); )+
A
Alex Crichton 已提交
257
        unsafe {
258
            $( $rx.add(); )+
A
Alex Crichton 已提交
259 260
        }
        let ret = sel.wait();
261
        $( if ret == $rx.id() { let $name = $rx.$meth(); $code } else )+
A
Alex Crichton 已提交
262 263 264
        { unreachable!() }
    })
}
265

266 267 268 269 270 271 272 273 274
#[cfg(test)]
macro_rules! assert_approx_eq {
    ($a:expr, $b:expr) => ({
        let (a, b) = (&$a, &$b);
        assert!((*a - *b).abs() < 1.0e-6,
                "{} is not approximately equal to {}", *a, *b);
    })
}

275 276 277 278 279 280 281
/// Built-in macros to the compiler itself.
///
/// These macros do not have any corresponding definition with a `macro_rules!`
/// macro, but are documented here. Their implementations can be found hardcoded
/// into libsyntax itself.
#[cfg(dox)]
pub mod builtin {
W
Wesley Wiser 已提交
282 283 284 285 286 287

    /// Unconditionally causes compilation to fail with the given error message when encountered.
    ///
    /// For more information, see the [RFC].
    ///
    /// [RFC]: https://github.com/rust-lang/rfcs/blob/master/text/1695-add-error-macro.md
288
    #[stable(feature = "compile_error_macro", since = "1.20.0")]
W
Wesley Wiser 已提交
289 290 291
    #[macro_export]
    macro_rules! compile_error { ($msg:expr) => ({ /* compiler built-in */ }) }

292 293
    /// The core macro for formatted string creation & output.
    ///
294 295 296 297 298 299 300
    /// This macro functions by taking a formatting string literal containing
    /// `{}` for each additional argument passed.  `format_args!` prepares the
    /// additional parameters to ensure the output can be interpreted as a string
    /// and canonicalizes the arguments into a single type.  Any value that implements
    /// the [`Display`] trait can be passed to `format_args!`, as can any
    /// [`Debug`] implementation be passed to a `{:?}` within the formatting string.
    ///
301
    /// This macro produces a value of type [`fmt::Arguments`]. This value can be
302
    /// passed to the macros within [`std::fmt`] for performing useful redirection.
303
    /// All other formatting macros ([`format!`], [`write!`], [`println!`], etc) are
304 305
    /// proxied through this one.  `format_args!`, unlike its derived macros, avoids
    /// heap allocations.
306
    ///
307 308
    /// For more information, see the documentation in [`std::fmt`].
    ///
309 310
    /// [`Display`]: ../std/fmt/trait.Display.html
    /// [`Debug`]: ../std/fmt/trait.Debug.html
311 312 313 314 315
    /// [`fmt::Arguments`]: ../std/fmt/struct.Arguments.html
    /// [`std::fmt`]: ../std/fmt/index.html
    /// [`format!`]: ../std/macro.format.html
    /// [`write!`]: ../std/macro.write.html
    /// [`println!`]: ../std/macro.println.html
316
    ///
S
Steve Klabnik 已提交
317
    /// # Examples
318
    ///
319
    /// ```
320 321
    /// use std::fmt;
    ///
322
    /// let s = fmt::format(format_args!("hello {}", "world"));
323 324 325
    /// assert_eq!(s, format!("hello {}", "world"));
    ///
    /// ```
326
    #[stable(feature = "rust1", since = "1.0.0")]
327
    #[macro_export]
A
Alex Crichton 已提交
328
    macro_rules! format_args { ($fmt:expr, $($args:tt)*) => ({
329
        /* compiler built-in */
330
    }) }
331 332 333 334 335 336 337

    /// Inspect an environment variable at compile time.
    ///
    /// This macro will expand to the value of the named environment variable at
    /// compile time, yielding an expression of type `&'static str`.
    ///
    /// If the environment variable is not defined, then a compilation error
338
    /// will be emitted.  To not emit a compile error, use the [`option_env!`]
339 340
    /// macro instead.
    ///
341 342
    /// [`option_env!`]: ../std/macro.option_env.html
    ///
S
Steve Klabnik 已提交
343
    /// # Examples
344
    ///
345
    /// ```
346 347
    /// let path: &'static str = env!("PATH");
    /// println!("the $PATH variable at the time of compiling was: {}", path);
348
    /// ```
349
    #[stable(feature = "rust1", since = "1.0.0")]
350
    #[macro_export]
351
    macro_rules! env { ($name:expr) => ({ /* compiler built-in */ }) }
352 353 354 355 356 357

    /// Optionally inspect an environment variable at compile time.
    ///
    /// If the named environment variable is present at compile time, this will
    /// expand into an expression of type `Option<&'static str>` whose value is
    /// `Some` of the value of the environment variable. If the environment
358 359
    /// variable is not present, then this will expand to `None`.  See
    /// [`Option<T>`][option] for more information on this type.
360 361 362 363
    ///
    /// A compile time error is never emitted when using this macro regardless
    /// of whether the environment variable is present or not.
    ///
364 365
    /// [option]: ../std/option/enum.Option.html
    ///
S
Steve Klabnik 已提交
366
    /// # Examples
367
    ///
368
    /// ```
369
    /// let key: Option<&'static str> = option_env!("SECRET_KEY");
A
Alex Crichton 已提交
370
    /// println!("the secret key might be: {:?}", key);
371
    /// ```
372
    #[stable(feature = "rust1", since = "1.0.0")]
373
    #[macro_export]
374
    macro_rules! option_env { ($name:expr) => ({ /* compiler built-in */ }) }
375 376 377 378 379 380

    /// Concatenate identifiers into one identifier.
    ///
    /// This macro takes any number of comma-separated identifiers, and
    /// concatenates them all into one, yielding an expression which is a new
    /// identifier. Note that hygiene makes it such that this macro cannot
381 382 383 384
    /// capture local variables. Also, as a general rule, macros are only
    /// allowed in item, statement or expression position. That means while
    /// you may use this macro for referring to existing variables, functions or
    /// modules etc, you cannot define a new one with it.
385
    ///
S
Steve Klabnik 已提交
386
    /// # Examples
387 388
    ///
    /// ```
A
Alex Crichton 已提交
389 390
    /// #![feature(concat_idents)]
    ///
S
Steve Klabnik 已提交
391
    /// # fn main() {
S
Steve Klabnik 已提交
392
    /// fn foobar() -> u32 { 23 }
393 394 395
    ///
    /// let f = concat_idents!(foo, bar);
    /// println!("{}", f());
396 397
    ///
    /// // fn concat_idents!(new, fun, name) { } // not usable in this way!
S
Steve Klabnik 已提交
398
    /// # }
399
    /// ```
400
    #[unstable(feature = "concat_idents_macro", issue = "29599")]
401
    #[macro_export]
402 403 404
    macro_rules! concat_idents {
        ($($e:ident),*) => ({ /* compiler built-in */ })
    }
405 406 407 408 409 410 411 412 413 414

    /// Concatenates literals into a static string slice.
    ///
    /// This macro takes any number of comma-separated literals, yielding an
    /// expression of type `&'static str` which represents all of the literals
    /// concatenated left-to-right.
    ///
    /// Integer and floating point literals are stringified in order to be
    /// concatenated.
    ///
S
Steve Klabnik 已提交
415
    /// # Examples
416 417
    ///
    /// ```
T
Tobias Bucher 已提交
418
    /// let s = concat!("test", 10, 'b', true);
419 420
    /// assert_eq!(s, "test10btrue");
    /// ```
421
    #[stable(feature = "rust1", since = "1.0.0")]
422
    #[macro_export]
423
    macro_rules! concat { ($($e:expr),*) => ({ /* compiler built-in */ }) }
424 425 426

    /// A macro which expands to the line number on which it was invoked.
    ///
427 428 429
    /// With [`column!`] and [`file!`], these macros provide debugging information for
    /// developers about the location within the source.
    ///
S
Steve Klabnik 已提交
430
    /// The expanded expression has type `u32`, and the returned line is not
431 432 433
    /// the invocation of the `line!()` macro itself, but rather the first macro
    /// invocation leading up to the invocation of the `line!()` macro.
    ///
434 435 436
    /// [`column!`]: macro.column.html
    /// [`file!`]: macro.file.html
    ///
S
Steve Klabnik 已提交
437
    /// # Examples
438 439 440 441 442
    ///
    /// ```
    /// let current_line = line!();
    /// println!("defined on line: {}", current_line);
    /// ```
443
    #[stable(feature = "rust1", since = "1.0.0")]
444
    #[macro_export]
445
    macro_rules! line { () => ({ /* compiler built-in */ }) }
446 447 448

    /// A macro which expands to the column number on which it was invoked.
    ///
449 450 451
    /// With [`line!`] and [`file!`], these macros provide debugging information for
    /// developers about the location within the source.
    ///
S
Steve Klabnik 已提交
452
    /// The expanded expression has type `u32`, and the returned column is not
453 454 455 456 457
    /// the invocation of the `column!` macro itself, but rather the first macro
    /// invocation leading up to the invocation of the `column!` macro.
    ///
    /// [`line!`]: macro.line.html
    /// [`file!`]: macro.file.html
458
    ///
S
Steve Klabnik 已提交
459
    /// # Examples
460 461
    ///
    /// ```
H
Huon Wilson 已提交
462
    /// let current_col = column!();
463 464
    /// println!("defined on column: {}", current_col);
    /// ```
465
    #[stable(feature = "rust1", since = "1.0.0")]
466
    #[macro_export]
467
    macro_rules! column { () => ({ /* compiler built-in */ }) }
468 469 470

    /// A macro which expands to the file name from which it was invoked.
    ///
471 472 473 474
    /// With [`line!`] and [`column!`], these macros provide debugging information for
    /// developers about the location within the source.
    ///
    ///
475
    /// The expanded expression has type `&'static str`, and the returned file
476 477
    /// is not the invocation of the `file!` macro itself, but rather the
    /// first macro invocation leading up to the invocation of the `file!`
478 479
    /// macro.
    ///
480 481 482
    /// [`line!`]: macro.line.html
    /// [`column!`]: macro.column.html
    ///
S
Steve Klabnik 已提交
483
    /// # Examples
484 485 486 487 488
    ///
    /// ```
    /// let this_file = file!();
    /// println!("defined in file: {}", this_file);
    /// ```
489
    #[stable(feature = "rust1", since = "1.0.0")]
490
    #[macro_export]
491
    macro_rules! file { () => ({ /* compiler built-in */ }) }
492

A
Alex Burka 已提交
493
    /// A macro which stringifies its arguments.
494 495 496 497 498
    ///
    /// This macro will yield an expression of type `&'static str` which is the
    /// stringification of all the tokens passed to the macro. No restrictions
    /// are placed on the syntax of the macro invocation itself.
    ///
499 500 501
    /// Note that the expanded results of the input tokens may change in the
    /// future. You should be careful if you rely on the output.
    ///
S
Steve Klabnik 已提交
502
    /// # Examples
503 504 505 506 507
    ///
    /// ```
    /// let one_plus_one = stringify!(1 + 1);
    /// assert_eq!(one_plus_one, "1 + 1");
    /// ```
508
    #[stable(feature = "rust1", since = "1.0.0")]
509
    #[macro_export]
A
Alex Burka 已提交
510
    macro_rules! stringify { ($($t:tt)*) => ({ /* compiler built-in */ }) }
511 512 513

    /// Includes a utf8-encoded file as a string.
    ///
514 515 516
    /// The file is located relative to the current file. (similarly to how
    /// modules are found)
    ///
517
    /// This macro will yield an expression of type `&'static str` which is the
518
    /// contents of the file.
519
    ///
S
Steve Klabnik 已提交
520
    /// # Examples
521
    ///
522 523 524 525 526 527 528 529 530 531 532
    /// Assume there are two files in the same directory with the following
    /// contents:
    ///
    /// File 'spanish.in':
    ///
    /// ```text
    /// adiós
    /// ```
    ///
    /// File 'main.rs':
    ///
533
    /// ```ignore (cannot-doctest-external-file-dependency)
534 535 536 537 538
    /// fn main() {
    ///     let my_str = include_str!("spanish.in");
    ///     assert_eq!(my_str, "adiós\n");
    ///     print!("{}", my_str);
    /// }
539
    /// ```
540 541
    ///
    /// Compiling 'main.rs' and running the resulting binary will print "adiós".
542
    #[stable(feature = "rust1", since = "1.0.0")]
543
    #[macro_export]
544
    macro_rules! include_str { ($file:expr) => ({ /* compiler built-in */ }) }
545

546
    /// Includes a file as a reference to a byte array.
547
    ///
548 549 550
    /// The file is located relative to the current file. (similarly to how
    /// modules are found)
    ///
551
    /// This macro will yield an expression of type `&'static [u8; N]` which is
552
    /// the contents of the file.
553
    ///
S
Steve Klabnik 已提交
554
    /// # Examples
555
    ///
556 557 558 559 560 561 562 563 564 565 566
    /// Assume there are two files in the same directory with the following
    /// contents:
    ///
    /// File 'spanish.in':
    ///
    /// ```text
    /// adiós
    /// ```
    ///
    /// File 'main.rs':
    ///
567
    /// ```ignore (cannot-doctest-external-file-dependency)
568 569 570 571 572
    /// fn main() {
    ///     let bytes = include_bytes!("spanish.in");
    ///     assert_eq!(bytes, b"adi\xc3\xb3s\n");
    ///     print!("{}", String::from_utf8_lossy(bytes));
    /// }
573
    /// ```
574 575
    ///
    /// Compiling 'main.rs' and running the resulting binary will print "adiós".
576
    #[stable(feature = "rust1", since = "1.0.0")]
577
    #[macro_export]
C
Chris Wong 已提交
578 579
    macro_rules! include_bytes { ($file:expr) => ({ /* compiler built-in */ }) }

580 581 582 583 584 585
    /// Expands to a string that represents the current module path.
    ///
    /// The current module path can be thought of as the hierarchy of modules
    /// leading back up to the crate root. The first component of the path
    /// returned is the name of the crate currently being compiled.
    ///
S
Steve Klabnik 已提交
586
    /// # Examples
587
    ///
588
    /// ```
589 590 591 592 593 594 595 596
    /// mod test {
    ///     pub fn foo() {
    ///         assert!(module_path!().ends_with("test"));
    ///     }
    /// }
    ///
    /// test::foo();
    /// ```
597
    #[stable(feature = "rust1", since = "1.0.0")]
598
    #[macro_export]
599
    macro_rules! module_path { () => ({ /* compiler built-in */ }) }
600 601 602 603 604 605 606

    /// Boolean evaluation of configuration flags.
    ///
    /// In addition to the `#[cfg]` attribute, this macro is provided to allow
    /// boolean expression evaluation of configuration flags. This frequently
    /// leads to less duplicated code.
    ///
A
Alex Burka 已提交
607
    /// The syntax given to this macro is the same syntax as [the `cfg`
608
    /// attribute](../book/first-edition/conditional-compilation.html).
609
    ///
S
Steve Klabnik 已提交
610
    /// # Examples
611
    ///
612
    /// ```
613 614 615 616 617 618
    /// let my_directory = if cfg!(windows) {
    ///     "windows-specific-directory"
    /// } else {
    ///     "unix-directory"
    /// };
    /// ```
619
    #[stable(feature = "rust1", since = "1.0.0")]
620
    #[macro_export]
A
Alex Burka 已提交
621
    macro_rules! cfg { ($($cfg:tt)*) => ({ /* compiler built-in */ }) }
S
Steve Klabnik 已提交
622

623 624
    /// Parse a file as an expression or an item according to the context.
    ///
625 626
    /// The file is located relative to the current file (similarly to how
    /// modules are found).
C
christopherdumas 已提交
627 628 629
    ///
    /// Using this macro is often a bad idea, because if the file is
    /// parsed as an expression, it is going to be placed in the
C
Corey Farwell 已提交
630
    /// surrounding code unhygienically. This could result in variables
C
christopherdumas 已提交
631 632 633
    /// or functions being different from what the file expected if
    /// there are variables or functions that have the same name in
    /// the current file.
S
Steve Klabnik 已提交
634 635 636
    ///
    /// # Examples
    ///
637 638 639
    /// Assume there are two files in the same directory with the following
    /// contents:
    ///
640
    /// File 'monkeys.in':
641
    ///
642
    /// ```ignore (only-for-syntax-highlight)
643 644 645 646 647
    /// ['🙈', '🙊', '🙉']
    ///     .iter()
    ///     .cycle()
    ///     .take(6)
    ///     .collect::<String>()
648 649 650 651
    /// ```
    ///
    /// File 'main.rs':
    ///
652
    /// ```ignore (cannot-doctest-external-file-dependency)
653
    /// fn main() {
654 655 656
    ///     let my_string = include!("monkeys.in");
    ///     assert_eq!("🙈🙊🙉🙈🙊🙉", my_string);
    ///     println!("{}", my_string);
S
Steve Klabnik 已提交
657 658
    /// }
    /// ```
659
    ///
660 661
    /// Compiling 'main.rs' and running the resulting binary will print
    /// "🙈🙊🙉🙈🙊🙉".
662
    #[stable(feature = "rust1", since = "1.0.0")]
S
Steve Klabnik 已提交
663
    #[macro_export]
A
Alex Burka 已提交
664
    macro_rules! include { ($file:expr) => ({ /* compiler built-in */ }) }
665
}
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701

/// A macro for defining #[cfg] if-else statements.
///
/// This is similar to the `if/elif` C preprocessor macro by allowing definition
/// of a cascade of `#[cfg]` cases, emitting the implementation which matches
/// first.
///
/// This allows you to conveniently provide a long list #[cfg]'d blocks of code
/// without having to rewrite each clause multiple times.
macro_rules! cfg_if {
    ($(
        if #[cfg($($meta:meta),*)] { $($it:item)* }
    ) else * else {
        $($it2:item)*
    }) => {
        __cfg_if_items! {
            () ;
            $( ( ($($meta),*) ($($it)*) ), )*
            ( () ($($it2)*) ),
        }
    }
}

macro_rules! __cfg_if_items {
    (($($not:meta,)*) ; ) => {};
    (($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
        __cfg_if_apply! { cfg(all(not(any($($not),*)), $($m,)*)), $($it)* }
        __cfg_if_items! { ($($not,)* $($m,)*) ; $($rest)* }
    }
}

macro_rules! __cfg_if_apply {
    ($m:meta, $($it:item)*) => {
        $(#[$m] $it)*
    }
}