#snippets #competitive #cargo #cargo-subcommand #subcommand

二进制 cargo-snippet

竞技程序员的代码片段提取器

24个不稳定版本 (5个破坏性版本)

0.6.5 2020年7月18日
0.6.3 2020年4月25日
0.6.1 2020年3月25日
0.3.1 2019年11月7日
0.2.0 2018年3月31日

#420模板引擎

Download history 72/week @ 2024-03-11 43/week @ 2024-03-18 24/week @ 2024-03-25 102/week @ 2024-04-01 27/week @ 2024-04-08 31/week @ 2024-04-15 38/week @ 2024-04-22 41/week @ 2024-04-29 35/week @ 2024-05-06 34/week @ 2024-05-13 37/week @ 2024-05-20 20/week @ 2024-05-27 47/week @ 2024-06-03 26/week @ 2024-06-10 30/week @ 2024-06-17 41/week @ 2024-06-24

每月147次 下载
2 crate 中使用

MIT 协议

55KB
1.5K SLoC

cargo-snippet

crates.io Build Status dependency status

竞技程序员的代码片段提取器。

你可以管理和测试代码片段!

安装

要运行rustfmt,您需要安装cargo-snippet

$ rustup component add rustfmt

安装cargo-snippet

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

使用方法

为片段创建一个项目。

$ cargo new mysnippet

将依赖项添加到Cargo.toml中。

[dependencies]
cargo-snippet = "0.5"

将此添加到src/lib.rs中。

注意:cargo-snippet在依赖项中只需要注册#[snippet]属性以防止编译器错误。所有提取片段的逻辑都在通过“安装”部分安装的二进制包中。

编写一些代码片段和测试。

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() {}

// By default, doc comments associated with items will be output with the snippet.
#[snippet]
/// This is a document!
fn documented() {
    //! Inner document also works.
}

// If you want it to be hidden, append `doc_hidden` keyword.
#[snippet(doc_hidden, prefix = "use std::collections::HashMap;")]
/// This is a doc comment for `bar`.
/// Since `doc_hidden` is specified, it won't be present in the snippet.
fn bar() {
    //! And this is also a doc comment for `bar`, which will be removed.
}

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

#[test]
fn test_lcm() {
    assert_eq!(lcm(3, 19), 57);
}

您可以测试。

$ cargo test

提取片段!

$ cargo snippet
snippet foo
    use std::io::{self, Read};
    use std::str::FromStr;
    fn foo() {}

snippet documented
    /// This is a document!
    fn documented() {
        //! Inner document also works.
    }

snippet bar
    use std::collections::HashMap;
    fn bar() {}

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
    }

示例

我的片段在这里

支持输出格式

  • Neosnippet
  • VScode
  • Ultisnips

您可以通过-t选项指定输出格式。请参阅cargo snippet -h

依赖项

~0–11MB
~110K SLoC