Skip to content

impl Display instead of ToString #12076

Closed
@andrewbanchich

Description

@andrewbanchich

What it does

The docs for ToString say:

ToString shouldn’t be implemented directly: Display should be implemented instead

Currently there are no warnings to a direct ToString impl.

Advantage

No response

Drawbacks

No response

Example

impl ToString for Point {
    fn to_string(&self) -> String {
        format!("({}, {})", self.x, self.y)
    }
}

Could be written as:

impl fmt::Display for Point {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({}, {})", self.x, self.y)
    }
}

Metadata

Metadata

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions