Skip to content

Commit 2d6e8b4

Browse files
authored
Implement println! using nested format_args! (#987)
Our previous usage of `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}"); ``` Signed-off-by: Klimenty Tsoutsman <[email protected]>
1 parent bb66e6a commit 2d6e8b4

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)