rust-lang / rust-lang/rustfmt

Converting Type Constraints

Open
#5,181 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature-request P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Apologies if this has been brought up before but I took a brief look and didn't see anything.

It would be useful if Rustfmt had an option for normalizing type constraints on functions to use where instead of inline. Perhaps it could go the other way too, converting where clauses into inline clauses if it is possible. (not everything that can be done with the where syntax can be done inline, but everything done inline can be done in where clauses)

The idea would be to convert between instances of:

pub fn some_fn<S: AsRef<str>>(arg: S) {}

and:

pub fn some_fn<S>(arg: S)
where
	S: AsRef<str>,
{
}

Here is a relevant SO question with an answer that sums up the differences nicely: https://stackoverflow.com/questions/28405621/what-is-the-syntax-and-semantics-of-the-where-keyword

As the SO discussion mentions, the inline syntax is commonly used just because it's more convenient, but the where syntax is more readable. This seems like a perfect use case for Rustfmt to make code more readable, without sacrificing convenience.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing Rustfmt's existing configuration options and handling of function type constraints. Define how normalization should work in both directions, including cases where inline syntax cannot represent a where clause. Done means the behavior is specified, configurable, and covered for the examples and edge cases described in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.