VSCode で Rust で競技プログラミングをするときの設定

拡張機能と RLS を入れる

task の登録

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cargo run",
            "type": "shell",
            "command": "RUST_BACKTRACE=1",
            "args": [
                "cargo",
                "run",
                "--bin",
                "${fileBasenameNoExtension}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

スニペットの登録

{
	// 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"
	// }
	"Scanner and main": {
		"prefix": "scanner",
		"body": [
			"fn main() {",
			"    let s = std::io::stdin();",
			"    let mut sc = Scanner { stdin: s.lock() };",
			"}",
			"",
			"pub struct Scanner<R> {",
			"    stdin: R,",
			"}",
			"",
			"impl<R: std::io::Read> Scanner<R> {",
			"    pub fn read<T: std::str::FromStr>(&mut self) -> T {",
			"        use std::io::Read;",
			"        let buf = self",
			"            .stdin",
			"            .by_ref()",
			"            .bytes()",
			"            .map(|b| b.unwrap())",
			"            .skip_while(|&b| b == b' ' || b == b'\\n' || b == b'\\r')",
			"            .take_while(|&b| b != b' ' && b != b'\\n' && b != b'\\r')",
			"            .collect::<Vec<_>>();",
			"        unsafe { std::str::from_utf8_unchecked(&buf) }",
			"            .parse()",
			"            .ok()",
			"            .expect(\"Parse error.\")",
			"    }",
			"    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {",
			"        (0..n).map(|_| self.read()).collect()",
			"    }",
			"    pub fn chars(&mut self) -> Vec<char> {",
			"        self.read::<String>().chars().collect()",
			"    }",
			"}"
		]
	}
}