Skip to content

Commit 88311c4

Browse files
bors[bot]matklad
andauthored
Merge #9854
9854: internal: document that ascription is preferred to a turbo fish r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 145b51f + 629c68e commit 88311c4

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

crates/ide_assists/src/handlers/sort_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl AddRewrite for Assists {
131131
target: TextRange,
132132
) -> Option<()> {
133133
self.add(AssistId("sort_items", AssistKind::RefactorRewrite), label, target, |builder| {
134-
let mutable: Vec<_> = old.into_iter().map(|it| builder.make_mut(it)).collect();
134+
let mutable: Vec<T> = old.into_iter().map(|it| builder.make_mut(it)).collect();
135135
mutable
136136
.into_iter()
137137
.zip(new)

crates/vfs/src/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl Directories {
161161
/// - This path is longer than any element in `self.exclude` that is a prefix
162162
/// of `path`. In case of equality, exclusion wins.
163163
fn includes_path(&self, path: &AbsPath) -> bool {
164-
let mut include = None::<&AbsPathBuf>;
164+
let mut include: Option<&AbsPathBuf> = None;
165165
for incl in &self.include {
166166
if path.starts_with(incl) {
167167
include = Some(match include {

docs/dev/style.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,26 @@ At the same time, it is more crowded -- it takes more time to visually scan it.
950950
**Rationale:** consistency, playing to language's strengths.
951951
Rust has first-class support for imperative control flow constructs like `for` and `if`, while functions are less first-class due to lack of universal function type, currying, and non-first-class effects (`?`, `.await`).
952952

953+
## Turbofish
954+
955+
Prefer type ascription over the turbofish.
956+
When ascribing types, avoid `_`
957+
958+
```rust
959+
// GOOD
960+
let mutable: Vec<T> = old.into_iter().map(|it| builder.make_mut(it)).collect();
961+
962+
// BAD
963+
let mutable: Vec<_> = old.into_iter().map(|it| builder.make_mut(it)).collect();
964+
965+
// BAD
966+
let mutable = old.into_iter().map(|it| builder.make_mut(it)).collect::<Vec<_>>();
967+
```
968+
969+
**Rationale:** consistency, readability.
970+
If compiler struggles to infer the type, the human would as well.
971+
Having the result type specified up-front helps with understanding what the chain of iterator methods is doing.
972+
953973
## Helper Functions
954974

955975
Avoid creating singe-use helper functions:

0 commit comments

Comments
 (0)