]> git.lizzy.rs Git - rust.git/blob - docs/user/assists.md
use unicode bar for drawing the cursor
[rust.git] / docs / user / assists.md
1 # Assists
2
3 Cursor position or selection is signified by `┃` character.
4
5
6 ## `add_derive`
7
8 Adds a new `#[derive()]` clause to a struct or enum.
9
10 ```rust
11 // BEFORE
12 struct Point {
13     x: u32,
14     y: u32,┃
15 }
16
17 // AFTER
18 #[derive()]
19 struct Point {
20     x: u32,
21     y: u32,
22 }
23 ```
24
25 ## `add_explicit_type`
26
27 Specify type for a let binding.
28
29 ```rust
30 // BEFORE
31 fn main() {
32     let x┃ = 92;
33 }
34
35 // AFTER
36 fn main() {
37     let x: i32 = 92;
38 }
39 ```
40
41 ## `add_impl`
42
43 Adds a new inherent impl for a type.
44
45 ```rust
46 // BEFORE
47 struct Ctx<T: Clone> {
48      data: T,┃
49 }
50
51 // AFTER
52 struct Ctx<T: Clone> {
53      data: T,
54 }
55
56 impl<T: Clone> Ctx<T> {
57
58 }
59 ```
60
61 ## `add_impl_default_members`
62
63 Adds scaffold for overriding default impl members.
64
65 ```rust
66 // BEFORE
67 trait T {
68     Type X;
69     fn foo(&self);
70     fn bar(&self) {}
71 }
72
73 impl T for () {
74     Type X = ();
75     fn foo(&self) {}┃
76
77 }
78
79 // AFTER
80 trait T {
81     Type X;
82     fn foo(&self);
83     fn bar(&self) {}
84 }
85
86 impl T for () {
87     Type X = ();
88     fn foo(&self) {}
89     fn bar(&self) {}
90
91 }
92 ```
93
94 ## `add_impl_missing_members`
95
96 Adds scaffold for required impl members.
97
98 ```rust
99 // BEFORE
100 trait T {
101     Type X;
102     fn foo(&self);
103     fn bar(&self) {}
104 }
105
106 impl T for () {┃
107
108 }
109
110 // AFTER
111 trait T {
112     Type X;
113     fn foo(&self);
114     fn bar(&self) {}
115 }
116
117 impl T for () {
118     fn foo(&self) { unimplemented!() }
119
120 }
121 ```
122
123 ## `apply_demorgan`
124
125 Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws).
126 This transforms expressions of the form `!l || !r` into `!(l && r)`.
127 This also works with `&&`. This assist can only be applied with the cursor
128 on either `||` or `&&`, with both operands being a negation of some kind.
129 This means something of the form `!x` or `x != y`.
130
131 ```rust
132 // BEFORE
133 fn main() {
134     if x != 4 ||┃ !y {}
135 }
136
137 // AFTER
138 fn main() {
139     if !(x == 4 && y) {}
140 }
141 ```
142
143 ## `change_visibility`
144
145 Adds or changes existing visibility specifier.
146
147 ```rust
148 // BEFORE
149 ┃fn frobnicate() {}
150
151 // AFTER
152 pub(crate) fn frobnicate() {}
153 ```
154
155 ## `convert_to_guarded_return`
156
157 Replace a large conditional with a guarded return.
158
159 ```rust
160 // BEFORE
161 fn main() {
162     ┃if cond {
163         foo();
164         bar();
165     }
166 }
167
168 // AFTER
169 fn main() {
170     if !cond {
171         return;
172     }
173     foo();
174     bar();
175 }
176 ```
177
178 ## `fill_match_arms`
179
180 Adds missing clauses to a `match` expression.
181
182 ```rust
183 // BEFORE
184 enum Action { Move { distance: u32 }, Stop }
185
186 fn handle(action: Action) {
187     match action {
188         ┃
189     }
190 }
191
192 // AFTER
193 enum Action { Move { distance: u32 }, Stop }
194
195 fn handle(action: Action) {
196     match action {
197         Action::Move { distance } => (),
198         Action::Stop => (),
199     }
200 }
201 ```
202
203 ## `flip_binexpr`
204
205 Flips operands of a binary expression.
206
207 ```rust
208 // BEFORE
209 fn main() {
210     let _ = 90 +┃ 2;
211 }
212
213 // AFTER
214 fn main() {
215     let _ = 2 + 90;
216 }
217 ```
218
219 ## `flip_comma`
220
221 Flips two comma-separated items.
222
223 ```rust
224 // BEFORE
225 fn main() {
226     ((1, 2),┃ (3, 4));
227 }
228
229 // AFTER
230 fn main() {
231     ((3, 4), (1, 2));
232 }
233 ```
234
235 ## `inline_local_variable`
236
237 Inlines local variable.
238
239 ```rust
240 // BEFORE
241 fn main() {
242     let x┃ = 1 + 2;
243     x * 4;
244 }
245
246 // AFTER
247 fn main() {
248     (1 + 2) * 4;
249 }
250 ```
251
252 ## `introduce_variable`
253
254 Extracts subexpression into a variable.
255
256 ```rust
257 // BEFORE
258 fn main() {
259     ┃(1 + 2)┃ * 4;
260 }
261
262 // AFTER
263 fn main() {
264     let var_name = (1 + 2);
265     var_name * 4;
266 }
267 ```