雑なメモ書き

気楽にいきます

プログラミングRust メモ (2) cargo

パッケージ作成

  • cargo new
  • パッケージを作成する
Create a new cargo package at <path>

USAGE:
    cargo new [OPTIONS] <path>

OPTIONS:
        --registry <REGISTRY>    Registry to use
        --vcs <VCS>              Initialize a new repository for the given version control system (git, hg, pijul, or
                                 fossil) or do not initialize any version control at all (none), overriding a global
                                 configuration. [possible values: git, hg, pijul, fossil, none]
        --bin                    Use a binary (application) template [default]
        --lib                    Use a library template
        --edition <YEAR>         Edition to set for the crate generated [possible values: 2015, 2018]
        --name <NAME>            Set the resulting package name, defaults to the directory name
    -v, --verbose                Use verbose output (-vv very verbose/build.rs output)
    -q, --quiet                  No output printed to stdout
        --color <WHEN>           Coloring: auto, always, never
        --frozen                 Require Cargo.lock and cache are up to date
        --locked                 Require Cargo.lock is up to date
    -Z <FLAG>...                 Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
    -h, --help                   Prints help information

ARGS:
    <path>

実行

  • 作成したディレクトリ以下でcargo runを行うと
  • main binaryを動作させる
Run the main binary of the local package (src/main.rs)

USAGE:
    cargo run [OPTIONS] [--] [args]...

OPTIONS:
        --bin <NAME>...             Name of the bin target to run
        --example <NAME>...         Name of the example target to run
    -p, --package <SPEC>            Package with the target to run
    -j, --jobs <N>                  Number of parallel jobs, defaults to # of CPUs
        --release                   Build artifacts in release mode, with optimizations
        --features <FEATURES>       Space-separated list of features to activate
        --all-features              Activate all available features
        --no-default-features       Do not activate the `default` feature
        --target <TRIPLE>           Build for the target triple
        --target-dir <DIRECTORY>    Directory for all generated artifacts
        --manifest-path <PATH>      Path to Cargo.toml
        --message-format <FMT>      Error format [default: human]  [possible values: human, json, short]
    -v, --verbose                   Use verbose output (-vv very verbose/build.rs output)
    -q, --quiet                     No output printed to stdout
        --color <WHEN>              Coloring: auto, always, never
        --frozen                    Require Cargo.lock and cache are up to date
        --locked                    Require Cargo.lock is up to date
    -Z <FLAG>...                    Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
    -h, --help                      Prints help information

ARGS:
    <args>...

If neither `--bin` nor `--example` are given, then if the package only has one
bin target it will be run. Otherwise `--bin` specifies the bin target to run,
and `--example` specifies the example target to run. At most one of `--bin` or
`--example` can be provided.

All the arguments following the two dashes (`--`) are passed to the binary to
run. If you're passing arguments to both Cargo and the binary, the ones after
`--` go to the binary, the ones before go to Cargo.

警告

プログラム中で使用しないものをuseに含める

use log::{info,warn};

fn main() {
    info!("trace");
    println!("Hello, world!");
}

実際に動作させると、必要なwarningsが丁寧に出力される 非常にありがたい

warning: unused import: `warn`
 --> src/main.rs:1:16
  |
1 | use log::{info,warn};
  |                ^^^^
  |
  = note: #[warn(unused_imports)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 1.96s
     Running `target/debug/hello`
Hello, world!

crates.io

rustには標準でcrates.ioというモジュールが登録されているサイトがある。

https://crates.io/

f:id:hiroyukim:20190127080356p:plain

このサイト非常に便利で、Most Downloadedとか表示されるので人気モジュールとか直ぐに分かる。

実際に各モジュールのページを開くと

https://crates.io/crates/log

f:id:hiroyukim:20190127080425p:plain

例えば、logを開くと、Cargo.tomlに簡単にコピーができるようになってる。 その上で、Usageがきちんと書かれているので

use log::{info, trace, warn};

pub fn shave_the_yak(yak: &mut Yak) {
    // …
}

いきなり訪れた人でも簡単に導入できる。