File tree Expand file tree Collapse file tree 1 file changed +32
-11
lines changed Expand file tree Collapse file tree 1 file changed +32
-11
lines changed Original file line number Diff line number Diff line change 219
219
``` rust
220
220
// Good
221
221
struct Foo {
222
- bars : Vec <Bar >
222
+ bars : Vec <Bar >
223
223
}
224
224
225
225
struct Bar ;
@@ -232,35 +232,56 @@ rather than
232
232
struct Bar ;
233
233
234
234
struct Foo {
235
- bars : Vec <Bar >
235
+ bars : Vec <Bar >
236
236
}
237
237
```
238
238
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
-
244
239
## Preconditions
245
240
246
241
Function preconditions should generally be expressed in types and provided by the caller (rather than checked by callee):
247
242
248
243
``` rust
249
244
// Good
250
245
fn frbonicate (walrus : Walrus ) {
251
- ...
246
+ ...
252
247
}
253
248
254
249
// Not as good
255
250
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 () {
257
269
Some (it ) => it ,
258
270
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
261
277
}
262
278
```
263
279
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
+
264
285
## Commit Style
265
286
266
287
We don't have specific rules around git history hygiene.
You can’t perform that action at this time.
0 commit comments