Skip to content

Commit

Permalink
Remove concat! from println! macro
Browse files Browse the repository at this point in the history
`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 <klim@tsoutsman.com>
  • Loading branch information
tsoutsman committed Jun 24, 2023
1 parent 18dc5f5 commit 275856c
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions kernel/app_io/src/lib.rs
Expand Up @@ -188,8 +188,10 @@ pub fn line_discipline() -> Result<Arc<LineDiscipline>, &'static str> {
/// Calls `print!()` with an extra newline ('\n') appended to the end.
#[macro_export]
macro_rules! println {
($fmt:expr) => ($crate::print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => ($crate::print!(concat!($fmt, "\n"), $($arg)*));
() => ($crate::print!("\n"));
($($arg:tt)*) => ({
$crate::print_to_stdout_args(::core::format_args!("{}\n", ::core::format_args!($($arg)*)));
});

}

Expand All @@ -198,7 +200,7 @@ macro_rules! println {
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ({
$crate::print_to_stdout_args(format_args!($($arg)*));
$crate::print_to_stdout_args(::core::format_args!($($arg)*));
});
}

Expand Down

0 comments on commit 275856c

Please sign in to comment.