Skip to content

Commit ba6fdbc

Browse files
committed
Implement println! using nested format_args!
`concat!` prevents `format_args!` from capturing variables from the surrounding scope. Implementing `println!` using a nested `format_args!` removes this limitation allowing us to do the following: ```rust let x = 3; println!("{x}"); ``` Similar to `std::println!`. Also, the change [does not impact performance][1]. [1]: rust-lang/rust#106824 Signed-off-by: Klimenty Tsoutsman <[email protected]>
1 parent 18dc5f5 commit ba6fdbc

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

kernel/app_io/src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,10 @@ pub fn line_discipline() -> Result<Arc<LineDiscipline>, &'static str> {
188188
/// Calls `print!()` with an extra newline ('\n') appended to the end.
189189
#[macro_export]
190190
macro_rules! println {
191-
($fmt:expr) => ($crate::print!(concat!($fmt, "\n")));
192-
($fmt:expr, $($arg:tt)*) => ($crate::print!(concat!($fmt, "\n"), $($arg)*));
191+
() => ($crate::print!("\n"));
192+
($($arg:tt)*) => ({
193+
$crate::print_to_stdout_args(::core::format_args!("{}\n", ::core::format_args!($($arg)*)));
194+
});
193195

194196
}
195197

@@ -198,7 +200,7 @@ macro_rules! println {
198200
#[macro_export]
199201
macro_rules! print {
200202
($($arg:tt)*) => ({
201-
$crate::print_to_stdout_args(format_args!($($arg)*));
203+
$crate::print_to_stdout_args(::core::format_args!($($arg)*));
202204
});
203205
}
204206

0 commit comments

Comments
 (0)