macro_rules! writeln { ( $ dst : expr ) => { ... }; ( $ dst : expr , $ fmt : expr ) => { ... }; ( $ dst : expr , $ fmt : expr , $ ( $ arg : tt ) * ) => { ... }; }
Write formatted data into a buffer, with a newline appended.
On all platforms, the newline is the LINE FEED character (\n
/U+000A
) alone (no additional CARRIAGE RETURN (\r
/U+000D
).
This macro accepts a 'writer' (any value with a write_fmt
method), a format string, and a list of arguments to format.
The write_fmt
method usually comes from an implementation of std::fmt::Write
or std::io::Write
traits. The term 'writer' refers to an implementation of one of these two traits.
Passed arguments will be formatted according to the specified format string and the resulting string will be passed to the writer, along with the appended newline.
See std::fmt
for more information on format syntax.
write!
returns whatever the 'write_fmt' method returns.
Common return values include: fmt::Result
, io::Result
use std::io::Write; let mut w = Vec::new(); writeln!(&mut w).unwrap(); writeln!(&mut w, "test").unwrap(); writeln!(&mut w, "formatted {}", "arguments").unwrap(); assert_eq!(&w[..], "\ntest\nformatted arguments\n".as_bytes());
A module can import both std::fmt::Write
and std::io::Write
and call write!
on objects implementing either, as objects do not typically implement both. However, the module must import the traits qualified so their names do not conflict:
use std::fmt::Write as FmtWrite; use std::io::Write as IoWrite; let mut s = String::new(); let mut v = Vec::new(); writeln!(&mut s, "{} {}", "abc", 123).unwrap(); // uses fmt::Write::write_fmt writeln!(&mut v, "s = {:?}", s).unwrap(); // uses io::Write::write_fmt assert_eq!(v, b"s = \"abc 123\\n\"\n");
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/macro.writeln.html