RS.CLIPPY.INHERENT_TO_STRING_SHADOW_DISPLAY

Type implements inherent method `to_string()`, which gets shadowed by the implementation of 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_shadow_display. 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 and if the type implementing this method also implements the Display trait.

Why is this bad?

This method is also implicitly defined if a type implements the Display trait. The less versatile inherent method will then shadow the implementation introduced by Display.

Example

use std::fmt;

pub struct A;

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

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

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")
    }
}