ToString in std::string - Rust (original) (raw)

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

Expand description

A trait for converting a value to a String.

This trait is automatically implemented for any type which implements theDisplay trait. As such, ToString shouldn’t be implemented directly:Display should be implemented instead, and you get the ToStringimplementation for free.

source

Converts the given value to a String.

Examples
let i = 5;
let five = String::from("5");

assert_eq!(five, i.to_string());

Run

source§

Panics

In this implementation, the to_string method panics if the Display implementation returns an error. This indicates an incorrect Display implementation since fmt::Write for String never returns an error itself.