diff options
Diffstat (limited to 'docs/user/assists.md')
-rw-r--r-- | docs/user/assists.md | 96 |
1 files changed, 84 insertions, 12 deletions
diff --git a/docs/user/assists.md b/docs/user/assists.md index 6c6943622..4ad7ea59d 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md | |||
@@ -17,7 +17,7 @@ struct S; | |||
17 | struct S; | 17 | struct S; |
18 | 18 | ||
19 | impl Debug for S { | 19 | impl Debug for S { |
20 | 20 | $0 | |
21 | } | 21 | } |
22 | ``` | 22 | ``` |
23 | 23 | ||
@@ -33,7 +33,7 @@ struct Point { | |||
33 | } | 33 | } |
34 | 34 | ||
35 | // AFTER | 35 | // AFTER |
36 | #[derive()] | 36 | #[derive($0)] |
37 | struct Point { | 37 | struct Point { |
38 | x: u32, | 38 | x: u32, |
39 | y: u32, | 39 | y: u32, |
@@ -77,7 +77,7 @@ fn foo() { | |||
77 | } | 77 | } |
78 | 78 | ||
79 | fn bar(arg: &str, baz: Baz) { | 79 | fn bar(arg: &str, baz: Baz) { |
80 | todo!() | 80 | ${0:todo!()} |
81 | } | 81 | } |
82 | 82 | ||
83 | ``` | 83 | ``` |
@@ -105,16 +105,16 @@ Adds a new inherent impl for a type. | |||
105 | ```rust | 105 | ```rust |
106 | // BEFORE | 106 | // BEFORE |
107 | struct Ctx<T: Clone> { | 107 | struct Ctx<T: Clone> { |
108 | data: T,┃ | 108 | data: T,┃ |
109 | } | 109 | } |
110 | 110 | ||
111 | // AFTER | 111 | // AFTER |
112 | struct Ctx<T: Clone> { | 112 | struct Ctx<T: Clone> { |
113 | data: T, | 113 | data: T, |
114 | } | 114 | } |
115 | 115 | ||
116 | impl<T: Clone> Ctx<T> { | 116 | impl<T: Clone> Ctx<T> { |
117 | 117 | $0 | |
118 | } | 118 | } |
119 | ``` | 119 | ``` |
120 | 120 | ||
@@ -146,7 +146,7 @@ trait Trait { | |||
146 | impl Trait for () { | 146 | impl Trait for () { |
147 | Type X = (); | 147 | Type X = (); |
148 | fn foo(&self) {} | 148 | fn foo(&self) {} |
149 | fn bar(&self) {} | 149 | $0fn bar(&self) {} |
150 | 150 | ||
151 | } | 151 | } |
152 | ``` | 152 | ``` |
@@ -175,7 +175,9 @@ trait Trait<T> { | |||
175 | } | 175 | } |
176 | 176 | ||
177 | impl Trait<u32> for () { | 177 | impl Trait<u32> for () { |
178 | fn foo(&self) -> u32 { todo!() } | 178 | fn foo(&self) -> u32 { |
179 | ${0:todo!()} | ||
180 | } | ||
179 | 181 | ||
180 | } | 182 | } |
181 | ``` | 183 | ``` |
@@ -196,11 +198,29 @@ struct Ctx<T: Clone> { | |||
196 | } | 198 | } |
197 | 199 | ||
198 | impl<T: Clone> Ctx<T> { | 200 | impl<T: Clone> Ctx<T> { |
199 | fn new(data: T) -> Self { Self { data } } | 201 | fn $0new(data: T) -> Self { Self { data } } |
200 | } | 202 | } |
201 | 203 | ||
202 | ``` | 204 | ``` |
203 | 205 | ||
206 | ## `add_turbo_fish` | ||
207 | |||
208 | Adds `::<_>` to a call of a generic method or function. | ||
209 | |||
210 | ```rust | ||
211 | // BEFORE | ||
212 | fn make<T>() -> T { todo!() } | ||
213 | fn main() { | ||
214 | let x = make┃(); | ||
215 | } | ||
216 | |||
217 | // AFTER | ||
218 | fn make<T>() -> T { todo!() } | ||
219 | fn main() { | ||
220 | let x = make::<${0:_}>(); | ||
221 | } | ||
222 | ``` | ||
223 | |||
204 | ## `apply_demorgan` | 224 | ## `apply_demorgan` |
205 | 225 | ||
206 | Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws). | 226 | Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws). |
@@ -239,6 +259,18 @@ fn main() { | |||
239 | } | 259 | } |
240 | ``` | 260 | ``` |
241 | 261 | ||
262 | ## `change_return_type_to_result` | ||
263 | |||
264 | Change the function's return type to Result. | ||
265 | |||
266 | ```rust | ||
267 | // BEFORE | ||
268 | fn foo() -> i32┃ { 42i32 } | ||
269 | |||
270 | // AFTER | ||
271 | fn foo() -> Result<i32, ${0:_}> { Ok(42i32) } | ||
272 | ``` | ||
273 | |||
242 | ## `change_visibility` | 274 | ## `change_visibility` |
243 | 275 | ||
244 | Adds or changes existing visibility specifier. | 276 | Adds or changes existing visibility specifier. |
@@ -293,12 +325,34 @@ enum Action { Move { distance: u32 }, Stop } | |||
293 | 325 | ||
294 | fn handle(action: Action) { | 326 | fn handle(action: Action) { |
295 | match action { | 327 | match action { |
296 | Action::Move { distance } => {} | 328 | $0Action::Move { distance } => {} |
297 | Action::Stop => {} | 329 | Action::Stop => {} |
298 | } | 330 | } |
299 | } | 331 | } |
300 | ``` | 332 | ``` |
301 | 333 | ||
334 | ## `fix_visibility` | ||
335 | |||
336 | Makes inaccessible item public. | ||
337 | |||
338 | ```rust | ||
339 | // BEFORE | ||
340 | mod m { | ||
341 | fn frobnicate() {} | ||
342 | } | ||
343 | fn main() { | ||
344 | m::frobnicate┃() {} | ||
345 | } | ||
346 | |||
347 | // AFTER | ||
348 | mod m { | ||
349 | $0pub(crate) fn frobnicate() {} | ||
350 | } | ||
351 | fn main() { | ||
352 | m::frobnicate() {} | ||
353 | } | ||
354 | ``` | ||
355 | |||
302 | ## `flip_binexpr` | 356 | ## `flip_binexpr` |
303 | 357 | ||
304 | Flips operands of a binary expression. | 358 | Flips operands of a binary expression. |
@@ -372,7 +426,7 @@ fn main() { | |||
372 | 426 | ||
373 | // AFTER | 427 | // AFTER |
374 | fn main() { | 428 | fn main() { |
375 | let var_name = (1 + 2); | 429 | let $0var_name = (1 + 2); |
376 | var_name * 4; | 430 | var_name * 4; |
377 | } | 431 | } |
378 | ``` | 432 | ``` |
@@ -679,7 +733,7 @@ fn main() { | |||
679 | let x: Result<i32, i32> = Result::Ok(92); | 733 | let x: Result<i32, i32> = Result::Ok(92); |
680 | let y = match x { | 734 | let y = match x { |
681 | Ok(a) => a, | 735 | Ok(a) => a, |
682 | _ => unreachable!(), | 736 | $0_ => unreachable!(), |
683 | }; | 737 | }; |
684 | } | 738 | } |
685 | ``` | 739 | ``` |
@@ -695,3 +749,21 @@ use std::┃collections::HashMap; | |||
695 | // AFTER | 749 | // AFTER |
696 | use std::{collections::HashMap}; | 750 | use std::{collections::HashMap}; |
697 | ``` | 751 | ``` |
752 | |||
753 | ## `unwrap_block` | ||
754 | |||
755 | This assist removes if...else, for, while and loop control statements to just keep the body. | ||
756 | |||
757 | ```rust | ||
758 | // BEFORE | ||
759 | fn foo() { | ||
760 | if true {┃ | ||
761 | println!("foo"); | ||
762 | } | ||
763 | } | ||
764 | |||
765 | // AFTER | ||
766 | fn foo() { | ||
767 | println!("foo"); | ||
768 | } | ||
769 | ``` | ||