From 629c68e80d3d9473226c8f3713e95a2011999f2c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 11 Aug 2021 14:16:15 +0300 Subject: [PATCH] internal: document that ascription is preferred to a turbo fish --- crates/ide_assists/src/handlers/sort_items.rs | 2 +- crates/vfs/src/loader.rs | 2 +- docs/dev/style.md | 20 +++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index 2ca4a19ec4..775bd367de 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -131,7 +131,7 @@ impl AddRewrite for Assists { target: TextRange, ) -> Option<()> { self.add(AssistId("sort_items", AssistKind::RefactorRewrite), label, target, |builder| { - let mutable: Vec<_> = old.into_iter().map(|it| builder.make_mut(it)).collect(); + let mutable: Vec = old.into_iter().map(|it| builder.make_mut(it)).collect(); mutable .into_iter() .zip(new) diff --git a/crates/vfs/src/loader.rs b/crates/vfs/src/loader.rs index c341b974a0..e2d74782ae 100644 --- a/crates/vfs/src/loader.rs +++ b/crates/vfs/src/loader.rs @@ -161,7 +161,7 @@ impl Directories { /// - This path is longer than any element in `self.exclude` that is a prefix /// of `path`. In case of equality, exclusion wins. fn includes_path(&self, path: &AbsPath) -> bool { - let mut include = None::<&AbsPathBuf>; + let mut include: Option<&AbsPathBuf> = None; for incl in &self.include { if path.starts_with(incl) { include = Some(match include { diff --git a/docs/dev/style.md b/docs/dev/style.md index 257049915d..ac9ad8fc28 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -950,6 +950,26 @@ At the same time, it is more crowded -- it takes more time to visually scan it. **Rationale:** consistency, playing to language's strengths. 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`). +## Turbofish + +Prefer type ascription over the turbofish. +When ascribing types, avoid `_` + +```rust +// GOOD +let mutable: Vec = old.into_iter().map(|it| builder.make_mut(it)).collect(); + +// BAD +let mutable: Vec<_> = old.into_iter().map(|it| builder.make_mut(it)).collect(); + +// BAD +let mutable = old.into_iter().map(|it| builder.make_mut(it)).collect::>(); +``` + +**Rationale:** consistency, readability. +If compiler struggles to infer the type, the human would as well. +Having the result type specified up-front helps with understanding what the chain of iterator methods is doing. + ## Helper Functions Avoid creating singe-use helper functions: