From e1338bb53e5961e8d8035c56299e1e26504f249f Mon Sep 17 00:00:00 2001 From: SomeAnotherDude <35201287+SomeAnotherDude@users.noreply.github.com> Date: Sat, 31 Mar 2018 11:44:59 +0300 Subject: [PATCH] Add an empty branch to println!() macro to be consistent with std (#423) --- .../second-edition/posts/03-vga-text-buffer/index.md | 7 +++++-- src/vga_buffer.rs | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/blog/content/second-edition/posts/03-vga-text-buffer/index.md b/blog/content/second-edition/posts/03-vga-text-buffer/index.md index 802561fe..0b3bacaa 100644 --- a/blog/content/second-edition/posts/03-vga-text-buffer/index.md +++ b/blog/content/second-edition/posts/03-vga-text-buffer/index.md @@ -528,13 +528,15 @@ Now that we have a global writer, we can add a `println` macro that can be used ```rust macro_rules! println { + () => (print!("\n")); ($fmt:expr) => (print!(concat!($fmt, "\n"))); ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); } ``` -Macros are defined through one or more rules, which are similar to `match` arms. The `println` macro has two rules: The first rule is for invocations with a single argument (e.g. `println!("Hello")`) and the second rule is for invocations with additional parameters (e.g. `println!("{}{}", 4, 2)`). +Macros are defined through one or more rules, which are similar to `match` arms. The `println` macro has three rules: The first rule for is invocations without arguments (e.g `println!()`), the second rule is for invocations with a single argument (e.g. `println!("Hello")`) and the third rule is for invocations with additional parameters (e.g. `println!("{}{}", 4, 2)`). -Both rules simply append a newline character (`\n`) to the format string and then invoke the [`print!` macro], which is defined as: +First line just prints a `\n` symbol which literally means "don't print anything, just break the line". +Last two rules simply append a newline character (`\n`) to the format string and then invoke the [`print!` macro], which is defined as: [`print!` macro]: https://doc.rust-lang.org/nightly/std/macro.print!.html @@ -562,6 +564,7 @@ macro_rules! print { } macro_rules! println { + () => (print!("\n")); ($fmt:expr) => (print!(concat!($fmt, "\n"))); ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); } diff --git a/src/vga_buffer.rs b/src/vga_buffer.rs index cc5ac38a..860d314c 100644 --- a/src/vga_buffer.rs +++ b/src/vga_buffer.rs @@ -149,6 +149,7 @@ macro_rules! print { /// Like the `print!` macro in the standard library, but prints to the VGA text buffer. macro_rules! println { + () => (print!("\n")); ($fmt:expr) => (print!(concat!($fmt, "\n"))); ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); }