Skip to content

Instantly share code, notes, and snippets.

@ksoda
Last active November 3, 2021 12:32
Show Gist options
  • Save ksoda/426f38e0454379a00add2dab7aeb658b to your computer and use it in GitHub Desktop.
Save ksoda/426f38e0454379a00add2dab7aeb658b to your computer and use it in GitHub Desktop.
VS Code snippet
#[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()
}
{
// 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."
}
}
#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
#[test]
fn parse() {
assert_eq!(1, 1);
}
}
@ksoda
Copy link
Author

ksoda commented Aug 17, 2019

@ksoda
Copy link
Author

ksoda commented Nov 3, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment