Merge #4869
4869: Discourage allocation r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
d64b583e5a
1 changed files with 34 additions and 13 deletions
|
@ -219,7 +219,7 @@ Do
|
||||||
```rust
|
```rust
|
||||||
// Good
|
// Good
|
||||||
struct Foo {
|
struct Foo {
|
||||||
bars: Vec<Bar>
|
bars: Vec<Bar>
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Bar;
|
struct Bar;
|
||||||
|
@ -232,15 +232,10 @@ rather than
|
||||||
struct Bar;
|
struct Bar;
|
||||||
|
|
||||||
struct Foo {
|
struct Foo {
|
||||||
bars: Vec<Bar>
|
bars: Vec<Bar>
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Documentation
|
|
||||||
|
|
||||||
For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines.
|
|
||||||
If the line is too long, you want to split the sentence in two :-)
|
|
||||||
|
|
||||||
## Preconditions
|
## Preconditions
|
||||||
|
|
||||||
Function preconditions should generally be expressed in types and provided by the caller (rather than checked by callee):
|
Function preconditions should generally be expressed in types and provided by the caller (rather than checked by callee):
|
||||||
|
@ -248,19 +243,45 @@ Function preconditions should generally be expressed in types and provided by th
|
||||||
```rust
|
```rust
|
||||||
// Good
|
// Good
|
||||||
fn frbonicate(walrus: Walrus) {
|
fn frbonicate(walrus: Walrus) {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not as good
|
// Not as good
|
||||||
fn frobnicate(walrus: Option<Walrus>) {
|
fn frobnicate(walrus: Option<Walrus>) {
|
||||||
let walrus = match walrus {
|
let walrus = match walrus {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Premature Pessimization
|
||||||
|
|
||||||
|
While we don't specifically optimize code yet, avoid writing the code which is slower than it needs to be.
|
||||||
|
Don't allocate a `Vec` were an iterator would do, don't allocate strings needlessly.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// Good
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
let (first_word, second_word) = match text.split_ascii_whitespace().collect_tuple() {
|
||||||
|
Some(it) => it,
|
||||||
|
None => return,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not as good
|
||||||
|
let words = text.split_ascii_whitespace().collect::<Vec<_>>();
|
||||||
|
if words.len() != 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines.
|
||||||
|
If the line is too long, you want to split the sentence in two :-)
|
||||||
|
|
||||||
## Commit Style
|
## Commit Style
|
||||||
|
|
||||||
We don't have specific rules around git history hygiene.
|
We don't have specific rules around git history hygiene.
|
||||||
|
|
Loading…
Reference in a new issue