about summary refs log tree commit diff
path: root/docs/dev
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2020-06-13 14:27:23 +0200
committerAleksey Kladov <aleksey.kladov@gmail.com>2020-06-13 14:27:23 +0200
commitbe0bb857c143f4712dffc40094e1e35debbf5e8b (patch)
tree93fb340fe4c9d8a417ae6bc011b21c950e204b77 /docs/dev
parentc87c4a0a40c79e90272d4df62c7e2dc12b6b2c3e (diff)
downloadrust-be0bb857c143f4712dffc40094e1e35debbf5e8b.tar.gz
rust-be0bb857c143f4712dffc40094e1e35debbf5e8b.zip
Discourage allocation
Diffstat (limited to 'docs/dev')
-rw-r--r--docs/dev/README.md43
1 files changed, 32 insertions, 11 deletions
diff --git a/docs/dev/README.md b/docs/dev/README.md
index ef5ffbf597a..1ce8666e3e6 100644
--- a/docs/dev/README.md
+++ b/docs/dev/README.md
@@ -219,7 +219,7 @@ Do
 ```rust
 // Good
 struct Foo {
-  bars: Vec<Bar>
+    bars: Vec<Bar>
 }
 
 struct Bar;
@@ -232,15 +232,10 @@ rather than
 struct Bar;
 
 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
 
 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
 // Good
 fn frbonicate(walrus: Walrus) {
-  ...
+    ...
 }
 
 // Not as good
 fn frobnicate(walrus: Option<Walrus>) {
-  let walrus = match walrus {
+    let walrus = match walrus {
+        Some(it) => it,
+        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
 
 We don't have specific rules around git history hygiene.