引数パーザ(clap)をインスタンス化しようとしています。私は似た既存の質問を通して見て、連結を発見リテラル文字列をconst文字列に連結する方法は?
const DEFAULT_VALUE: &'static str = "12312312";
// ...
.help("this parameter is for (default:" + DEFAULT_VALUE + ")")
// ...
: コードは次のようにあります!マクロとlazy_staticです。
最初のオプションは適合せず、lazy_staticの例はありません。 可能であれば、lazy_staticはブロックを別の場所に定義する必要があるため、とにかく複雑になります。
私はいくつかの簡潔な構文の砂糖を探しています。
ローカル変数を定義すると、クラップスのDSLが非常に長くなる可能性があるため、ローカル変数を高く設定する可能性があります。それはコード内の論理的な場所から文字列を取り除くので便利ではありません。
ヘルプ文字列全体の静的変数を定義する別の方法ですが、上記のアプローチと同じ欠点に加えて名前空間の汚染もあります。
フォーマットの推奨されるソリューション!あまりにもフィットしません。ローカル変数を定義する必要があります。
例
extern crate clap;
use clap::{Arg, App};
const DEFAULT: &'static str = "1";
fn main() {
let params = App::new("app")
.arg(Arg::with_name("p")
// here 100 lines of the uninterruptable expression
.help(&format!("parameter p (DEFAULT: {})", DEFAULT)))
// here also 100 lines of the uninterruptable expression
.get_matches();
println!("param p = {}", params.value_of("p").unwrap())
}
Cargo.toml
[package]
name = "demo-clap"
version = "1.0.0"
[dependencies]
clap = "2.10.0"
あなたは、単に参照およびマクロformat!
を使用することができ
<std macros>:2:1: 2:61 error: borrowed value does not live long enough
<std macros>:2 $ crate :: fmt :: format (format_args ! ($ ($ arg) *)))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:11:21: 11:66 note: in this expansion of format! (defined in <std macros>)
src/main.rs:13:24: 15:2 note: reference must be valid for the block suffix following statement 0 at 13:23...
src/main.rs:13 .get_matches();
^
src/main.rs:8:5: 13:24 note: ...but borrowed value is only valid for the statement at 8:4
src/main.rs:8 let params = App::new("app")
^
src/main.rs:8:5: 13:24 help: consider using a `let` binding to increase its lifetime
src/main.rs:8 let params = App::new("app")
^
error: aborting due to previous error
error: Could not compile `demo-clap`.
あなたのタイトルの「連絡先」は...「連動」を意味しましたか? –