rustysnips
Various VS Code snippets for the Rust language.
Support for Selected Text
Many of the snippets will automagically surround the currently selected text with the snippet template code, but in order to get this to work in VS Code, you will need to do the following one-time configuration change:
- Open VS Code
- Press
Ctrl + K
followed by Ctrl + S
(this should open the VS Code keyboard shortcuts page)
- Search for
surround with snippet
- Assign a keybind of your choosing or use
Ctrl + K Ctrl + M
Standard Process for Surrounding Selected Text with Snippet
After you have configured VS Code (see previous steps) and you have some selected text you want to surround a snippet with, then do the following:
- Press the keybinding you configured previously
- Type the snippet name or shortcut
- Select the appropriate snippet
- Press
Enter
VS Code Snippets
block
What you type:
What you get:
{
// new code block ...
}
Any selected text will be included in the new code block. Also, this snippet is smart and will automatically add a ;
automatically if needed (e.g. you selected some text after the arrow in a match
arm you want to turn into a code block).
error match
What you type:
What you get:
match <first tab position here> {
Ok(<second tab position here>) => <third tab position here>,
Err(<fourth tab position here>) => <fifth tab position here>
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.
for in enumerate()
What you type:
What you get:
for (i, <first tab position here with default &item>) in <second tab position here; choose: iter(),into_iter(),iter_mut()>.enumerate() {
<selected text goes here>
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.
for in loop
What you type:
What you get:
for <first tab position here> in <second tab position here> {
<selected text goes here>
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.
if
What you type:
What you get:
if <first tab position here> {
<selected text goes here>
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.
if/else
What you type:
What you get:
if <first tab position here> {
<selected text goes here>
} else {
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.
if let
What you type:
What you get:
if let <first tab position here; select: Some(), Err()> = <second tab position here> {
<selected text goes here>
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.
loop
What you type:
What you get:
loop {
<selected text goes here>
}
match
What you type:
What you get:
match <first tab position here> {
<second tab position here> => <third tab position here>,
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.
optional match
What you type:
optional-match
match?
mt?
match <first tab position here> {
Some(<second tab position here>) => <third tab position here>,
None => <fourth tab position here>
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.
optional while
What you type:
optional-while
while?
ow?
What you get:
while let Some(<first tab position here with default: value>) = <second tab position here> {
<selected text goes here>
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.
println! macro
What you type:
What you get:
println!("<cursor is here>");
struct declaration
What you type:
declare-struct
define-struct
ds
struct
stc
What you get:
struct <first tab position here with default: name> {
<second tab position here>
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.
trait declaration
What you type:
What you get:
trait <first tab position here with default: name> {
<second tab position here>
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.
while
What you type:
What you get:
while <first tab position here> {
<selected text goes here>
}
Once the code snippet fills in the above code template, use the Tab
key to quickly fill in the code.