]> git.lizzy.rs Git - rust.git/commitdiff
Expand code order section
authorAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 16 Oct 2020 10:50:09 +0000 (12:50 +0200)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 16 Oct 2020 10:50:09 +0000 (12:50 +0200)
docs/dev/style.md

index 883a6845d91189bd5d3ece691b22e19db4b39917..7a64a0d228c7105e49211fb455b3852681ca9228 100644 (file)
@@ -366,27 +366,66 @@ People read things from top to bottom, so place most important things first.
 
 Specifically, if all items except one are private, always put the non-private item on top.
 
-Put `struct`s and `enum`s first, functions and impls last.
-
-Do
-
 ```rust
 // Good
-struct Foo {
-    bars: Vec<Bar>
+pub(crate) fn frobnicate() {
+    Helper::act()
+}
+
+#[derive(Default)]
+struct Helper { stuff: i32 }
+
+impl Helper {
+    fn act(&self) {
+
+    }
+}
+
+// Not as good
+#[derive(Default)]
+struct Helper { stuff: i32 }
+
+pub(crate) fn frobnicate() {
+    Helper::act()
 }
 
-struct Bar;
+impl Helper {
+    fn act(&self) {
+
+    }
+}
 ```
 
-rather than
+If there's a mixture of private and public items, put public items first.
+If function bodies are folded in the editor, the source code should read as documentation for the public API.
+
+Put `struct`s and `enum`s first, functions and impls last. Order types declarations in top-down manner.
 
 ```rust
+// Good
+struct Parent {
+    children: Vec<Child>
+}
+
+struct Child;
+
+impl Parent {
+}
+
+impl Child {
+}
+
 // Not as good
-struct Bar;
+struct Child;
 
-struct Foo {
-    bars: Vec<Bar>
+impl Child {
+}
+
+struct Parent {
+    children: Vec<Child>
+}
+
+impl Parent {
 }
 ```