Skip to content

Commit 33fe113

Browse files
committed
Auto merge of #59826 - llogiq:multi-dbg, r=SimonSapin
allow multiple args to `dbg!(..)` This closes #59763
2 parents 4530c52 + b641fd3 commit 33fe113

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/libstd/macros.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,22 @@ macro_rules! eprintln {
314314
/// You can also use `dbg!()` without a value to just print the
315315
/// file and line whenever it's reached.
316316
///
317+
/// Finally, if you want to `dbg!(..)` multiple values, it will treat them as
318+
/// a tuple (and return it, too):
319+
///
320+
/// ```
321+
/// assert_eq!(dbg!(1usize, 2u32), (1, 2));
322+
/// ```
323+
///
324+
/// However, a single argument with a trailing comma will still not be treated
325+
/// as a tuple, following the convention of ignoring trailing commas in macro
326+
/// invocations. You can use a 1-tuple directly if you need one:
327+
///
328+
/// ```
329+
/// assert_eq!(1, dbg!(1u32,)); // trailing comma ignored
330+
/// assert_eq!((1,), dbg!((1u32,))); // 1-tuple
331+
/// ```
332+
///
317333
/// [stderr]: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)
318334
/// [`debug!`]: https://docs.rs/log/*/log/macro.debug.html
319335
/// [`log`]: https://crates.io/crates/log
@@ -333,7 +349,12 @@ macro_rules! dbg {
333349
tmp
334350
}
335351
}
336-
}
352+
};
353+
// Trailing comma with single argument is ignored
354+
($val:expr,) => { dbg!($val) };
355+
($($val:expr),+ $(,)?) => {
356+
($(dbg!($val)),+,)
357+
};
337358
}
338359

339360
/// Awaits the completion of an async call.

src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs

+22
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ fn test() {
5454
7331
5555
}));
5656
assert_eq!(foo, 42);
57+
58+
// Test trailing comma:
59+
assert_eq!(("Yeah",), dbg!(("Yeah",)));
60+
61+
// Test multiple arguments:
62+
assert_eq!((1u8, 2u32), dbg!(1,
63+
2));
64+
65+
// Test multiple arguments + trailing comma:
66+
assert_eq!((1u8, 2u32, "Yeah"), dbg!(1u8, 2u32,
67+
"Yeah",));
5768
}
5869

5970
fn validate_stderr(stderr: Vec<String>) {
@@ -85,6 +96,17 @@ fn validate_stderr(stderr: Vec<String>) {
8596

8697
"before",
8798
":51] { foo += 1; eprintln!(\"before\"); 7331 } = 7331",
99+
100+
":59] (\"Yeah\",) = (",
101+
" \"Yeah\",",
102+
")",
103+
104+
":62] 1 = 1",
105+
":62] 2 = 2",
106+
107+
":66] 1u8 = 1",
108+
":66] 2u32 = 2",
109+
":66] \"Yeah\" = \"Yeah\"",
88110
]);
89111
}
90112

0 commit comments

Comments
 (0)