]> git.lizzy.rs Git - rust.git/commitdiff
Preconditions style
authorAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 8 Jun 2020 10:54:48 +0000 (12:54 +0200)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 8 Jun 2020 10:54:48 +0000 (12:54 +0200)
docs/dev/README.md

index 64d595b684a2d093c0736001ab818b7f1d02ffca..5a9c0a148a124db1ca53dd344fd39eaef19709b7 100644 (file)
@@ -241,6 +241,26 @@ struct Foo {
 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):
+
+```rust
+// Good
+fn frbonicate(walrus: Walrus) {
+  ...
+}
+
+// Not as good
+fn frobnicate(walrus: Option<Walrus>) {
+  let walrus = match walrus {
+    Some(it) => it,
+    None => return,
+  };
+  ...
+}
+```
+
 # Architecture Invariants
 
 This section tries to document high-level design constraints, which are not