RS.CLIPPY.PRINT_IN_FORMAT_IMPL

Use of a print macro in a formatting trait impl

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

What it does

Checks for usage of println, print, eprintln or eprint in an implementation of a formatting trait.

Why is this bad?

Using a print macro is likely unintentional since formatting traits should write to the Formatter, not stdout/stderr.

Example

use std::fmt::{Display, Error, Formatter};

struct S;
impl Display for S {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        println!("S");

        Ok(())
    }
}

Use instead:

use std::fmt::{Display, Error, Formatter};

struct S;
impl Display for S {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        writeln!(f, "S");

        Ok(())
    }
}