Dragonight
Dragonight

Reputation: 1193

How to convert std::fmt::Arguments to a string?

I would like to convert a std::fmt::Arguments to a string type. However, since the fields of Arguments are private, I cannot directly convert it into a string.

Upvotes: 2

Views: 887

Answers (1)

Shepmaster
Shepmaster

Reputation: 430921

Use ToString:

fn example(a: std::fmt::Arguments) -> String {
    a.to_string()
}

Or use format!:

fn example(a: std::fmt::Arguments) -> String {
    format!("{}", a)
}

Any of the other ways of using the formatting machinery will also work.

You could have figured this out yourself by looking at the documentation for Arguments and making note of what methods and traits it implements:

impl<'a> Debug for Arguments<'a>
impl<'a> Clone for Arguments<'a>
impl<'a> Display for Arguments<'a>
impl<'a> Copy for Arguments<'a>

Copy and Clone aren't relevant here, but Debug and Display are.

Upvotes: 6

Related Questions