std::io::Write - Rust (original) (raw)

Trait std::io::Write1.0.0 [−] [src]

pub trait Write { fn write(&mut self, buf: &u8[]) -> Result<usize>; fn flush(&mut self) -> Result<()>;

fn [write_all](#method.write%5Fall)(&mut self, buf: [&[](../primitive.slice.html)[u8](../primitive.u8.html)[]](../primitive.slice.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)> { ... }
fn [write_fmt](#method.write%5Ffmt)(&mut self, fmt: [Arguments](../../std/fmt/struct.Arguments.html "struct std::fmt::Arguments")) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)> { ... }
fn [by_ref](#method.by%5Fref)(&mut self) -> [&mut ](../primitive.reference.html)Self  

    where
        Self: Sized, { ... } }

A trait for objects which are byte-oriented sinks.

Implementors of the Write trait are sometimes called 'writers'.

Writers are defined by two required methods, write and flush:

Writers are intended to be composable with one another. Many implementors throughout std::io take and provide types which implement the Writetrait.

use std::io::prelude::*; use std::fs::File;

let mut buffer = File::create("foo.txt")?;

buffer.write(b"some bytes")?;Run

fn [write](#tymethod.write)(&mut self, buf: [&[](../primitive.slice.html)[u8](../primitive.u8.html)[]](../primitive.slice.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>

Write a buffer into this object, returning how many bytes were written.

This function will attempt to write the entire contents of buf, but the entire write may not succeed, or the write may also generate an error. A call to write represents at most one attempt to write to any wrapped object.

Calls to write are not guaranteed to block waiting for data to be written, and a write which would otherwise block can be indicated through an Err variant.

If the return value is Ok(n) then it must be guaranteed that0 <= n <= buf.len(). A return value of 0 typically means that the underlying object is no longer able to accept bytes and will likely not be able to in the future as well, or that the buffer provided is empty.

Each call to write may generate an I/O error indicating that the operation could not be completed. If an error is returned then no bytes in the buffer were written to this writer.

It is not considered an error if the entire buffer could not be written to this writer.

An error of the ErrorKind::Interrupted kind is non-fatal and the write operation should be retried if there is nothing else to do.

use std::io::prelude::*; use std::fs::File;

let mut buffer = File::create("foo.txt")?;

buffer.write(b"some bytes")?;Run

fn [flush](#tymethod.flush)(&mut self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination.

It is considered an error if not all bytes could be written due to I/O errors or EOF being reached.

use std::io::prelude::*; use std::io::BufWriter; use std::fs::File;

let mut buffer = BufWriter::new(File::create("foo.txt")?);

buffer.write(b"some bytes")?; buffer.flush()?;Run

fn [write_all](#method.write%5Fall)(&mut self, buf: [&[](../primitive.slice.html)[u8](../primitive.u8.html)[]](../primitive.slice.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>

Attempts to write an entire buffer into this write.

This method will continuously call write until there is no more data to be written or an error of non-ErrorKind::Interrupted kind is returned. This method will not return until the entire buffer has been successfully written or such an error occurs. The first error that is not of ErrorKind::Interrupted kind generated from this method will be returned.

This function will return the first error of non-ErrorKind::Interrupted kind that write returns.

use std::io::prelude::*; use std::fs::File;

let mut buffer = File::create("foo.txt")?;

buffer.write_all(b"some bytes")?;Run

fn [write_fmt](#method.write%5Ffmt)(&mut self, fmt: [Arguments](../../std/fmt/struct.Arguments.html "struct std::fmt::Arguments")) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>

Writes a formatted string into this writer, returning any error encountered.

This method is primarily used to interface with theformat_args! macro, but it is rare that this should explicitly be called. The write! macro should be favored to invoke this method instead.

This function internally uses the write_all method on this trait and hence will continuously write data so long as no errors are received. This also means that partial writes are not indicated in this signature.

This function will return any I/O error reported while formatting.

use std::io::prelude::*; use std::fs::File;

let mut buffer = File::create("foo.txt")?;

write!(buffer, "{:.*}", 2, 1.234567)?;

buffer.write_fmt(format_args!("{:.*}", 2, 1.234567))?;Run

`fn by_ref(&mut self) -> &mut Self where

Self: Sized, `

Creates a "by reference" adaptor for this instance of Write.

The returned adaptor also implements Write and will simply borrow this current writer.

use std::io::Write; use std::fs::File;

let mut buffer = File::create("foo.txt")?;

let reference = buffer.by_ref();

reference.write_all(b"some bytes")?;Run