1 //! Standard library macros
3 //! This modules contains a set of macros which are exported from the standard
4 //! library. Each macro is available for use when linking against the standard
7 #[doc(include = "../libcore/macros/panic.md")]
9 #[stable(feature = "rust1", since = "1.0.0")]
10 #[allow_internal_unstable(libstd_sys_internals)]
13 $crate::panic!("explicit panic")
16 $crate::rt::begin_panic($msg, &($crate::file!(), $crate::line!(), $crate::column!()))
21 ($fmt:expr, $($arg:tt)+) => ({
22 $crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+),
23 &($crate::file!(), $crate::line!(), $crate::column!()))
27 /// Prints to the standard output.
29 /// Equivalent to the [`println!`] macro except that a newline is not printed at
30 /// the end of the message.
32 /// Note that stdout is frequently line-buffered by default so it may be
33 /// necessary to use [`io::stdout().flush()`][flush] to ensure the output is emitted
36 /// Use `print!` only for the primary output of your program. Use
37 /// [`eprint!`] instead to print error and progress messages.
39 /// [`println!`]: ../std/macro.println.html
40 /// [flush]: ../std/io/trait.Write.html#tymethod.flush
41 /// [`eprint!`]: ../std/macro.eprint.html
45 /// Panics if writing to `io::stdout()` fails.
50 /// use std::io::{self, Write};
60 /// io::stdout().flush().unwrap();
62 /// print!("this string has a newline, why not choose println! instead?\n");
64 /// io::stdout().flush().unwrap();
67 #[stable(feature = "rust1", since = "1.0.0")]
68 #[allow_internal_unstable(print_internals)]
70 ($($arg:tt)*) => ($crate::io::_print($crate::format_args!($($arg)*)));
73 /// Prints to the standard output, with a newline.
75 /// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`) alone
76 /// (no additional CARRIAGE RETURN (`\r`/`U+000D`)).
78 /// Use the [`format!`] syntax to write data to the standard output.
79 /// See [`std::fmt`] for more information.
81 /// Use `println!` only for the primary output of your program. Use
82 /// [`eprintln!`] instead to print error and progress messages.
84 /// [`format!`]: ../std/macro.format.html
85 /// [`std::fmt`]: ../std/fmt/index.html
86 /// [`eprintln!`]: ../std/macro.eprintln.html
89 /// Panics if writing to `io::stdout` fails.
94 /// println!(); // prints just a newline
95 /// println!("hello there!");
96 /// println!("format {} arguments", "some");
99 #[stable(feature = "rust1", since = "1.0.0")]
100 #[allow_internal_unstable(print_internals, format_args_nl)]
101 macro_rules! println {
102 () => ($crate::print!("\n"));
104 $crate::io::_print($crate::format_args_nl!($($arg)*));
108 /// Prints to the standard error.
110 /// Equivalent to the [`print!`] macro, except that output goes to
111 /// [`io::stderr`] instead of `io::stdout`. See [`print!`] for
114 /// Use `eprint!` only for error and progress messages. Use `print!`
115 /// instead for the primary output of your program.
117 /// [`io::stderr`]: ../std/io/struct.Stderr.html
118 /// [`print!`]: ../std/macro.print.html
122 /// Panics if writing to `io::stderr` fails.
127 /// eprint!("Error: Could not complete task");
130 #[stable(feature = "eprint", since = "1.19.0")]
131 #[allow_internal_unstable(print_internals)]
132 macro_rules! eprint {
133 ($($arg:tt)*) => ($crate::io::_eprint($crate::format_args!($($arg)*)));
136 /// Prints to the standard error, with a newline.
138 /// Equivalent to the [`println!`] macro, except that output goes to
139 /// [`io::stderr`] instead of `io::stdout`. See [`println!`] for
142 /// Use `eprintln!` only for error and progress messages. Use `println!`
143 /// instead for the primary output of your program.
145 /// [`io::stderr`]: ../std/io/struct.Stderr.html
146 /// [`println!`]: ../std/macro.println.html
150 /// Panics if writing to `io::stderr` fails.
155 /// eprintln!("Error: Could not complete task");
158 #[stable(feature = "eprint", since = "1.19.0")]
159 #[allow_internal_unstable(print_internals, format_args_nl)]
160 macro_rules! eprintln {
161 () => ($crate::eprint!("\n"));
163 $crate::io::_eprint($crate::format_args_nl!($($arg)*));
167 /// Prints and returns the value of a given expression for quick and dirty
174 /// let b = dbg!(a * 2) + 1;
175 /// // ^-- prints: [src/main.rs:2] a * 2 = 4
176 /// assert_eq!(b, 5);
179 /// The macro works by using the `Debug` implementation of the type of
180 /// the given expression to print the value to [stderr] along with the
181 /// source location of the macro invocation as well as the source code
182 /// of the expression.
184 /// Invoking the macro on an expression moves and takes ownership of it
185 /// before returning the evaluated expression unchanged. If the type
186 /// of the expression does not implement `Copy` and you don't want
187 /// to give up ownership, you can instead borrow with `dbg!(&expr)`
188 /// for some expression `expr`.
190 /// The `dbg!` macro works exactly the same in release builds.
191 /// This is useful when debugging issues that only occur in release
192 /// builds or when debugging in release mode is significantly faster.
194 /// Note that the macro is intended as a debugging tool and therefore you
195 /// should avoid having uses of it in version control for longer periods.
196 /// Use cases involving debug output that should be added to version control
197 /// are better served by macros such as [`debug!`] from the [`log`] crate.
201 /// The exact output printed by this macro should not be relied upon
202 /// and is subject to future changes.
206 /// Panics if writing to `io::stderr` fails.
208 /// # Further examples
210 /// With a method call:
213 /// fn foo(n: usize) {
214 /// if let Some(_) = dbg!(n.checked_sub(4)) {
222 /// This prints to [stderr]:
225 /// [src/main.rs:4] n.checked_sub(4) = None
228 /// Naive factorial implementation:
231 /// fn factorial(n: u32) -> u32 {
232 /// if dbg!(n <= 1) {
235 /// dbg!(n * factorial(n - 1))
239 /// dbg!(factorial(4));
242 /// This prints to [stderr]:
245 /// [src/main.rs:3] n <= 1 = false
246 /// [src/main.rs:3] n <= 1 = false
247 /// [src/main.rs:3] n <= 1 = false
248 /// [src/main.rs:3] n <= 1 = true
249 /// [src/main.rs:4] 1 = 1
250 /// [src/main.rs:5] n * factorial(n - 1) = 2
251 /// [src/main.rs:5] n * factorial(n - 1) = 6
252 /// [src/main.rs:5] n * factorial(n - 1) = 24
253 /// [src/main.rs:11] factorial(4) = 24
256 /// The `dbg!(..)` macro moves the input:
259 /// /// A wrapper around `usize` which importantly is not Copyable.
261 /// struct NoCopy(usize);
263 /// let a = NoCopy(42);
264 /// let _ = dbg!(a); // <-- `a` is moved here.
265 /// let _ = dbg!(a); // <-- `a` is moved again; error!
268 /// You can also use `dbg!()` without a value to just print the
269 /// file and line whenever it's reached.
271 /// Finally, if you want to `dbg!(..)` multiple values, it will treat them as
272 /// a tuple (and return it, too):
275 /// assert_eq!(dbg!(1usize, 2u32), (1, 2));
278 /// However, a single argument with a trailing comma will still not be treated
279 /// as a tuple, following the convention of ignoring trailing commas in macro
280 /// invocations. You can use a 1-tuple directly if you need one:
283 /// assert_eq!(1, dbg!(1u32,)); // trailing comma ignored
284 /// assert_eq!((1,), dbg!((1u32,))); // 1-tuple
287 /// [stderr]: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)
288 /// [`debug!`]: https://docs.rs/log/*/log/macro.debug.html
289 /// [`log`]: https://crates.io/crates/log
291 #[stable(feature = "dbg_macro", since = "1.32.0")]
294 $crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!());
297 // Use of `match` here is intentional because it affects the lifetimes
298 // of temporaries - https://stackoverflow.com/a/48732525/1063961
301 $crate::eprintln!("[{}:{}] {} = {:#?}",
302 $crate::file!(), $crate::line!(), $crate::stringify!($val), &tmp);
307 // Trailing comma with single argument is ignored
308 ($val:expr,) => { $crate::dbg!($val) };
309 ($($val:expr),+ $(,)?) => {
310 ($($crate::dbg!($val)),+,)
315 macro_rules! assert_approx_eq {
316 ($a:expr, $b:expr) => {{
317 let (a, b) = (&$a, &$b);
318 assert!((*a - *b).abs() < 1.0e-6, "{} is not approximately equal to {}", *a, *b);