RS.CLIPPY.INHERENT_TO_STRING

Type implements inherent method `to_string()`, but should instead implement the `Display` trait

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: inherent_to_string. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for the definition of inherent methods with a signature of to_string(&self) -> String.

Why is this bad?

This method is also implicitly defined if a type implements the Display trait. As the functionality of Display is much more versatile, it should be preferred.

Example

pub struct A;

impl A {
    pub fn to_string(&self) -> String {
        "I am A".to_string()
    }
}

Use instead:

use std::fmt;

pub struct A;

impl fmt::Display for A {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "I am A")
    }
}