Skip to content

Commit d64b583

Browse files
bors[bot]matklad
andauthored
Merge #4869
4869: Discourage allocation r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents d00ca86 + be0bb85 commit d64b583

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

docs/dev/README.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ Do
219219
```rust
220220
// Good
221221
struct Foo {
222-
bars: Vec<Bar>
222+
bars: Vec<Bar>
223223
}
224224

225225
struct Bar;
@@ -232,35 +232,56 @@ rather than
232232
struct Bar;
233233

234234
struct Foo {
235-
bars: Vec<Bar>
235+
bars: Vec<Bar>
236236
}
237237
```
238238

239-
## Documentation
240-
241-
For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines.
242-
If the line is too long, you want to split the sentence in two :-)
243-
244239
## Preconditions
245240

246241
Function preconditions should generally be expressed in types and provided by the caller (rather than checked by callee):
247242

248243
```rust
249244
// Good
250245
fn frbonicate(walrus: Walrus) {
251-
...
246+
...
252247
}
253248

254249
// Not as good
255250
fn frobnicate(walrus: Option<Walrus>) {
256-
let walrus = match walrus {
251+
let walrus = match walrus {
252+
Some(it) => it,
253+
None => return,
254+
};
255+
...
256+
}
257+
```
258+
259+
## Premature Pessimization
260+
261+
While we don't specifically optimize code yet, avoid writing the code which is slower than it needs to be.
262+
Don't allocate a `Vec` were an iterator would do, don't allocate strings needlessly.
263+
264+
```rust
265+
// Good
266+
use itertools::Itertools;
267+
268+
let (first_word, second_word) = match text.split_ascii_whitespace().collect_tuple() {
257269
Some(it) => it,
258270
None => return,
259-
};
260-
...
271+
}
272+
273+
// Not as good
274+
let words = text.split_ascii_whitespace().collect::<Vec<_>>();
275+
if words.len() != 2 {
276+
return
261277
}
262278
```
263279

280+
## Documentation
281+
282+
For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines.
283+
If the line is too long, you want to split the sentence in two :-)
284+
264285
## Commit Style
265286

266287
We don't have specific rules around git history hygiene.

0 commit comments

Comments
 (0)