Trait | URL |
---|---|
ToString |
std::string::ToString |
pub trait ToString {
fn to_string(&self) -> String;
}
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
.
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
}
}