Discourage allocation

This commit is contained in:
Aleksey Kladov 2020-06-13 14:27:23 +02:00
parent c87c4a0a40
commit be0bb857c1

View file

@ -236,11 +236,6 @@ struct Foo {
}
```
## 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
Function preconditions should generally be expressed in types and provided by the caller (rather than checked by callee):
@ -261,6 +256,32 @@ fn frobnicate(walrus: Option<Walrus>) {
}
```
## 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
We don't have specific rules around git history hygiene.