Skip to content

Suggest to use turbofish syntax instead of type hints #7081

Open
@thomaseizinger

Description

@thomaseizinger

What it does

Suggest to use turbofish syntax instead of type hints.

Categories (optional)

  • Kind: clippy::style

What is the advantage of the recommended code over the original code

Type hints can get stale if the expression is modified / refactored and ends up returning a different type than what was originally returned.

Using turbofish syntax instead allows the compiler to continue with its type-inference and hence not require a type hint. Contrary to the type hint on the variable, the turbofish syntax is applied "locally" to where it is needed and therefore is more likely to stay relevant as the code around it changes.

Overall, I think this makes it easier to maintain Rust code.

Drawbacks

Some people may prefer to write type hints instead of turbo-fish syntax.

Examples

let numbers: Vec<_> = (0..10).collect();
let lucky_number: u64 = "42".parse().unwrap();

Could be written as:

let numbers = (0..10).collect::<Vec<_>>();
let lucky_number = "42".parse::<u64>().unwrap();

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-restrictionLint: Belongs in the restriction lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions