Skip to content

Instantly share code, notes, and snippets.

@Pagliacii
Last active June 26, 2021 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pagliacii/b73965c3b8dfaf12963da2284ab0455b to your computer and use it in GitHub Desktop.
Save Pagliacii/b73965c3b8dfaf12963da2284ab0455b to your computer and use it in GitHub Desktop.
Useful snippets for Rust in VSCode.
{
// Place your snippets for rust here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"let variable declaration with type inference": {
"prefix": "let",
"body": "let $1 = $2;$0"
},
"let variable declaration with explicit type annotation": {
"prefix": "lett",
"body": "let $1: $2 = $3;$0"
},
"let mut variable declaration with type inference": {
"prefix": "letm",
"body": "let mut $1 = $2;$0"
},
"let mut variable declaration with explicit type annotation": {
"prefix": "lettm",
"body": "let mut $1: $2 = $3;$0"
},
"Struct definition": {
"prefix": "st",
"body": [
"struct $1 {",
" $2",
"}$0"
]
},
"Struct with new constructor": {
"prefix": "stn",
"body": [
"${1:pub }struct $2 {",
" $3",
"}",
"",
"impl $1 {",
" pub fn new($4) -> Self {",
" $1 {",
" $5",
" }",
" }",
"}$0"
]
},
"Public function": {
"prefix": "pfn",
"body": "${1:pub }fn $2($3)${4: -> $5} {$6}$0"
},
"Function definition": {
"prefix": "fn",
"body": "fn $1($2)${3: -> $4} {$5}$0"
},
"if / else": {
"prefix": "ife",
"body": [
"if $1 {",
" $2",
"} else {",
" $3",
"}$4"
]
},
"Unwrap Option with if let": {
"prefix": "iflet",
"body": [
"if let Some($1) = $2 {",
" $3",
"}$4"
]
},
"Struct/Trait implementaion": {
"prefix": "impl",
"body": [
"impl ${1:Type/Trait} ${2:for ${3:Type}} {",
" $4",
"}$0"
]
},
"Struct implementaion": {
"prefix": "impls",
"body": [
"impl ${1:Type} {",
" $2",
"}$0"
]
},
"Trait implementaion": {
"prefix": "implt",
"body": [
"impl ${1:Trait} for ${2:Type} {",
" $3",
"}$0"
]
},
"static item declaration": {
"prefix": "stat",
"body": "static $1: $2 = $3;$0"
},
"Test module": {
"prefix": "testmod",
"body": [
"#[cfg(test)]",
"mod tests {",
" use super::*$1;",
"",
" ${0:test}",
"}"
]
},
"Unit test function": {
"prefix": "test",
"body": [
"#[test]",
"fn ${1:it_works}($2)$3 {",
" $0",
"}"
]
},
"Insert for loop": {
"prefix": "for",
"body": [
"for $1 in $2 {",
" $3",
"}$0"
]
},
"#[derive(..)]": {
"prefix": "der",
"body": "#[derive(${1:Debug})]$0"
},
"assert!": {
"prefix": "as",
"body": "assert!(${1:predicate});$0"
},
"assert_eq!": {
"prefix": "ase",
"body": "assert_eq!(${1:expected}, ${2:actual});$0"
},
"AsRef trait implementation": {
"prefix": "asref",
"body": [
"impl AsRef<${1:Ref}> for ${2:Type} {",
" fn as_ref(&self) -> &$1 {",
" &self.${3:field}",
" }",
"}"
]
},
"AsMut trait implementation": {
"prefix": "asmut",
"body": [
"impl AsMut<${1:Ref}> for ${2:Type} {",
" fn as_mut(&mut self) -> &mut $1 {",
" &mut self.${3:field}",
" }",
"}"
]
},
"match pattern": {
"prefix": "mat",
"body": [
"match $1 {",
" $2 => $3,$0",
"}"
]
},
"Case clause of pattern match": {
"prefix": "case",
"body": "$1 => $2,$0"
},
"dbg! debugging macro": {
"prefix": "d",
"body": "dbg!($1)$0"
},
"dbg! debugging macro statement": {
"prefix": "d;",
"body": "dbg!(&$1)$0"
},
"enum definition": {
"prefix": "enum",
"body": [
"enum ${1:Name} {",
" $2,",
"}"
]
},
"pub enum definition": {
"prefix": "penum",
"body": [
"${1:pub }enum ${2:Name} {",
" $3,",
"}"
]
},
"else if": {
"prefix": "eli",
"body": [
"else if $1 {",
" $2",
"}$3"
]
},
"else": {
"prefix": "else",
"body": [
"else {",
" $1",
"}$2"
]
},
"extern crate": {
"prefix": "ec",
"body": "extern crate ${1:name};$2"
},
"Insert print!": {
"prefix": "pri",
"body": "print!(\"$1\"${2:, $3});$4"
},
"Insert println!": {
"prefix": "pln",
"body": "println!(\"$1\"${2:, $3});$4"
},
"print! with format param": {
"prefix": "pri,",
"body": "print!(\"{$1}\", $2);$3"
},
"println! with format param": {
"prefix": "pln,",
"body": "println!(\"{$1}\", $2);$3"
},
"Insert eprint!": {
"prefix": "epri",
"body": "eprint!(\"$1\"${2:, $3});$4"
},
"Insert eprintln!": {
"prefix": "epln",
"body": "eprintln!(\"$1\"${2:, $3});$4"
},
"eprint! with format param": {
"prefix": "epri,",
"body": "eprint!(\"{$1}\", $2);$3"
},
"eprintln! with format param": {
"prefix": "epln,",
"body": "eprintln!(\"{$1}\", $2);$3"
},
"allow lint attribute": {
"prefix": "allow",
"body": "#[allow(${1:unused_variables)]$2"
},
"Main function": {
"prefix": "main",
"body": [
"${1:pub }fn main() {",
" $2",
"}"
]
},
"feature attribute": {
"prefix": "feat",
"body": "#![feature(${1:plugin})]$2"
},
"#[..]": {
"prefix": "attr",
"body": "#[${1:inline}]$2"
},
"Declare (Sender, Receiver) pair of asynchronous channel()": {
"prefix": "chan",
"body": "let (${1:tx}, ${2:rx}): (Sender<${3:i32}>, Receiver<${4:i32}>) = $5;$6"
},
"Define crate meta attributes": {
"prefix": "crate",
"body": [
"// Crate name",
"#![crate_name = \"${1:crate_name}\"]",
"// Additional metadata attributes",
"#![desc = \"${2:Descrption.}\"]",
"#![license = \"${3:BSD}\"]",
"#![comment = \"${4:Comment.}\"]",
"// Specify the output type",
"#![crate_type = \"${5:lib}\"]",
"$6"
]
},
"Trait definition": {
"prefix": "trait",
"body": [
"trait ${1:Name} {",
" $2",
"}"
]
},
"spawn a thread": {
"prefix": "spawn",
"body": [
"thread::spawn(move |$1| {",
" $2",
"});"
]
},
"spawn a scoped thread": {
"prefix": "scoped",
"body": [
"thread::scoped(move |$1| {",
" $2",
"});"
]
},
"macro_rules!": {
"prefix": "macro",
"body": [
"macro_rules! ${1:name} {",
" (${2:matcher}) => (",
" $3",
" ),$4",
"}"
]
},
"Box::new()": {
"prefix": "box",
"body": "Box::new($1)"
},
"Rc::new()": {
"prefix": "rc",
"body": "Rc::new($1)"
},
"Bench function": {
"prefix": "bench",
"body": [
"#[bench]",
"fn ${1:bench_function_name}(b: &mut test::Bencher) {",
" b.iter(|$2| {",
" $3",
" })",
"}"
]
},
"#[cfg(...)]": {
"prefix": "cfg",
"body": "#[cfg($1)]"
},
"cfg attribute": {
"prefix": "cfg",
"body": "#[cfg(target_os = \"${1:linux}\")]"
},
"Drop trait implementation (destructor)": {
"prefix": "drop",
"body": [
"impl Drop for ${1:Name} {",
" fn drop(&mut self) {",
" $2",
" }",
"}"
]
},
"Module definition": {
"prefix": "mod",
"body": [
"mod ${1:Main} {",
" $2",
"}"
]
},
"Struct field definition": {
"prefix": "fd",
"body": "${1:name}: ${2:Type},"
},
"Constructor function": {
"prefix": "new",
"body": [
"${1:pub }fn new($2) -> ${3:Self} {",
" $3 {",
" $4",
" }",
"}"
]
},
"Result": {
"prefix": "res",
"body": "Result<${1:T}, ${2:E}>"
},
"Insert format macro": {
"prefix": "fmt",
"body": "format!(\"{$1}\", $2);"
},
"FIXME comment": {
"prefix": "fixme",
"body": "// FIXME: $1"
},
"TODO comment": {
"prefix": "todo",
"body": "// TODO: $1"
},
"#[ignore]": {
"prefix": "ig",
"body": "#[${1:ignore}]"
},
"while loop": {
"prefix": "wh",
"body": [
"while ${1:condition} {",
" $2",
"}"
]
},
".iter()": {
"prefix": ".it",
"body": ".iter()"
},
"unimplemented!()": {
"prefix": "unim",
"body": "unimplemented!()"
},
"loop": {
"prefix": "loop",
"body": [
"loop {",
" $1",
"}"
]
},
"Option<T>": {
"prefix": "opt",
"body": "Option<${1:T}>"
},
"Insert panic! macro": {
"prefix": "pan",
"body": "panic!(\"{$1}\");"
},
"static string declaration": {
"prefix": "ss",
"body": "static $1: &'static str = \"$2\";"
},
"Type alias": {
"prefix": "ty",
"body": "type ${1:NewName} = $2;"
},
"Insert todo! macro": {
"prefix": "td",
"body": "todo!();"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment