Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Core Traits

The ToString trait

Reference for Rust ToString: the blanket implementation for every Display type, to_string() cost, specialization for str and numbers, and when not to implement it.

ToString is the trait behind the .to_string() method. It has a single method that turns a value into an owned String. You almost never implement it yourself: the standard library provides a blanket implementation for every type that implements Display, so implementing Display gives you to_string() for free.

Because of that blanket implementation, to_string() works on integers, floats, char, bool, &str, String, and any of your own types that implement Display.

Rust
let s: String = 42.to_string();

Try it

Rust to_string on built-in types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Display gives you ToString

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

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

impl<T: fmt::Display + ?Sized> ToString for T { /* provided by std */ }
Name Description
&self The value to format. It is only borrowed.

Returns: a new String containing exactly what format!("{}", value) would produce.

Errors / panics / edge cases: to_string() panics if the Display implementation returns an error, because a String cannot fail to accept text. A correct Display implementation only returns errors that come from the writer, so this does not happen in practice.

Usage notes

  • Implement Display, not ToString. A manual impl ToString for MyType conflicts with the blanket implementation and does not compile. The one exception is a type that does not implement Display, which is rare and usually a design smell.
  • It allocates. Every call creates a new String. To print or to write into an existing buffer, use println!, write! or the fmt::Write trait instead.
  • Fast paths exist. The standard library specializes to_string() for str, String, char, bool and the integer types, so these skip the general formatting machinery. Behavior is identical.
  • Not the same as Debug. to_string() uses Display. A type that only implements Debug has no to_string(); use format!("{:?}", v).
  • Accepts unsized types. The ?Sized bound means str itself is covered, so "text".to_string() works through auto-referencing.
  • to_string() versus to_owned() and String::from. For a &str all three give the same String. Pick one style per codebase; .to_string() is the most common in examples.
  • Formatting options are lost. Width, precision and fill are formatter flags, so to_string() always uses the default ones. Use format!("{:>8.2}", x) when you need them.
  • Generic code. Write T: ToString when you only need text out, and T: Display when you also want to print it directly. Both accept the same types.