2014-07-02 07:18:18 -04:00
|
|
|
#################
|
|
|
|
# Rust Snippets #
|
|
|
|
#################
|
|
|
|
|
|
|
|
# Functions
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet fn "Function definition"
|
2015-01-18 07:58:28 -05:00
|
|
|
fn ${1:function_name}(${2})${3} {
|
2014-07-02 07:18:18 -04:00
|
|
|
${0}
|
|
|
|
}
|
2015-12-08 08:20:04 -05:00
|
|
|
snippet pfn "Function definition"
|
|
|
|
pub fn ${1:function_name}(${2})${3} {
|
|
|
|
${0}
|
|
|
|
}
|
2015-01-18 07:58:28 -05:00
|
|
|
snippet bench "Bench function" b
|
|
|
|
#[bench]
|
|
|
|
fn ${1:bench_function_name}(b: &mut test::Bencher) {
|
|
|
|
b.iter(|| {
|
|
|
|
${0}
|
|
|
|
})
|
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet new "Constructor function"
|
2018-12-17 06:28:27 -05:00
|
|
|
pub fn new(${2}) -> ${1:Self} {
|
2016-03-14 06:04:57 -04:00
|
|
|
$1 { ${3} }
|
2014-07-02 07:18:18 -04:00
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet main "Main function"
|
2014-07-02 07:18:18 -04:00
|
|
|
pub fn main() {
|
|
|
|
${0}
|
|
|
|
}
|
2016-02-20 08:13:10 -05:00
|
|
|
snippet let "let variable declaration with type inference"
|
2017-02-11 08:01:38 -05:00
|
|
|
let ${1} = ${2};
|
2016-02-20 08:13:10 -05:00
|
|
|
snippet lett "let variable declaration with explicit type annotation"
|
2017-07-06 08:57:35 -04:00
|
|
|
let ${1}: ${2} = ${3};
|
2016-02-20 08:13:10 -05:00
|
|
|
snippet letm "let mut variable declaration with type inference"
|
2017-07-06 08:57:35 -04:00
|
|
|
let mut ${1} = ${2};
|
2016-02-20 08:13:10 -05:00
|
|
|
snippet lettm "let mut variable declaration with explicit type annotation"
|
2017-07-06 08:57:35 -04:00
|
|
|
let mut ${1}: ${2} = ${3};
|
2017-05-02 08:42:08 -04:00
|
|
|
snippet pri "print!"
|
|
|
|
print!("${1}");
|
|
|
|
snippet pri, "print! with format param"
|
2018-02-04 06:35:08 -05:00
|
|
|
print!("${1}{${2}}", ${3});
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet pln "println!"
|
2014-07-02 07:18:18 -04:00
|
|
|
println!("${1}");
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet pln, "println! with format param"
|
2018-02-04 06:35:08 -05:00
|
|
|
println!("${1}{${2}}", ${3});
|
2017-05-02 08:42:08 -04:00
|
|
|
snippet fmt "format!"
|
2018-02-04 06:35:08 -05:00
|
|
|
format!("${1}{${2}}", ${3});
|
2019-03-08 06:04:56 -05:00
|
|
|
snippet d "dbg! debugging macro"
|
|
|
|
dbg!(${0:${VISUAL}})
|
|
|
|
snippet d; "dbg! debugging macro statement"
|
|
|
|
dbg!(&${1});
|
|
|
|
${0}
|
2015-03-14 16:02:10 -04:00
|
|
|
# Modules
|
|
|
|
snippet ec "extern crate"
|
2014-07-02 07:18:18 -04:00
|
|
|
extern crate ${1:sync};
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet ecl "extern crate log"
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2014-07-02 07:18:18 -04:00
|
|
|
snippet mod
|
|
|
|
mod ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
|
|
|
|
${0}
|
|
|
|
} /* $1 */
|
2018-12-17 06:28:27 -05:00
|
|
|
# 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}
|
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet testmod "Test module" b
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::${1:*};
|
|
|
|
|
|
|
|
test${0}
|
|
|
|
}
|
2019-03-08 06:04:56 -05:00
|
|
|
snippet ig "#[ignore]"
|
|
|
|
#[ignore]
|
2015-03-14 16:02:10 -04:00
|
|
|
# Attributes
|
|
|
|
snippet allow "allow lint attribute" b
|
2019-03-08 06:04:56 -05:00
|
|
|
#[allow(${1:unused_variables})]
|
2015-03-14 16:02:10 -04:00
|
|
|
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"
|
2014-09-27 11:32:18 -04:00
|
|
|
// Crate name
|
|
|
|
#![crate_name = "${1:crate_name}"]
|
2014-07-02 07:18:18 -04:00
|
|
|
// Additional metadata attributes
|
2014-09-27 11:32:18 -04:00
|
|
|
#![desc = "${2:Descrption.}"]
|
|
|
|
#![license = "${3:BSD}"]
|
|
|
|
#![comment = "${4:Comment.}"]
|
2014-07-02 07:18:18 -04:00
|
|
|
// Specify the output type
|
2014-09-27 11:32:18 -04:00
|
|
|
#![crate_type = "${5:lib}"]
|
2014-07-02 07:18:18 -04:00
|
|
|
# Common types
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet opt "Option<T>"
|
|
|
|
Option<${1:i32}>
|
|
|
|
snippet res "Result<T, E>"
|
2014-07-02 07:18:18 -04:00
|
|
|
Result<${1:~str}, ${2:()}>
|
2015-03-14 16:02:10 -04:00
|
|
|
# Control structures
|
2014-07-02 07:18:18 -04:00
|
|
|
snippet if
|
2015-01-18 07:58:28 -05:00
|
|
|
if ${1} {
|
2017-02-11 08:01:38 -05:00
|
|
|
${0:${VISUAL}}
|
2015-01-18 07:58:28 -05:00
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet ife "if / else"
|
2015-01-18 07:58:28 -05:00
|
|
|
if ${1} {
|
2017-02-11 08:01:38 -05:00
|
|
|
${2:${VISUAL}}
|
2015-01-18 07:58:28 -05:00
|
|
|
} else {
|
|
|
|
${0}
|
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet el "else"
|
2015-01-18 07:58:28 -05:00
|
|
|
else {
|
2017-02-11 08:01:38 -05:00
|
|
|
${0:${VISUAL}}
|
2015-01-18 07:58:28 -05:00
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet eli "else if"
|
2015-01-18 07:58:28 -05:00
|
|
|
else if ${1} {
|
2017-02-11 08:01:38 -05:00
|
|
|
${0:${VISUAL}}
|
2014-07-02 07:18:18 -04:00
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet mat "match pattern"
|
2014-07-02 07:18:18 -04:00
|
|
|
match ${1} {
|
2015-03-14 16:02:10 -04:00
|
|
|
${2} => ${3}
|
2014-07-02 07:18:18 -04:00
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet case "Case clause of pattern match"
|
|
|
|
${1:_} => ${2:expression}
|
2018-12-17 06:28:27 -05:00
|
|
|
snippet = "=> "
|
|
|
|
=> $0
|
2015-01-18 07:58:28 -05:00
|
|
|
snippet loop "loop {}" b
|
|
|
|
loop {
|
2017-02-11 08:01:38 -05:00
|
|
|
${0:${VISUAL}}
|
2015-01-18 07:58:28 -05:00
|
|
|
}
|
2015-12-08 08:20:04 -05:00
|
|
|
snippet wh "while loop"
|
2014-07-02 07:18:18 -04:00
|
|
|
while ${1:condition} {
|
2017-02-11 08:01:38 -05:00
|
|
|
${0:${VISUAL}}
|
2014-07-02 07:18:18 -04:00
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet for "for ... in ... loop"
|
2015-07-13 06:22:46 -04:00
|
|
|
for ${1:i} in ${2} {
|
2014-07-02 07:18:18 -04:00
|
|
|
${0}
|
|
|
|
}
|
|
|
|
# TODO commenting
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet todo "TODO comment"
|
2018-12-17 06:28:27 -05:00
|
|
|
// TODO: $0
|
2015-01-18 07:58:28 -05:00
|
|
|
snippet fixme "FIXME comment"
|
|
|
|
// FIXME: $0
|
2014-07-02 07:18:18 -04:00
|
|
|
# Struct
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet st "Struct definition"
|
2014-07-02 07:18:18 -04:00
|
|
|
struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
|
|
|
|
${0}
|
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet impl "Struct/Trait implementation"
|
|
|
|
impl ${1:Type/Trait}${2: for ${3:Type}} {
|
|
|
|
${0}
|
|
|
|
}
|
|
|
|
snippet stn "Struct with new constructor"
|
|
|
|
pub struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
|
2015-01-18 07:58:28 -05:00
|
|
|
${0}
|
|
|
|
}
|
2014-07-02 07:18:18 -04:00
|
|
|
|
|
|
|
impl $1 {
|
2017-03-07 12:04:28 -05:00
|
|
|
pub fn new(${2}) -> Self {
|
2016-03-14 06:04:57 -04:00
|
|
|
$1 { ${3} }
|
2014-07-02 07:18:18 -04:00
|
|
|
}
|
|
|
|
}
|
2018-12-17 06:28:27 -05:00
|
|
|
snippet ty "Type alias"
|
2015-03-14 16:02:10 -04:00
|
|
|
type ${1:NewName} = $2;
|
|
|
|
snippet enum "enum definition"
|
2015-01-18 07:58:28 -05:00
|
|
|
enum ${1:Name} {
|
2015-03-14 16:02:10 -04:00
|
|
|
${2},
|
2014-07-02 07:18:18 -04:00
|
|
|
}
|
2018-12-17 06:28:27 -05:00
|
|
|
snippet penum "pub enum definition"
|
|
|
|
pub enum ${1:Name} {
|
|
|
|
${2},
|
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
# Traits
|
|
|
|
snippet trait "Trait definition"
|
|
|
|
trait ${1:Name} {
|
2014-07-02 07:18:18 -04:00
|
|
|
${0}
|
|
|
|
}
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet drop "Drop trait implementation (destructor)"
|
2014-07-02 07:18:18 -04:00
|
|
|
impl Drop for ${1:Name} {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
${0}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Statics
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet ss "static string declaration"
|
2014-07-02 07:18:18 -04:00
|
|
|
static ${1}: &'static str = "${0}";
|
2015-03-14 16:02:10 -04:00
|
|
|
snippet stat "static item declaration"
|
2015-01-18 07:58:28 -05:00
|
|
|
static ${1}: ${2:usize} = ${0};
|
2015-03-14 16:02:10 -04:00
|
|
|
# Concurrency
|
|
|
|
snippet scoped "spawn a scoped thread"
|
|
|
|
thread::scoped(${1:move }|| {
|
|
|
|
${0}
|
|
|
|
});
|
|
|
|
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();
|
2018-06-14 06:31:12 -04:00
|
|
|
# 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}
|
|
|
|
}
|
|
|
|
}
|
2018-12-17 06:28:27 -05:00
|
|
|
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 box "Box::new()"
|
|
|
|
Box::new(${0:${VISUAL}})
|
2019-11-16 10:28:42 -05:00
|
|
|
snippet rc "Rc::new()"
|
|
|
|
Rc::new(${0:${VISUAL}})
|
|
|
|
snippet unim "unimplemented!()"
|
|
|
|
unimplemented!()
|