Write — 바이트를 쓰는 트레잇
Write — 바이트를 쓰는 트레잇
Write는 바이트를 스트림에 쓰는 트레잇이에요. writeln!, write! 매크로와 함께 사용돼요.
출처: Rust 공식 문서
본문
pub trait Write {
fn write(&mut self, buf: &[u8]) -> Result<usize>;
fn flush(&mut self) -> Result<()>;
fn write_all(&mut self, buf: &[u8]) -> Result<()> { ... }
fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<()> { ... }
// ... 외
}
필수 메서드
// 버퍼에서 최대 buf.len() 바이트를 써요. 쓴 바이트 수를 반환해요.
fn write(&mut self, buf: &[u8]) -> Result<usize>;
// 버퍼링된 데이터를 플러시해요.
fn flush(&mut self) -> Result<()>;
File, Stdout, Vec<u8>(바이트 버퍼), TcpStream 등이 Write를 구현해요. 안전한 쓰기를 위해 write_all을 권장해요.