Skip to content

Commit fb2d332

Browse files
bors[bot]matklad
andauthored
Merge #6250
6250: Expand code order section r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 0d45802 + 0c67edc commit fb2d332

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

docs/dev/style.md

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -366,27 +366,66 @@ People read things from top to bottom, so place most important things first.
366366

367367
Specifically, if all items except one are private, always put the non-private item on top.
368368

369-
Put `struct`s and `enum`s first, functions and impls last.
370-
371-
Do
372-
373369
```rust
374370
// Good
375-
struct Foo {
376-
bars: Vec<Bar>
371+
pub(crate) fn frobnicate() {
372+
Helper::act()
373+
}
374+
375+
#[derive(Default)]
376+
struct Helper { stuff: i32 }
377+
378+
impl Helper {
379+
fn act(&self) {
380+
381+
}
382+
}
383+
384+
// Not as good
385+
#[derive(Default)]
386+
struct Helper { stuff: i32 }
387+
388+
pub(crate) fn frobnicate() {
389+
Helper::act()
377390
}
378391

379-
struct Bar;
392+
impl Helper {
393+
fn act(&self) {
394+
395+
}
396+
}
380397
```
381398

382-
rather than
399+
If there's a mixture of private and public items, put public items first.
400+
If function bodies are folded in the editor, the source code should read as documentation for the public API.
401+
402+
Put `struct`s and `enum`s first, functions and impls last. Order types declarations in top-down manner.
383403

384404
```rust
405+
// Good
406+
struct Parent {
407+
children: Vec<Child>
408+
}
409+
410+
struct Child;
411+
412+
impl Parent {
413+
}
414+
415+
impl Child {
416+
}
417+
385418
// Not as good
386-
struct Bar;
419+
struct Child;
387420

388-
struct Foo {
389-
bars: Vec<Bar>
421+
impl Child {
422+
}
423+
424+
struct Parent {
425+
children: Vec<Child>
426+
}
427+
428+
impl Parent {
390429
}
391430
```
392431

0 commit comments

Comments
 (0)