Last active
November 3, 2021 12:32
-
-
Save ksoda/426f38e0454379a00add2dab7aeb658b to your computer and use it in GitHub Desktop.
VS Code snippet
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
#[allow(dead_code)] | |
fn read<T: std::str::FromStr>() -> Option<T> { | |
let mut b = String::new(); | |
std::io::stdin().read_line(&mut b).ok(); | |
b.trim().parse().ok() | |
} | |
/// # Examples | |
/// ``` | |
/// let mtx = (0..2).map(|_| read_v().unwrap()).collect(); | |
/// ``` | |
#[allow(dead_code)] | |
fn read_v<T: std::str::FromStr>() -> Option<Vec<T>> { | |
read::<String>() | |
.unwrap() | |
.split_whitespace() | |
.map(|e| e.parse().ok()) | |
.collect() | |
} |
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. | |
"scan and solve": { | |
"prefix": "scanner", | |
"body": [ | |
"use std::io::stdin;", | |
"", | |
"#[allow(unused_macros)]", | |
"macro_rules! read {", | |
" ($(\\$t:ty), *) => {", | |
" {", | |
" let mut line: String = String::new();", | |
" stdin().read_line(&mut line).unwrap();", | |
" let mut iter = line.split_whitespace();", | |
" (", | |
" $(iter.next().unwrap().parse::<\\$t>().unwrap(),)*", | |
" )", | |
" }", | |
" };", | |
" (\\$t:ty;) => {", | |
" {", | |
" let mut line: String = String::new();", | |
" stdin().read_line(&mut line).unwrap();", | |
" let iter = line.split_whitespace();", | |
" iter", | |
" .map(|t| t.parse::<\\$t>().unwrap())", | |
" .collect::<Vec<_>>()", | |
" }", | |
" };", | |
"}", | |
"", | |
"fn main() {", | |
" let (n,) = read!(i32);", | |
" println!(\"{}\", solve(n));", | |
"}", | |
"", | |
"fn solve<T>(x: T) -> T {", | |
" x", | |
"}", | |
"", | |
"#[cfg(test)]", | |
"mod tests {", | |
" #[allow(unused_imports)]", | |
" use super::*;", | |
"", | |
" #[test]", | |
" fn it_works() {", | |
" assert_eq!(solve(42), 42);", | |
" }", | |
"}" | |
], | |
"description": "Read, Solve, Print, and Test." | |
} | |
} |
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
#[cfg(test)] | |
mod tests { | |
#[allow(unused_imports)] | |
use super::*; | |
#[test] | |
fn parse() { | |
assert_eq!(1, 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ref. https://qiita.com/hatoo@github/items/fa14ad36a1b568d14f3e