internal: document that ascription is preferred to a turbo fish

This commit is contained in:
Aleksey Kladov 2021-08-11 14:16:15 +03:00
parent 145b51f9da
commit 629c68e80d
3 changed files with 22 additions and 2 deletions

View file

@ -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<T> = old.into_iter().map(|it| builder.make_mut(it)).collect();
mutable
.into_iter()
.zip(new)

View file

@ -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 {

View file

@ -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<T> = 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::<Vec<_>>();
```
**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: