在该代码中,println
后面有一个!
:
fn main() {
println!("Hello, world!");
}
在我见过的大多数语言中,打印操作都是一个函数.为什么它是 rust 中的宏?
在该代码中,println
后面有一个!
:
fn main() {
println!("Hello, world!");
}
在我见过的大多数语言中,打印操作都是一个函数.为什么它是 rust 中的宏?
作为程序宏,println!()
能够:
自动引用其参数.例如,这是有效的:
let x = "x".to_string();
println!("{}", x);
println!("{}", x); // Works even though you might expect `x` to have been moved on the previous line.
接受任意数量的参数.
在编译时验证格式字符串占位符和参数是否匹配.这是C's printf()
常见的错误源.
这些都不可能通过简单的函数或方法实现.
另见:
未被发送