CodeSampleX

示例

clap 4.6.7: clap::FromArgMatches

已验证示例 — cargo clap 4.6.7: clap::FromArgMatches. contract 在 rust 1 · linux alpine/x64 · docker 上运行并通过: clap::FromArgMatches::from_arg_matches constructs a…

sha256:ab3e406857a5f0c4e6a091a8a8e10636276bc8ac3c55970fb33bdae1da1e0879

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux x64 cargo

验证运行环境

环境 契约 阶段 运行日期
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-10-22

案例

HOW
目标
verify clap::FromArgMatches in pkg:cargo/clap@4.6.7
包
符号
  • clap::FromArgMatches
创建时间
2026-10-22T14:31:27Z

契约

  1. clap::FromArgMatches::from_arg_matches constructs a typed struct from parsed clap::ArgMatches
  2. clap::FromArgMatches::update_from_arg_matches updates an existing struct instance in-place from new clap::ArgMatches

文件

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/main.rs

下载源代码构件 (tar.gz)

源代码

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "anstream"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
dependencies = [
 "anstyle",
 "anstyle-parse",
 "anstyle-query",
 "anstyle-wincon",
 "colorchoice",
 "is_terminal_polyfill",
 "utf8parse",
]

[[package]]
name = "anstyle"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"

[[package]]
name = "anstyle-parse"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
dependencies = [
 "utf8parse",
]

[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
 "windows-sys",
]

[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
 "anstyle",
 "once_cell_polyfill",
 "windows-sys",
]

[[package]]
name = "clap"
version = "4.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946"
dependencies = [
 "clap_builder",
]

[[package]]
name = "clap_builder"
version = "4.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d"
dependencies = [
 "anstream",
 "anstyle",
 "clap_lex",
 "strsim",
]

[[package]]
name = "clap_lex"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486"

[[package]]
name = "colorchoice"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"

[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"

[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"

[[package]]
name = "sample"
version = "0.1.0"
dependencies = [
 "clap",
]

[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"

[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"

[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"

[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
 "windows-link",
]
Cargo.toml
[package]
name = "sample"
version = "0.1.0"
edition = "2021"

[dependencies]
clap = "=4.6.7"
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify clap::FromArgMatches in pkg:cargo/clap@4.6.7
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/clap@4.6.7
Demonstrate these symbols/APIs:
  - clap::FromArgMatches

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:820bea942f9af2e1d05c4f40d3f0ba251ed603afbd2584d5f769f89d06239543","contract":["clap::FromArgMatches::from_arg_matches constructs a typed struct from parsed clap::ArgMatches","clap::FromArgMatches::update_from_arg_matches updates an existing struct instance in-place from new clap::ArgMatches"],"goal":"verify clap::FromArgMatches in pkg:cargo/clap@4.6.7","kind":"HOW","packages":["pkg:cargo/clap@4.6.7"],"schemaVersion":1,"symbols":["clap::FromArgMatches"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/clap@4.6.7"],"schemaVersion":1,"subject":"pkg:cargo/clap@4.6.7","symbols":["clap::FromArgMatches"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify clap::FromArgMatches in pkg:cargo/clap@4.6.7",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/clap@4.6.7"
  ],
  "symbols": [
    "clap::FromArgMatches"
  ]
}
src/main.rs
use clap::{Arg, ArgMatches, Command, Error, FromArgMatches};

#[derive(Debug, PartialEq, Eq)]
struct Config {
    mode: String,
    count: u32,
}

impl FromArgMatches for Config {
    fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error> {
        let mut matches = matches.clone();
        Self::from_arg_matches_mut(&mut matches)
    }

    fn from_arg_matches_mut(matches: &mut ArgMatches) -> Result<Self, Error> {
        let mode = matches
            .remove_one::<String>("mode")
            .unwrap_or_else(|| "default".to_string());
        let count = matches.remove_one::<u32>("count").unwrap_or(1);
        Ok(Self { mode, count })
    }

    fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error> {
        let mut matches = matches.clone();
        self.update_from_arg_matches_mut(&mut matches)
    }

    fn update_from_arg_matches_mut(&mut self, matches: &mut ArgMatches) -> Result<(), Error> {
        if let Some(mode) = matches.remove_one::<String>("mode") {
            self.mode = mode;
        }
        if let Some(count) = matches.remove_one::<u32>("count") {
            self.count = count;
        }
        Ok(())
    }
}

fn build_cli() -> Command {
    Command::new("app")
        .arg(Arg::new("mode").long("mode"))
        .arg(
            Arg::new("count")
                .long("count")
                .value_parser(clap::value_parser!(u32)),
        )
}

fn main() {
    let matches = build_cli()
        .try_get_matches_from(["app", "--mode", "fast", "--count", "4"])
        .expect("valid arguments should parse");

    let mut cfg =
        <Config as FromArgMatches>::from_arg_matches(&matches).expect("from_arg_matches succeeds");
    assert_eq!(
        cfg,
        Config {
            mode: "fast".to_string(),
            count: 4
        }
    );

    let update_matches = build_cli()
        .try_get_matches_from(["app", "--count", "9"])
        .expect("valid update arguments should parse");

    <Config as FromArgMatches>::update_from_arg_matches(&mut cfg, &update_matches)
        .expect("update_from_arg_matches succeeds");
    assert_eq!(
        cfg,
        Config {
            mode: "fast".to_string(),
            count: 9
        }
    );
}

原始种子者

匿名