Rust

Rust

Made by DeepSource

Found print! in implementation of Debug RS-W1133

Anti-pattern
Minor

The fmt method provided by the Debug trait uses a formatter to generate the output string. It does not directly write to stdout or stderr. Using print! or eprint! in this implementation leads to incorrect functionality. Instead, use the standard suite of debug representations provided by the Formatter trait (debug_struct, debug_tuple, debug_list, debug_set, debug_map). If you wish to use a custom representation for debugging, you can use the write! macro.

Bad practice

use std::fmt;

struct Foo { bar: usize }

impl fmt::Debug for Foo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        print!("This is bar in Foo: {}", self.bar)
    }
}

Recommended

use std::fmt;

struct Foo { bar: usize }

impl fmt::Debug for Foo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Foo")
        .field("bar", &self.bar)
        .finish()
    }
}