Add an empty branch to println!() macro to be consistent with std (#423)

This commit is contained in:
SomeAnotherDude
2018-03-31 11:44:59 +03:00
committed by Philipp Oppermann
parent 5f195a869c
commit e1338bb53e
2 changed files with 6 additions and 2 deletions

View File

@@ -528,13 +528,15 @@ Now that we have a global writer, we can add a `println` macro that can be used
```rust ```rust
macro_rules! println { macro_rules! println {
() => (print!("\n"));
($fmt:expr) => (print!(concat!($fmt, "\n"))); ($fmt:expr) => (print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); ($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 [`print!` macro]: https://doc.rust-lang.org/nightly/std/macro.print!.html
@@ -562,6 +564,7 @@ macro_rules! print {
} }
macro_rules! println { macro_rules! println {
() => (print!("\n"));
($fmt:expr) => (print!(concat!($fmt, "\n"))); ($fmt:expr) => (print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
} }

View File

@@ -149,6 +149,7 @@ macro_rules! print {
/// Like the `print!` macro in the standard library, but prints to the VGA text buffer. /// Like the `print!` macro in the standard library, but prints to the VGA text buffer.
macro_rules! println { macro_rules! println {
() => (print!("\n"));
($fmt:expr) => (print!(concat!($fmt, "\n"))); ($fmt:expr) => (print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
} }