mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 20:43:21 +00:00
internal: document that ascription is preferred to a turbo fish
This commit is contained in:
parent
145b51f9da
commit
629c68e80d
3 changed files with 22 additions and 2 deletions
|
@ -131,7 +131,7 @@ impl AddRewrite for Assists {
|
||||||
target: TextRange,
|
target: TextRange,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
self.add(AssistId("sort_items", AssistKind::RefactorRewrite), label, target, |builder| {
|
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
|
mutable
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.zip(new)
|
.zip(new)
|
||||||
|
|
|
@ -161,7 +161,7 @@ impl Directories {
|
||||||
/// - This path is longer than any element in `self.exclude` that is a prefix
|
/// - This path is longer than any element in `self.exclude` that is a prefix
|
||||||
/// of `path`. In case of equality, exclusion wins.
|
/// of `path`. In case of equality, exclusion wins.
|
||||||
fn includes_path(&self, path: &AbsPath) -> bool {
|
fn includes_path(&self, path: &AbsPath) -> bool {
|
||||||
let mut include = None::<&AbsPathBuf>;
|
let mut include: Option<&AbsPathBuf> = None;
|
||||||
for incl in &self.include {
|
for incl in &self.include {
|
||||||
if path.starts_with(incl) {
|
if path.starts_with(incl) {
|
||||||
include = Some(match include {
|
include = Some(match include {
|
||||||
|
|
|
@ -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.
|
**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`).
|
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
|
## Helper Functions
|
||||||
|
|
||||||
Avoid creating singe-use helper functions:
|
Avoid creating singe-use helper functions:
|
||||||
|
|
Loading…
Reference in a new issue