1.0.0[−][src]Macro std::writeln
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
).
For more information, see write!
. For information on the format string syntax, see
std::fmt
.
Examples
use std::io::{Write, Result}; fn main() -> Result<()> { let mut w = Vec::new(); writeln!(&mut w)?; writeln!(&mut w, "test")?; writeln!(&mut w, "formatted {}", "arguments")?; assert_eq!(&w[..], "\ntest\nformatted arguments\n".as_bytes()); Ok(()) }Run
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; fn main() -> Result<(), Box<dyn std::error::Error>> { let mut s = String::new(); let mut v = Vec::new(); writeln!(&mut s, "{} {}", "abc", 123)?; // uses fmt::Write::write_fmt writeln!(&mut v, "s = {:?}", s)?; // uses io::Write::write_fmt assert_eq!(v, b"s = \"abc 123\\n\"\n"); Ok(()) }Run