std::write

Macro std::write

macro_rules! write {
    ( $ dst : expr , $ ( $ arg : tt ) * ) => { ... };
}

Write formatted data into a buffer

This macro accepts a format string, a list of arguments, and a 'writer'. Arguments will be formatted according to the specified format string and the result will be passed to the writer. The writer may be any value with a write_fmt method; generally this comes from an implementation of either the std::fmt::Write or the std::io::Write trait. The macro returns whatever the 'write_fmt' method returns; commonly a std::fmt::Result, or an io::Result.

See std::fmt for more information on the format string syntax.

Examples

use std::io::Write;

let mut w = Vec::new();
write!(&mut w, "test").unwrap();
write!(&mut w, "formatted {}", "arguments").unwrap();

assert_eq!(w, b"testformatted arguments");

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();
write!(&mut s, "{} {}", "abc", 123).unwrap(); // uses fmt::Write::write_fmt
write!(&mut v, "s = {:?}", s).unwrap(); // uses io::Write::write_fmt
assert_eq!(v, b"s = \"abc 123\"");

© 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.write.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部