Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What benefits are there with making println a macro?

Tags:

rust

println

In this code, there is a ! after the println:

fn main() {
    println!("Hello, world!");
}

In most languages I have seen, the print operation is a function. Why is it a macro in Rust?

like image 207
Parn 23 Avatar asked May 12 '21 19:05

Parn 23


1 Answers

By being a procedural macro, println!() gains the ability to:

  1. Automatically reference its arguments. For example this is valid:

    let x = "x".to_string();
    println!("{}", x);
    println!("{}", x); // Works even though you might expect `x` to have been moved on the previous line.
    
  2. Accept an arbitrary number of arguments.

  3. Validate, at compile time, that the format string placeholders and arguments match up. This is a common source of bugs with C's printf().

None of those are possible with plain functions or methods.

See also:

  • Does println! borrow or own the variable?
  • How can I create a function with a variable number of arguments?
  • Is it possible to write something as complex as `print!` in a pure Rust macro?
  • What is the difference between macros and functions in Rust?
like image 137
Shepmaster Avatar answered Nov 16 '22 03:11

Shepmaster