Struct Arguments
struct Arguments<'a> { ... }
This structure represents a safely precompiled version of a format string and its arguments. This cannot be generated at runtime because it cannot safely be done, so no constructors are given and the fields are private to prevent modification.
The [format_args!] macro will safely create an instance of this structure.
The macro validates the format string at compile-time so usage of the
[write()] and format() functions can be safely performed.
You can use the Arguments<'a> that [format_args!] returns in Debug
and Display contexts as seen below. The example also shows that Debug
and Display format to the same thing: the interpolated format string
in format_args!.
let debug = format!;
let display = format!;
assert_eq!;
assert_eq!;
Implementations
impl<'a> Arguments<'a>
const fn from_str(s: &'static str) -> Arguments<'a>Create a
fmt::Argumentsobject for a single static string.Formatting this
fmt::Argumentswill just produce the string as-is.const fn as_str(self: &Self) -> Option<&'static str>Gets the formatted string, if it has no arguments to be formatted at runtime.
This can be used to avoid allocations in some cases.
Guarantees
For
format_args!("just a literal"), this function is guaranteed to returnSome("just a literal").For most cases with placeholders, this function will return
None.However, the compiler may perform optimizations that can cause this function to return
Some(_)even if the format string contains placeholders. For example,format_args!("Hello, {}!", "world")may be optimized toformat_args!("Hello, world!"), such thatas_str()returnsSome("Hello, world!").The behavior for anything but the trivial case (without placeholders) is not guaranteed, and should not be relied upon for anything other than optimization.
Examples
use Arguments;assert_eq!; assert_eq!; assert_eq!;
impl Debug for Arguments<'_>
fn fmt(self: &Self, fmt: &mut Formatter<'_>) -> Result
impl Display for Arguments<'_>
fn fmt(self: &Self, fmt: &mut Formatter<'_>) -> Result
impl Send for Arguments<'_>
impl Sync for Arguments<'_>
impl<'a> Clone for Arguments<'a>
fn clone(self: &Self) -> Arguments<'a>
impl<'a> Copy for Arguments<'a>
impl<'a> Freeze for Arguments<'a>
impl<'a> RefUnwindSafe for Arguments<'a>
impl<'a> Unpin for Arguments<'a>
impl<'a> UnwindSafe for Arguments<'a>
impl<T> Any for Arguments<'a>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Arguments<'a>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Arguments<'a>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Arguments<'a>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Arguments<'a>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into for Arguments<'a>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Arguments<'a>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Arguments<'a>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>