4 ms·
Technically, you don't need a macro at all: use std::io::Write; fn main() { std::io::stdout().write(b"Hello, world!\n").unwrap(); } The r
by WaffleIronMaker 4y ago
Technically, you don't need a macro at all:
use std::io::Write;
fn main() {
std::io::stdout().write(b"Hello, world!\n").unwrap();
}
The reason people often use println! is that println! is variadic and can support any number of arguments to format in the string. For example:
println!("x is: {}", x);
This also has the benefit of allowing type checking at compile time, as opposed to using a function like printf in C, which does not have such power. Additionally, this allows Rust to take references to the variables entered without the programmer's specification, in order to prevent unnecessary copying.
These reasons tie directly into Rust's philosophy of making it easy to write reliable, performant code.