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

macro bin cargo-snippet

A snippet extractor for competitive programmers

20 releases (5 breaking)

✓ Uses Rust 2018 edition

new 0.6.1 Mar 25, 2020
0.5.1 Mar 19, 2020
0.3.1 Nov 7, 2019
0.2.8 May 15, 2019
0.2.0 Mar 31, 2018

#23 in Template engine

Download history 48/week @ 2019-12-02 91/week @ 2019-12-09 4/week @ 2019-12-16 15/week @ 2019-12-23 5/week @ 2019-12-30 32/week @ 2020-01-06 93/week @ 2020-01-13 75/week @ 2020-01-20 3/week @ 2020-01-27 8/week @ 2020-02-03 27/week @ 2020-02-10 71/week @ 2020-02-17 99/week @ 2020-02-24 8/week @ 2020-03-02 37/week @ 2020-03-09 71/week @ 2020-03-16

208 downloads per month

MIT license

47KB
1.5K SLoC

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 to install rustfmt to run cargo-snippet.

$ rustup component add rustfmt

Install cargo-snippet

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

Usage

Create a project for snippet.

$ cargo new mysnippet

Add dependencies to Cargo.toml.

[dependencies]
cargo-snippet = "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 cargo_snippet::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))
}

// You can set prefix string.
// Note: All codes will be formatted by rustfmt on output
#[snippet(prefix = "use std::io::{self,Read};")]
#[snippet(prefix = "use std::str::FromStr;")]
fn foo() {}

#[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 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.

Dependencies

~0–2.6MB
~55K SLoC