1
0
Fork 0
mirror of synced 2024-11-27 03:05:36 -05:00
ultimate-vim/sources_non_forked/vim-snippets/snippets/rust.snippets

253 lines
5.4 KiB
Text
Raw Permalink Normal View History

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}
}
2020-05-10 10:24:38 -04:00
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}
}
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
2021-06-23 05:57:12 -04:00
#![desc = "${2:Description.}"]
2014-09-27 11:32:18 -04:00
#![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>"
2020-12-04 16:15:32 -05: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}
}
2020-06-21 11:50:44 -04:00
snippet ifl "if let (...)"
2020-12-04 16:15:32 -05:00
if let ${1:Some($2)} = $3 {
2020-06-21 11:50:44 -04:00
${0:${VISUAL}}
}
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"
while $1 {
2017-02-11 08:01:38 -05:00
${0:${VISUAL}}
2014-07-02 07:18:18 -04:00
}
2020-06-21 11:50:44 -04:00
snippet whl "while let (...)"
2020-12-04 16:15:32 -05:00
while let ${1:Some($2)} = $3 {
2020-06-21 11:50:44 -04:00
${0:${VISUAL}}
}
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"
2020-12-04 16:15:32 -05:00
struct ${1:`substitute(vim_snippets#Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} {
2014-07-02 07:18:18 -04:00
${0}
}
2015-03-14 16:02:10 -04:00
snippet impl "Struct/Trait implementation"
2020-12-04 16:15:32 -05:00
impl ${1:Type/Trait}${2: for $3} {
2015-03-14 16:02:10 -04:00
${0}
}
snippet stn "Struct with new constructor"
2020-12-04 16:15:32 -05:00
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
2020-06-21 11:50:44 -04:00
impl$2 $1$2 {
pub fn new(${4}) -> Self {
$1 { ${5} }
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)"
2020-12-04 16:15:32 -05:00
impl Drop for $1 {
2014-07-02 07:18:18 -04:00
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 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
)
}
2020-06-21 11:50:44 -04:00
snippet boxp "Box::new()"
2018-12-17 06:28:27 -05:00
Box::new(${0:${VISUAL}})
2019-11-16 10:28:42 -05:00
snippet rc "Rc::new()"
Rc::new(${0:${VISUAL}})
snippet unim "unimplemented!()"
unimplemented!()
2020-06-21 11:50:44 -04:00
snippet use "use ...;" b
2020-12-04 16:15:32 -05:00
use ${1:std::$2};