pub trait Write {
// Required method
fn write_str(&mut self, s: &str) -> Result<(), Error>;
// Provided methods
fn write_char(&mut self, c: char) -> Result<(), Error> { ... }
fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error> { ... }
}
Expand description
A trait for writing or formatting into Unicode-accepting buffers or streams.
This trait only accepts UTF-8âencoded data and is not flushable. If you only want to accept Unicode and you donât need flushing, you should implement this trait; otherwise you should implement std::io::Write
.
Writes a string slice into this writer, returning whether the write succeeded.
This method can only succeed if the entire string slice was successfully written, and this method will not return until all data has been written or an error occurs.
§ErrorsThis function will return an instance of std::fmt::Error
on error.
The purpose of that error is to abort the formatting operation when the underlying destination encounters some error preventing it from accepting more text; in particular, it does not communicate any information about what error occurred. It should generally be propagated rather than handled, at least when implementing formatting traits.
§Examplesuse std::fmt::{Error, Write};
fn writer<W: Write>(f: &mut W, s: &str) -> Result<(), Error> {
f.write_str(s)
}
let mut buf = String::new();
writer(&mut buf, "hola")?;
assert_eq!(&buf, "hola");
1.1.0 · Source
Writes a char
into this writer, returning whether the write succeeded.
A single char
may be encoded as more than one byte. This method can only succeed if the entire byte sequence was successfully written, and this method will not return until all data has been written or an error occurs.
This function will return an instance of Error
on error.
use std::fmt::{Error, Write};
fn writer<W: Write>(f: &mut W, c: char) -> Result<(), Error> {
f.write_char(c)
}
let mut buf = String::new();
writer(&mut buf, 'a')?;
writer(&mut buf, 'b')?;
assert_eq!(&buf, "ab");
1.0.0 · Source
Glue for usage of the write!
macro with implementors of this trait.
This method should generally not be invoked manually, but rather through the write!
macro itself.
This function will return an instance of Error
on error. Please see write_str for details.
use std::fmt::{Error, Write};
fn writer<W: Write>(f: &mut W, s: &str) -> Result<(), Error> {
f.write_fmt(format_args!("{s}"))
}
let mut buf = String::new();
writer(&mut buf, "world")?;
assert_eq!(&buf, "world");
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4