]> git.lizzy.rs Git - rust.git/blob - docs/user/assists.md
document a couple of assists
[rust.git] / docs / user / assists.md
1 # Assists
2
3 ## `add_derive`
4
5 Adds a new `#[derive()]` clause to a struct or enum.
6
7 ```rust
8 // BEFORE
9 struct Point {
10     x: u32,
11     y: u32,<|>
12 }
13
14 // AFTER
15 #[derive()]
16 struct Point {
17     x: u32,
18     y: u32,
19 }
20 ```
21
22 ## `add_explicit_type`
23
24 Specify type for a let binding
25
26 ```rust
27 // BEFORE
28 fn main() {
29     let x<|> = 92;
30 }
31
32 // AFTER
33 fn main() {
34     let x: i32 = 92;
35 }
36 ```
37
38 ## `add_impl`
39
40 Adds a new inherent impl for a type
41
42 ```rust
43 // BEFORE
44 struct Ctx<T: Clone> {
45      data: T,<|>
46 }
47
48 // AFTER
49 struct Ctx<T: Clone> {
50      data: T,
51 }
52
53 impl<T: Clone> Ctx<T> {
54
55 }
56 ```
57
58 ## `add_impl_default_members`
59
60 Adds scaffold for overriding default impl members
61
62 ```rust
63 // BEFORE
64 trait T {
65     Type X;
66     fn foo(&self);
67     fn bar(&self) {}
68 }
69
70 impl T for () {
71     Type X = ();
72     fn foo(&self) {}<|>
73
74 }
75
76 // AFTER
77 trait T {
78     Type X;
79     fn foo(&self);
80     fn bar(&self) {}
81 }
82
83 impl T for () {
84     Type X = ();
85     fn foo(&self) {}
86     fn bar(&self) {}
87
88 }
89 ```
90
91 ## `add_impl_missing_members`
92
93 Adds scaffold for required impl members
94
95 ```rust
96 // BEFORE
97 trait T {
98     Type X;
99     fn foo(&self);
100     fn bar(&self) {}
101 }
102
103 impl T for () {<|>
104
105 }
106
107 // AFTER
108 trait T {
109     Type X;
110     fn foo(&self);
111     fn bar(&self) {}
112 }
113
114 impl T for () {
115     fn foo(&self) { unimplemented!() }
116
117 }
118 ```
119
120 ## `apply_demorgan`
121
122 Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws).
123 This transforms expressions of the form `!l || !r` into `!(l && r)`.
124 This also works with `&&`. This assist can only be applied with the cursor
125 on either `||` or `&&`, with both operands being a negation of some kind.
126 This means something of the form `!x` or `x != y`.
127
128 ```rust
129 // BEFORE
130 fn main() {
131     if x != 4 ||<|> !y {}
132 }
133
134 // AFTER
135 fn main() {
136     if !(x == 4 && y) {}
137 }
138 ```
139
140 ## `change_visibility`
141
142 Adds or changes existing visibility specifier.
143
144 ```rust
145 // BEFORE
146 fn<|> frobnicate() {}
147
148 // AFTER
149 pub(crate) fn frobnicate() {}
150 ```
151
152 ## `convert_to_guarded_return`
153
154 Replace a large conditional with a guarded return.
155
156 ```rust
157 // BEFORE
158 fn main() {
159     <|>if cond {
160         foo();
161         bar();
162     }
163 }
164
165 // AFTER
166 fn main() {
167     if !cond {
168         return;
169     }
170     foo();
171     bar();
172 }
173 ```
174
175 ## `fill_match_arms`
176
177 Adds missing clauses to a `match` expression.
178
179 ```rust
180 // BEFORE
181 enum Action { Move { distance: u32 }, Stop }
182
183 fn handle(action: Action) {
184     match action {
185         <|>
186     }
187 }
188
189 // AFTER
190 enum Action { Move { distance: u32 }, Stop }
191
192 fn handle(action: Action) {
193     match action {
194         Action::Move{ distance } => (),
195         Action::Stop => (),
196     }
197 }
198 ```