pub trait AsStrFormatExt: AsRef<str> {
// Provided method
fn format<'a, T: Display + ?Sized + 'a>(
&self,
args: impl IntoIterator<Item = &'a T> + Clone,
) -> String { ... }
}
Expand description
Extends strings with the format
method, which is a runtime analog of the format!
macro.
Unavailable in no_std
environment.
Provided Methods§
Sourcefn format<'a, T: Display + ?Sized + 'a>(
&self,
args: impl IntoIterator<Item = &'a T> + Clone,
) -> String
fn format<'a, T: Display + ?Sized + 'a>( &self, args: impl IntoIterator<Item = &'a T> + Clone, ) -> String
Creates a String
replacing the {}s within self
using provided parameters in the order given.
A runtime analog of format!
macro. In contrast with the macro format string have not be a string literal.
§Examples:
use dyn_fmt::AsStrFormatExt;
assert_eq!("{}a{}b{}c".format(&[1, 2, 3]), "1a2b3c");
assert_eq!("{}a{}b{}c".format(&[1, 2, 3, 4]), "1a2b3c"); // extra arguments are ignored
assert_eq!("{}a{}b{}c".format(&[1, 2]), "1a2bc"); // missing arguments are replaced by empty string
assert_eq!("{{}}{}".format(&[1, 2]), "{}1");
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.