Function std::fmt::write

1.0.0 · source ·
pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result<(), Error>
Expand description

write 函数接受一个输出流,以及一个可以与 format_args! 宏预编译的 Arguments 结构体。

参数将根据指定的格式字符串格式化为提供的输出流。

Examples

基本用法:

use std::fmt;

let mut output = String::new();
fmt::write(&mut output, format_args!("Hello {}!", "world"))
    .expect("Error occurred while trying to write in String");
assert_eq!(output, "Hello world!");
Run

请注意,使用 write! 可能更可取。Example:

use std::fmt::Write;

let mut output = String::new();
write!(&mut output, "Hello {}!", "world")
    .expect("Error occurred while trying to write in String");
assert_eq!(output, "Hello world!");
Run