#subcommand #cargo-subcommand #cargo #competitive #snippet

nightly bin+lib cargo-snippet

A snippet extractor for competitive programmers

13 releases

✓ Uses Rust 2018 edition

new 0.3.0 Aug 14, 2019
0.2.8 May 15, 2019
0.2.7 Feb 23, 2019
0.2.6 Dec 14, 2018
0.2.0 Mar 31, 2018

#20 in Template engine

Download history 15/week @ 2019-04-29 13/week @ 2019-05-06 26/week @ 2019-05-13 46/week @ 2019-05-20 17/week @ 2019-05-27 22/week @ 2019-06-03 38/week @ 2019-06-10 36/week @ 2019-06-17 173/week @ 2019-06-24 85/week @ 2019-07-01 63/week @ 2019-07-08 2/week @ 2019-07-15 14/week @ 2019-07-22 16/week @ 2019-07-29 25/week @ 2019-08-05

179 downloads per month

MIT license

31KB
860 lines

cargo-snippet

crates.io Build Status dependency status

A snippet extractor for competitive programmers.

You can manage code snippet with test and bench !!

Installing

You need nightly rust.

$ cargo install cargo-snippet --features="binaries"

Usage

Create a project for snippet.

$ cargo new mysnippet

Add dependencies to Cargo.toml.

[dependencies]
cargo-snippet = "0.2"

Add this to src/lib.rs.

#![feature(plugin)]
#![plugin(cargo_snippet)]

Write some snippet codes and tests.


#![feature(plugin)]
#![plugin(cargo_snippet)]

// Annotate snippet name
#[snippet = "mymath"]
#[snippet = "gcd"]
fn gcd(a: u64, b: u64) -> u64 {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}

// Also works
#[snippet(name = "mymath")]
// Equivalent to #[snippet = "lcm"]
#[snippet]
fn lcm(a: u64, b: u64) -> u64 {
    a / gcd(a, b) * b
}

#[snippet]
// Include snippet
#[snippet(include = "gcd")]
fn gcd_list(list: &[u64]) -> u64 {
    list.iter().fold(list[0], |a, b| gcd(a, b));
}

#[test]
fn test_gcd() {
    assert_eq!(gcd(57, 3), 3);
}

#[test]
fn test_lcm() {
    assert_eq!(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 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.

Dependencies

~3MB
~55K SLoC