cargo-snippet
A snippet extractor for competitive programmers.
You can manage code snippet with test and bench !!
Installing
You need to install rustfmt
to run cargo-snippet
.
Install cargo-snippet
Usage
Create a project for snippet.
$ cargo new mysnippet
Add dependencies to Cargo.toml.
[]
= "0.5"
Add this to src/lib.rs.
Note: cargo-snippet
on dependencies is needed just for register #[snippet]
attribute to prevent error from the compiler.
All logic that extract snippet is in binary package which is installed by Installing
section.
Write some snippet codes and tests.
use snippet;
// Annotate snippet name
// Also works
// Equivalent to #[snippet("lcm")]
// Include snippet
// You can set prefix string.
// Note: All codes will be formatted by rustfmt on output
You can test.
$ cargo test
Extract snippet !
$ cargo snippet
snippet foo
use std::io::{self, Read};
use std::str::FromStr;
fn foo() {}
snippet gcd
fn gcd(a: u64, b: u64) -> u64 {
if b == 0 {
a
} else {
gcd(b, a % b)
}
}
snippet gcd_list
fn gcd(a: u64, b: u64) -> u64 {
if b == 0 {
a
} else {
gcd(b, a % b)
}
}
fn gcd_list(list: &[u64]) -> u64 {
list.iter().fold(list[0], |a, &b| gcd(a, b))
}
snippet lcm
fn lcm(a: u64, b: u64) -> u64 {
a / gcd(a, b) * b
}
snippet mymath
fn gcd(a: u64, b: u64) -> u64 {
if b == 0 {
a
} else {
gcd(b, a % b)
}
}
fn lcm(a: u64, b: u64) -> u64 {
a / gcd(a, b) * b
}
Example
My snippets here.
Supported output format
- Neosnippet
- VScode
- Ultisnips
You can specify output format via -t
option.
See cargo snippet -h
.