Last active
November 22, 2018 09:16
-
-
Save h4z31/e47c87ae828792deaccc31209c2cf9f7 to your computer and use it in GitHub Desktop.
my vscode snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// Place your snippets for rust here. Each snippet is defined under a snippet name and has a prefix, body and | |
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are: | |
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the | |
// same ids are connected. | |
// Example: | |
// "Print to console": { | |
// "prefix": "log", | |
// "body": [ | |
// "console.log('$1');", | |
// "$2" | |
// ], | |
// "description": "Log output to console" | |
// } | |
"read a line from stdin": { | |
"prefix": "read", | |
"description": "read a line from stdin and convert into specified type", | |
"body": [ | |
"fn read<T: std::str::FromStr>(i: &mut StdinLock) -> T {", | |
" let mut s = String::new();", | |
" i.by_ref().read_line(&mut s).ok();", | |
" s.trim().parse().ok().unwrap()", | |
"}", | |
"$1", | |
], | |
}, | |
"read cols from stdin": { | |
"prefix": "reads", | |
"description": "read lines from stdin and convert into specified type", | |
"body": [ | |
"fn reads<T: std::str::FromStr>(i: &mut StdinLock) -> Vec<T> {", | |
" let mut s = String::new();", | |
" i.by_ref().read_line(&mut s).ok();", | |
" s.trim().split_whitespace().map(|e| e.parse().ok().unwrap()).collect()", | |
"}", | |
"$1" | |
], | |
}, | |
"read lines from stdin": { | |
"prefix": "read_n", | |
"description": "read lines from stdin and convert into specified type", | |
"body": [ | |
"fn read<T: std::str::FromStr>(i: &mut StdinLock) -> T {", | |
" let mut s = String::new();", | |
" i.by_ref().read_line(&mut s).ok();", | |
" s.trim().parse().ok().unwrap()", | |
"}", | |
"", | |
"fn read_n<T: std::str::FromStr>(i: &mut StdinLock, n: usize) -> Vec<T> {", | |
" (0..n).map(|_x| read(i)).collect()", | |
"}", | |
"$1", | |
], | |
}, | |
"scan n lines from stdin": { | |
"prefix": "scan", | |
"description": "read lines from stdin and scan", | |
"body": [ | |
"fn read<T: std::str::FromStr>(i: &mut StdinLock) -> T {", | |
" let mut s = String::new();", | |
" i.by_ref().read_line(&mut s).ok();", | |
" s.trim().parse().ok().unwrap()", | |
"}", | |
"", | |
"fn scan<T: std::str::FromStr>(i: &mut StdinLock, n: usize, mut f: impl FnMut(T)) {", | |
" for _ in 0..n {", | |
" f(read(i));", | |
" }", | |
"}", | |
"$1", | |
], | |
}, | |
"scan n collections from stdin": { | |
"prefix": "scans", | |
"description": "read lines from stdin and scan", | |
"body": [ | |
"fn reads<T: std::str::FromStr>(i: &mut StdinLock) -> Vec<T> {", | |
" let mut s = String::new();", | |
" i.by_ref().read_line(&mut s).ok();", | |
" s.trim().split_whitespace().map(|e| e.parse().ok().unwrap()).collect()", | |
"}", | |
"", | |
"fn scans<T: std::str::FromStr>(i: &mut StdinLock, n: usize, mut f: impl FnMut(Vec<T>)) {", | |
" for _ in 0..n {", | |
" f(reads(i));", | |
" }", | |
"}", | |
"$1", | |
], | |
}, | |
"scan items in line": { | |
"prefix": "scan_inline", | |
"description": "scan items in a line", | |
"body": [ | |
"fn scan_inline<T: std::str::FromStr>(i: &mut StdinLock, f: impl FnMut(T)) {", | |
" let mut s = String::new();", | |
" i.by_ref().read_line(&mut s).ok();", | |
" s.trim()", | |
" .split_whitespace()", | |
" .map(|e| e.parse().ok().unwrap())", | |
" .for_each(f);", | |
"}", | |
] | |
}, | |
"read n collections from stdin": { | |
"prefix": "reads_n", | |
"description": "read n lines from stdin and convert into specified type", | |
"body": [ | |
"fn reads_n<T: std::str::FromStr>(i: &mut StdinLock, n: u32) -> Vec<Vec<T>> {", | |
" let mut v2 = Vec::new();", | |
" for _ in 0..n {", | |
" let mut s = String::new();", | |
" i.by_ref().read_line(&mut s).ok();", | |
" let v = s.trim().split_whitespace().map(|e| e.parse().ok().unwrap()).collect();", | |
" v2.push(v);", | |
" }", | |
" v2", | |
"}", | |
"$1", | |
], | |
}, | |
"init stdin lock": { | |
"prefix": "stdin", | |
"description": "initialize stdin with lock (faster implementation)", | |
"body": [ | |
"// initialize stdin", | |
"let sin = std::io::stdin();", | |
"let mut sin = sin.lock();", | |
"let sin = &mut sin;", | |
], | |
}, | |
"get input values": { | |
"prefix": "input", | |
"description": "input from stdin", | |
"body": [ | |
"let input$1: $2 = read$3(${4:sin});", | |
"$5", | |
] | |
}, | |
"implement default": { | |
"prefix": "impl Default", | |
"description": "implement default for some type", | |
"body": [ | |
"impl Default for $1 {", | |
" fn default() -> Self {", | |
" $2", | |
" }", | |
"}", | |
"$3" | |
] | |
}, | |
"derive": { | |
"prefix": "derive", | |
"description": "derive snippet", | |
"body": "#[derive($1, Debug)]", | |
}, | |
"sortings": { | |
"prefix": "sort", | |
"description": "provide sorts", | |
"body": [ | |
"PartialEq, Eq, PartialOrd, Ord" | |
] | |
}, | |
"serde": { | |
"prefix": "serde", | |
"description": "provide serialize / deserialize", | |
"body": "Serialize, Deserialize" | |
}, | |
"serde default value": { | |
"prefix": "default value", | |
"description": "provide serde default value", | |
"body": "#[serde(default$1)]", | |
}, | |
"input!": { | |
"prefix": "input!", | |
"description": "input macro for competitive programming", | |
"body": [ | |
"macro_rules! input {", | |
" (source = $$s:expr, $($$r:tt)*) => {", | |
" let mut iter = $$s.split_whitespace();", | |
" input_inner!{iter, $($$r)*}", | |
" };", | |
" ($($$r:tt)*) => {", | |
" let s = {", | |
" use std::io::Read;", | |
" let mut s = String::new();", | |
" std::io::stdin().read_to_string(&mut s).unwrap();", | |
" s", | |
" };", | |
" let mut iter = s.split_whitespace();", | |
" input_inner!{iter, $($$r)*}", | |
" };", | |
"}", | |
"", | |
"macro_rules! input_inner {", | |
" ($$iter:expr) => {};", | |
" ($$iter:expr, ) => {};", | |
"", | |
" ($$iter:expr, $$var:ident : $$t:tt $($$r:tt)*) => {", | |
" let $$var = read_value!($$iter, $$t);", | |
" input_inner!{$$iter $($$r)*}", | |
" };", | |
"}", | |
"", | |
"macro_rules! read_value {", | |
" ($$iter:expr, ( $($$t:tt),* )) => {", | |
" ( $(read_value!($$iter, $$t)),* )", | |
" };", | |
"", | |
" ($$iter:expr, [ $$t:tt ; $$len:expr ]) => {", | |
" (0..$$len).map(|_| read_value!($$iter, $$t)).collect::<Vec<_>>()", | |
" };", | |
"", | |
" ($$iter:expr, chars) => {", | |
" read_value!($$iter, String).chars().collect::<Vec<char>>()", | |
" };", | |
"", | |
" ($$iter:expr, usize1) => {", | |
" read_value!($$iter, usize) - 1", | |
" };", | |
"", | |
" ($$iter:expr, $$t:ty) => {", | |
" $$iter.next().unwrap().parse::<$$t>().expect(\"Parse error\")", | |
" };", | |
"}", | |
"", | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice