mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 01:17:27 +00:00
Merge #6250
6250: Expand code order section r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
fb2d332f5f
1 changed files with 50 additions and 11 deletions
|
@ -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.
|
||||
```rust
|
||||
// Good
|
||||
pub(crate) fn frobnicate() {
|
||||
Helper::act()
|
||||
}
|
||||
|
||||
Do
|
||||
#[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()
|
||||
}
|
||||
|
||||
impl Helper {
|
||||
fn act(&self) {
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
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 Foo {
|
||||
bars: Vec<Bar>
|
||||
struct Parent {
|
||||
children: Vec<Child>
|
||||
}
|
||||
|
||||
struct Bar;
|
||||
```
|
||||
struct Child;
|
||||
|
||||
rather than
|
||||
impl Parent {
|
||||
}
|
||||
|
||||
impl Child {
|
||||
}
|
||||
|
||||
```rust
|
||||
// Not as good
|
||||
struct Bar;
|
||||
struct Child;
|
||||
|
||||
struct Foo {
|
||||
bars: Vec<Bar>
|
||||
impl Child {
|
||||
}
|
||||
|
||||
struct Parent {
|
||||
children: Vec<Child>
|
||||
}
|
||||
|
||||
impl Parent {
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in a new issue