Skip to content

Instantly share code, notes, and snippets.

@thomcc
Last active September 12, 2019 02:26
Show Gist options
  • Save thomcc/06c827f29e52e9e94176902caf2cfe22 to your computer and use it in GitHub Desktop.
Save thomcc/06c827f29e52e9e94176902caf2cfe22 to your computer and use it in GitHub Desktop.
vscode rust snippets
{
"unwrap or return": {
"scope": "rust",
"prefix": ["unwrap or return", "let if let"],
"body": [
"let $1 = if let ${3:Some($1)} = ${2:expr} {",
"\t$1",
"} else {",
"\treturn ${0:Ok(())};",
"};"
]
},
"impl std::fmt::Display": {
"description": "Implement std::fmt::Display for some type",
"scope": "rust",
"prefix": ["impl display", "impl-display"],
"body": [
"impl std::fmt::Display for ${1:Type} {",
"\tfn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {",
"\t\t${0:unimplemented!()}",
"\t}",
"}"
]
},
"impl Default": {
"description": "Implement Default for some type",
"scope": "rust",
"prefix": ["impl default", "impl-default"],
"body": [
"impl Default for $1 {",
"\t#[inline]",
"\tfn default() -> Self {",
"\t\t${0:Self::new()}",
"\t}",
"}",
]
},
"impl From": {
"description": "Implement From/Into conversions for some type",
"scope": "rust",
"prefix": ["impl from", "impl-from"],
"body": [
"impl From<${1:Src}> for ${2:Dest} {",
"\t#[inline]",
"\tfn from(src: $1) -> Self {",
"\t\t${0:unimplemented!()}",
"\t}",
"}",
]
},
"impl Iterator": {
"description": "Basic iterator boilerplate, inlcuding size_hint",
"scope": "rust",
"prefix": ["impl iter", "impl-iter"],
"body": [
"impl Iterator for ${1:Type} {",
"\ttype Item = ${2:ItemType}",
"\t#[inline]",
"\tfn next(&mut self) -> Option<Self::Item> {",
"\t\t${0:unimplemented!()}",
"\t}",
"\t",
"\t#[inline]",
"\tfn size_hint(&self) -> (usize, Option<usize>) {",
"\t\t(0, None)",
"\t}",
"}",
]
},
"impl FromStr with error": {
"description": "Implement std::str::FromStr, and error type",
"scope": "rust",
"prefix": ["impl fromstr", "impl-fromstr"],
"body": [
"impl std::str::FromStr for ${1:Type} {",
"\ttype Err = Parse$1Error;",
"\tfn from_str(src: &str) -> Result<$1, Self::Err> {",
"\t\t${0:unimplemented!()}",
"\t}",
"}",
"",
"#[derive(Debug, PartialEq, Clone)]",
"pub struct Parse$1Error(String);",
"",
"impl std::fmt::Display for Parse$1Error {",
"\tfn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {",
"\t\tf.write_str(\"Error parsing $1: \")?;",
"\t\tf.write_str(&self.0)",
"\t}",
"}",
"impl std::error::Error for Parse$1Error {}",
]
},
"impl std::fmt::Debug": {
"description": "Implement std::fmt::Debug, for e.g. when you don't want/cannot include some fields in the derived output",
"scope": "rust",
"prefix": ["impl debug", "impl-debug"],
"body": [
"impl std::fmt::Debug for ${1:Type} {",
"\tfn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {",
"\t\t${0:f.debug_struct(\"$1\").field(\"foo\", &self.foo).finish()}",
"\t}",
"}"
]
},
"impl IntoIterator": {
"scope": "rust",
"prefix": ["impl intoiterator", "impl-into-iterator"],
"body": [
"impl IntoIterator for ${1:Type} {",
"\ttype IntoIter = ${2:Iter};",
"\ttype Item = ${3:ItemTy};",
"\t#[inline]",
"\tfn into_iter(self) -> Self::IntoIter {",
"\t\t${0:unimplemented!()}",
"\t}",
"}",
]
},
"impl ref IntoIterator": {
"description": "Implement IntoIterator for a reference",
"scope": "rust",
"prefix": ["impl intoiterator ref", "impl-into-iterator-ref"],
"body": [
"impl<'a> IntoIterator for &'a ${1:Type} {",
"\ttype IntoIter = ${2:Iter};",
"\ttype Item = &'a ${3:ItemTy};",
"\t#[inline]",
"\tfn into_iter(self) -> Self::IntoIter {",
"\t\t${0:self.iter()}",
"\t}",
"}",
]
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment