Skip to content

Latest commit

 

History

History
48 lines (40 loc) · 1.29 KB

ToString.md

File metadata and controls

48 lines (40 loc) · 1.29 KB

Table of contents


URLs

Trait URL
ToString std::string::ToString

Declaration

pub trait ToString {
    fn to_string(&self) -> String;
}

In a nutshell

Converts the given value to a String. ToString trait provides method .to_string() to convert value to a String.
ToString trait is automatically implemented for any type that implements Display.


Blanket implementations

impl<T> ToString for T

The standard library implements the ToString trait on any type that implements the Display trait:

impl<T: fmt::Display + ?Sized> ToString for T {
    default fn to_string(&self) -> String {
        let mut buf = String::new();
        let mut formatter = core::fmt::Formatter::new(&mut buf);
        // Bypass format_args!() to avoid write_str with zero-length strs
        fmt::Display::fmt(self, &mut formatter)
            .expect("a Display implementation returned an error unexpectedly");
        buf
    }
}