-
Notifications
You must be signed in to change notification settings - Fork 2k
/
rust.snippets
252 lines (249 loc) · 5.4 KB
/
rust.snippets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#################
# Rust Snippets #
#################
# Functions
snippet fn "Function definition"
fn ${1:function_name}(${2})${3} {
${0}
}
snippet pfn "Function definition"
pub fn ${1:function_name}(${2})${3} {
${0}
}
snippet afn "Async function definition"
async fn ${1:function_name}(${2})${3} {
${0}
}
snippet pafn "Async function definition"
pub async fn ${1:function_name}(${2})${3} {
${0}
}
snippet bench "Bench function" b
#[bench]
fn ${1:bench_function_name}(b: &mut test::Bencher) {
b.iter(|| {
${0}
})
}
snippet new "Constructor function"
pub fn new(${2}) -> ${1:Self} {
$1 { ${3} }
}
snippet main "Main function"
pub fn main() {
${0}
}
snippet let "let variable declaration with type inference"
let ${1} = ${2};
snippet lett "let variable declaration with explicit type annotation"
let ${1}: ${2} = ${3};
snippet letm "let mut variable declaration with type inference"
let mut ${1} = ${2};
snippet lettm "let mut variable declaration with explicit type annotation"
let mut ${1}: ${2} = ${3};
snippet pri "print!"
print!("${1}");
snippet pri, "print! with format param"
print!("${1}{${2}}", ${3});
snippet pln "println!"
println!("${1}");
snippet pln, "println! with format param"
println!("${1}{${2}}", ${3});
snippet fmt "format!"
format!("${1}{${2}}", ${3});
snippet d "dbg! debugging macro"
dbg!(${0:${VISUAL}})
snippet d; "dbg! debugging macro statement"
dbg!(&${1});
${0}
# Modules
snippet ec "extern crate"
extern crate ${1:sync};
snippet ecl "extern crate log"
#[macro_use]
extern crate log;
snippet mod
mod ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
${0}
} /* $1 */
# Testing
snippet as "assert!"
assert!(${1:predicate});
snippet ase "assert_eq!"
assert_eq!(${1:expected}, ${2:actual});
snippet test "Unit test function"
#[test]
fn ${1:function_name}_test() {
${0}
}
snippet testmod "Test module" b
#[cfg(test)]
mod tests {
use super::${1:*};
test${0}
}
snippet ig "#[ignore]"
#[ignore]
# Attributes
snippet allow "allow lint attribute" b
#[allow(${1:unused_variables})]
snippet cfg "cfg attribute" b
#[cfg(${1:target_os = "linux"})]
snippet feat "feature attribute" b
#![feature(${1:plugin})]
snippet der "#[derive(..)]" b
#[derive(${1:Debug})]
snippet attr "#[..]" b
#[${1:inline}]
snippet crate "Define create meta attributes"
// Crate name
#![crate_name = "${1:crate_name}"]
// Additional metadata attributes
#![desc = "${2:Description.}"]
#![license = "${3:BSD}"]
#![comment = "${4:Comment.}"]
// Specify the output type
#![crate_type = "${5:lib}"]
# Common types
snippet opt "Option<T>"
Option<${1:i32}>
snippet res "Result<T, E>"
Result<${1:&str}, ${2:()}>
# Control structures
snippet if
if ${1} {
${0:${VISUAL}}
}
snippet ife "if / else"
if ${1} {
${2:${VISUAL}}
} else {
${0}
}
snippet ifl "if let (...)"
if let ${1:Some($2)} = $3 {
${0:${VISUAL}}
}
snippet el "else"
else {
${0:${VISUAL}}
}
snippet eli "else if"
else if ${1} {
${0:${VISUAL}}
}
snippet mat "match pattern"
match ${1} {
${2} => ${3}
}
snippet case "Case clause of pattern match"
${1:_} => ${2:expression}
snippet = "=> "
=> $0
snippet loop "loop {}" b
loop {
${0:${VISUAL}}
}
snippet wh "while loop"
while $1 {
${0:${VISUAL}}
}
snippet whl "while let (...)"
while let ${1:Some($2)} = $3 {
${0:${VISUAL}}
}
snippet for "for ... in ... loop"
for ${1:i} in ${2} {
${0}
}
# TODO commenting
snippet todo "TODO comment"
// TODO: $0
snippet fixme "FIXME comment"
// FIXME: $0
# Struct
snippet st "Struct definition"
struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
${0}
}
snippet impl "Struct/Trait implementation"
impl ${1:Type/Trait}${2: for $3} {
${0}
}
snippet stn "Struct with new constructor"
pub struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
${0}
}
impl$2 $1$2 {
pub fn new(${4}) -> Self {
$1 { ${5} }
}
}
snippet ty "Type alias"
type ${1:NewName} = $2;
snippet enum "enum definition"
enum ${1:Name} {
${2},
}
snippet penum "pub enum definition"
pub enum ${1:Name} {
${2},
}
# Traits
snippet trait "Trait definition"
trait ${1:Name} {
${0}
}
snippet drop "Drop trait implementation (destructor)"
impl Drop for $1 {
fn drop(&mut self) {
${0}
}
}
# Statics
snippet ss "static string declaration"
static ${1}: &'static str = "${0}";
snippet stat "static item declaration"
static ${1}: ${2:usize} = ${0};
# Concurrency
snippet spawn "spawn a thread"
thread::spawn(${1:move }|| {
${0}
});
snippet chan "Declare (Sender, Receiver) pair of asynchronous channel()"
let (${1:tx}, ${2:rx}): (Sender<${3:i32}>, Receiver<${4:i32}>) = channel();
# Implementations
snippet asref "AsRef trait implementation"
impl AsRef<${1:Ref}> for ${2:Type} {
fn as_ref(&self) -> &${3:$1} {
&self.${0:field}
}
}
snippet asmut "AsMut trait implementation"
impl AsMut<${1:Ref}> for ${2:Type} {
fn as_mut(&mut self) -> &mut ${3:$1} {
&mut self.${0:field}
}
}
snippet fd "Struct field definition" w
${1:name}: ${2:Type},
snippet || "Closure, anonymous function (inline)" i
${1:move }|$2| { $3 }
snippet |} "Closure, anonymous function (block)" i
${1:move }|$2| {
$3
}
snippet macro "macro_rules!" b
macro_rules! ${1:name} {
(${2:matcher}) => (
$3
)
}
snippet boxp "Box::new()"
Box::new(${0:${VISUAL}})
snippet rc "Rc::new()"
Rc::new(${0:${VISUAL}})
snippet unim "unimplemented!()"
unimplemented!()
snippet use "use ...;" b
use ${1:std::$2};