A snippet extractor for competitive programmers.
You can manage code snippet with test and bench !!
You need nightly rust.
$ cargo install cargo-snippet --features="binaries"
Create a project for snippet.
$ cargo new mysnippet
Add dependencies to Cargo.toml.
toml
[dependencies]
cargo-snippet = "0.2"
Add this to src/lib.rs.
```rust
```
Write some snippet codes and tests.
```rust
// Annotate snippet name
fn gcd(a: u64, b: u64) -> u64 { if b == 0 { a } else { gcd(b, a % b) } }
// Also works
// Equivalent to #[snippet = "lcm"]
fn lcm(a: u64, b: u64) -> u64 { a / gcd(a, b) * b }
// Include snippet
fn gcd_list(list: &[u64]) -> u64 { list.iter().fold(list[0], |a, b| gcd(a, b)); }
fn testgcd() { asserteq!(gcd(57, 3), 3); }
fn testlcm() { asserteq!(lcm(3, 19), 57); }
```
You can test.
$ cargo test
Extract snippet !
``` $ cargo snippet snippet gcd fn gcd(a: u64, b: u64) -> u64 { if b == 0 { a } else { gcd(b, a % b) } }
snippet gcdlist fn gcd(a: u64, b: u64) -> u64 { if b == 0 { a } else { gcd(b, a % b) } } fn gcdlist(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 }
```
My snippets here.
You can specify output format via -t
option.
See cargo snippet -h
.