vim-snippets/snippets/rust.snippets

180 lines
3.5 KiB
Plaintext
Raw Normal View History

2014-05-01 14:49:00 -04:00
#################
# Rust Snippets #
#################
# Functions
snippet fn "Function definition"
fn ${1:function_name}(${2})${3} {
2014-05-01 14:49:00 -04:00
${0}
}
snippet test "Unit test function"
2014-05-01 14:49:00 -04:00
#[test]
fn ${1:test_function_name}() {
${0}
}
snippet bench "Bench function" b
#[bench]
fn ${1:bench_function_name}(b: &mut test::Bencher) {
b.iter(|| {
${0}
})
}
snippet new "Constructor function"
2014-05-01 14:49:00 -04:00
pub fn new(${2}) -> ${1:Name} {
$1 { ${3} };
2014-05-01 14:49:00 -04:00
}
snippet main "Main function"
2014-05-01 14:49:00 -04:00
pub fn main() {
${0}
}
snippet let "let variable declaration"
let ${1:name}${2:: ${3:type}} = ${4};
snippet letm "let mut variable declaration"
let mut ${1:name}${2:: ${3:type}} = ${4};
snippet pln "println!"
2014-05-01 14:49:00 -04:00
println!("${1}");
snippet pln, "println! with format param"
2014-05-01 14:49:00 -04:00
println!("${1}", ${2});
# Modules
snippet ec "extern crate"
2014-05-01 14:49:00 -04:00
extern crate ${1:sync};
snippet ecl "extern crate log"
#[macro_use]
extern crate log;
2014-05-01 14:49:00 -04:00
snippet mod
mod ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
${0}
} /* $1 */
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-02 14:37:58 -04:00
// Crate name
#![crate_name = "${1:crate_name}"]
2014-05-01 14:49:00 -04:00
// Additional metadata attributes
2014-09-02 14:37:58 -04:00
#![desc = "${2:Descrption.}"]
#![license = "${3:BSD}"]
#![comment = "${4:Comment.}"]
2014-05-01 14:49:00 -04:00
// Specify the output type
2014-09-02 14:37:58 -04:00
#![crate_type = "${5:lib}"]
2014-05-01 14:49:00 -04:00
# Common types
snippet opt "Option<T>"
Option<${1:i32}>
snippet res "Result<T, E>"
2014-05-01 14:49:00 -04:00
Result<${1:~str}, ${2:()}>
# Control structures
2014-05-01 14:49:00 -04:00
snippet if
if ${1} {
${0}
}
snippet ife "if / else"
if ${1} {
${2}
} else {
${0}
}
snippet el "else"
else {
${0}
}
snippet eli "else if"
else if ${1} {
2014-05-01 14:49:00 -04:00
${0}
}
snippet mat "match pattern"
2014-05-01 14:49:00 -04:00
match ${1} {
${2} => ${3},
}
snippet loop "loop {}" b
loop {
${0}
}
snippet while "while loop"
2014-05-01 14:49:00 -04:00
while ${1:condition} {
${0}
}
snippet for "for ... in ... loop"
for ${1:i} in ${2:0u..10} {
2014-05-01 14:49:00 -04:00
${0}
}
# TODO commenting
snippet todo "TODO comment"
// [TODO]: ${0:Description}
snippet fixme "FIXME comment"
// FIXME: $0
2014-05-01 14:49:00 -04:00
# Struct
snippet st "Struct definition"
2014-05-01 14:49:00 -04:00
struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
${0}
}
snippet impl "Struct/Trait implementation"
impl ${1:Type/Trait}${2: ${3:for Type}} {
${0}
}
snippet stn "Struct with new constructor"
pub struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
${0}
}
2014-05-01 14:49:00 -04:00
impl $1 {
pub fn new(${2}) -> $1 {
$1 { ${3} };
2014-05-01 14:49:00 -04:00
}
}
snippet type "Type alias"
type ${1:NewName} = $2;
snippet enum "enum definition"
enum ${1:Name} {
${2},
2014-05-01 14:49:00 -04:00
}
# Traits
snippet trait "Trait definition"
trait ${1:Name} {
2014-05-01 14:49:00 -04:00
${0}
}
snippet drop "Drop trait implementation (destructor)"
2014-05-01 14:49:00 -04:00
impl Drop for ${1:Name} {
fn drop(&mut self) {
${0}
}
}
# Statics
snippet ss "static string declaration"
2014-05-01 14:49:00 -04:00
static ${1}: &'static str = "${0}";
snippet stat "static item declaration"
static ${1}: ${2:usize} = ${0};
# Concurrency
snippet spawn "spawn a task or thread"
spawn(proc() {
${0}
});
snippet chan "Declare (Sender, Receiver) pair of asynchronous channel()"
let (${1:tx}, ${2:rx}): (Sender<${3:i32}>, Receiver<${4:i32}>) = channel();
# Testing
snippet as "assert!"
assert!(${1:predicate})
snippet ase "assert_eq!"
assert_eq!(${1:expected}, ${2:actual})