pub trait Format {
// Required methods
fn try_format(&self, param: &dyn FormatParam) -> Result<String, FormatError>;
fn try_write_to(
&self,
sink: &mut dyn Write,
param: &dyn FormatParam,
) -> Result<(), FormatError>;
fn builder<'t>(&'t self) -> FormatBuilder<'t, 'static>;
// Provided methods
fn format(&self, param: &dyn FormatParam) -> String { ... }
fn checked_format(&self, param: &dyn FormatParam) -> Option<String> { ... }
fn write_to(&self, sink: &mut dyn Write, param: &dyn FormatParam) { ... }
}Expand description
A template string that can be formatted with runtime arguments.
Implemented for str; String, Box<str>, etc. get it through deref.
See the crate documentation for the supported placeholder syntax.
These one-shot methods parse the template on every call; for a template
used repeatedly, parse it once with Template::parse.
Required Methods§
Sourcefn try_format(&self, param: &dyn FormatParam) -> Result<String, FormatError>
fn try_format(&self, param: &dyn FormatParam) -> Result<String, FormatError>
Formats param into a new String.
§Errors
Returns FormatError::InvalidFormatString for a syntactically
invalid template, FormatError::InsufficientParameters for a
placeholder index beyond the supplied arguments,
FormatError::UnsupportedFormatType when an argument does not
support the requested format type, and FormatError::ExpectedUsize
when a dynamic width/precision argument is not an unsigned integer.
Sourcefn try_write_to(
&self,
sink: &mut dyn Write,
param: &dyn FormatParam,
) -> Result<(), FormatError>
fn try_write_to( &self, sink: &mut dyn Write, param: &dyn FormatParam, ) -> Result<(), FormatError>
Formats param into sink, appending to its current contents.
§Errors
Returns the same errors as try_format, plus
FormatError::WriteFailed if the sink rejects a write. Writing
into a String never fails.
Sourcefn builder<'t>(&'t self) -> FormatBuilder<'t, 'static>
fn builder<'t>(&'t self) -> FormatBuilder<'t, 'static>
Starts a FormatBuilder for adding arguments one at a time.
§Examples
use rtformat::Format;
assert_eq!("{} + {} = {2}".builder().arg(&1).arg(&2).arg(&3).build(), "1 + 2 = 3");Provided Methods§
Sourcefn format(&self, param: &dyn FormatParam) -> String
fn format(&self, param: &dyn FormatParam) -> String
Formats param into a new String.
§Panics
Panics if the template or the arguments are invalid. Use
try_format for a fallible version.
Sourcefn checked_format(&self, param: &dyn FormatParam) -> Option<String>
fn checked_format(&self, param: &dyn FormatParam) -> Option<String>
Like try_format, but returns None on error.
Sourcefn write_to(&self, sink: &mut dyn Write, param: &dyn FormatParam)
fn write_to(&self, sink: &mut dyn Write, param: &dyn FormatParam)
Formats param into sink, appending to its current contents.
§Panics
Panics if the template or the arguments are invalid, or the sink
rejects a write. Use try_write_to for a
fallible version.
§Examples
use rtformat::Format;
let mut log = String::from("log: ");
"user {} ({})".write_to(&mut log, &("ada", 7));
assert_eq!(log, "log: user ada (7)");Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl Format for str
Template implementation for string slices. String, Box<str>, etc.
get it through deref.
impl Format for str
Template implementation for string slices. String, Box<str>, etc.
get it through deref.