vimrc/sources_non_forked/vim-snippets/snippets/rust.snippets

229 lines
4.9 KiB
Text
Raw Normal View History

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