diff options
| author | bors <bors@rust-lang.org> | 2018-09-25 07:06:15 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-09-25 07:06:15 +0000 |
| commit | e7416d565048efdc42efdb143eb7bb0b382f5273 (patch) | |
| tree | c1742cf3ac950f1e4dcfe48e3a1d72db216cb3ba /src/libstd | |
| parent | 287187230b8d76686883d1ea32940294ec0fd5c9 (diff) | |
| parent | e5b9331a863c071954bcfbaa4446888414e87249 (diff) | |
| download | rust-e7416d565048efdc42efdb143eb7bb0b382f5273.tar.gz rust-e7416d565048efdc42efdb143eb7bb0b382f5273.zip | |
Auto merge of #54317 - Centril:feature/dbg_macro, r=SimonSapin
Implement the dbg!(..) macro Implements the `dbg!(..)` macro due to #54306. cc https://github.com/rust-lang/rfcs/pull/2361 r? @alexcrichton
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/macros.rs | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 06056d6ed20..16a3aecfc18 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -220,6 +220,126 @@ macro_rules! eprintln { }) } +/// A macro for quick and dirty debugging with which you can inspect +/// the value of a given expression. An example: +/// +/// ```rust +/// #![feature(dbg_macro)] +/// +/// let a = 2; +/// let b = dbg!(a * 2) + 1; +/// // ^-- prints: [src/main.rs:4] a * 2 = 4 +/// assert_eq!(b, 5); +/// ``` +/// +/// The macro works by using the `Debug` implementation of the type of +/// the given expression to print the value to [stderr] along with the +/// source location of the macro invocation as well as the source code +/// of the expression. +/// +/// Invoking the macro on an expression moves and takes ownership of it +/// before returning the evaluated expression unchanged. If the type +/// of the expression does not implement `Copy` and you don't want +/// to give up ownership, you can instead borrow with `dbg!(&expr)` +/// for some expression `expr`. +/// +/// Note that the macro is intended as a debugging tool and therefore you +/// should avoid having uses of it in version control for longer periods. +/// Use cases involving debug output that should be added to version control +/// may be better served by macros such as `debug!` from the `log` crate. +/// +/// # Stability +/// +/// The exact output printed by this macro should not be relied upon +/// and is subject to future changes. +/// +/// # Panics +/// +/// Panics if writing to `io::stderr` fails. +/// +/// # Further examples +/// +/// With a method call: +/// +/// ```rust +/// #![feature(dbg_macro)] +/// +/// fn foo(n: usize) { +/// if let Some(_) = dbg!(n.checked_sub(4)) { +/// // ... +/// } +/// } +/// +/// foo(3) +/// ``` +/// +/// This prints to [stderr]: +/// +/// ```text,ignore +/// [src/main.rs:4] n.checked_sub(4) = None +/// ``` +/// +/// Naive factorial implementation: +/// +/// ```rust +/// #![feature(dbg_macro)] +/// +/// fn factorial(n: u32) -> u32 { +/// if dbg!(n <= 1) { +/// dbg!(1) +/// } else { +/// dbg!(n * factorial(n - 1)) +/// } +/// } +/// +/// dbg!(factorial(4)); +/// ``` +/// +/// This prints to [stderr]: +/// +/// ```text,ignore +/// [src/main.rs:3] n <= 1 = false +/// [src/main.rs:3] n <= 1 = false +/// [src/main.rs:3] n <= 1 = false +/// [src/main.rs:3] n <= 1 = true +/// [src/main.rs:4] 1 = 1 +/// [src/main.rs:5] n * factorial(n - 1) = 2 +/// [src/main.rs:5] n * factorial(n - 1) = 6 +/// [src/main.rs:5] n * factorial(n - 1) = 24 +/// [src/main.rs:11] factorial(4) = 24 +/// ``` +/// +/// The `dbg!(..)` macro moves the input: +/// +/// ```compile_fail +/// #![feature(dbg_macro)] +/// +/// /// A wrapper around `usize` which importantly is not Copyable. +/// #[derive(Debug)] +/// struct NoCopy(usize); +/// +/// let a = NoCopy(42); +/// let _ = dbg!(a); // <-- `a` is moved here. +/// let _ = dbg!(a); // <-- `a` is moved again; error! +/// ``` +/// +/// [stderr]: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr) +#[macro_export] +#[unstable(feature = "dbg_macro", issue = "54306")] +macro_rules! dbg { + ($val:expr) => { + // Use of `match` here is intentional because it affects the lifetimes + // of temporaries - https://stackoverflow.com/a/48732525/1063961 + match $val { + tmp => { + eprintln!("[{}:{}] {} = {:#?}", + file!(), line!(), stringify!($val), &tmp); + tmp + } + } + } +} + #[macro_export] #[unstable(feature = "await_macro", issue = "50547")] #[allow_internal_unstable] |
