特性 `std::convert::From<cli::Opts>` 未实现
The trait `std::convert::From<cli::Opts>` is not implemented
我尝试创建一个简单的应用程序,使用 clap 库解析命令行参数并将它们转换为 Config
自定义结构。我为我的结构实现了 From
特性,但是,当我尝试调用 from
函数时,我收到以下错误:
the trait bound `minimal_example::Config: std::convert::From<cli::Opts>` is not satisfied
the following implementations were found:
<minimal_example::Config as std::convert::From<minimal_example::cli::Opts>>
required by `std::convert::From::from`
代码如下:
main.rs:
mod cli;
use clap::Clap;
use minimal_example::Config;
fn main() {
println!("Hello, world!");
let opts = cli::Opts::parse();
let config = Config::from(opts);
}
cli.rs:
use clap::{Clap, crate_version};
/// This doc string acts as a help message when the user runs '--help'
/// as do all doc strings on fields
#[derive(Clap)]
#[clap(version = crate_version!(), author = "Yury")]
pub struct Opts {
/// Simple option
pub opt: String,
}
lib.rs:
mod cli;
pub struct Config {
pub opt: String,
}
impl From<cli::Opts> for Config {
fn from(opts: cli::Opts) -> Self {
Config {
opt: opts.opt,
}
}
}
cargo.toml:
[package]
name = "minimal_example"
version = "0.1.0"
authors = ["Yury"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = {version="3.0.0-beta.2", features=["wrap_help"]}
我做错了什么?
您已将 mod cli
添加到 lib.rs
和 main.rs
。
他们的立场不一样
Rust modules confusion when there is main.rs and lib.rs
可能有助于理解这一点。
错误就是这么说的。 std::convert::From<minimal_example::cli::Opts>
满意,但 std::convert::From<cli::Opts>
.
不满意
一个简单的修复:
main.rs
mod cli;
use clap::Clap;
use minimal_example::Config;
impl From<cli::Opts> for Config {
fn from(opts: cli::Opts) -> Self {
Config {
opt: opts.opt,
}
}
}
fn main() {
println!("Hello, world!");
let opts = cli::Opts::parse();
let config = Config::from(opts);
}
现在 std::convert::From<cli::Opts>
已为 Config
实施。
您实际想要放置所有这些的方式取决于您的包架构。
我尝试创建一个简单的应用程序,使用 clap 库解析命令行参数并将它们转换为 Config
自定义结构。我为我的结构实现了 From
特性,但是,当我尝试调用 from
函数时,我收到以下错误:
the trait bound `minimal_example::Config: std::convert::From<cli::Opts>` is not satisfied
the following implementations were found:
<minimal_example::Config as std::convert::From<minimal_example::cli::Opts>>
required by `std::convert::From::from`
代码如下:
main.rs:
mod cli;
use clap::Clap;
use minimal_example::Config;
fn main() {
println!("Hello, world!");
let opts = cli::Opts::parse();
let config = Config::from(opts);
}
cli.rs:
use clap::{Clap, crate_version};
/// This doc string acts as a help message when the user runs '--help'
/// as do all doc strings on fields
#[derive(Clap)]
#[clap(version = crate_version!(), author = "Yury")]
pub struct Opts {
/// Simple option
pub opt: String,
}
lib.rs:
mod cli;
pub struct Config {
pub opt: String,
}
impl From<cli::Opts> for Config {
fn from(opts: cli::Opts) -> Self {
Config {
opt: opts.opt,
}
}
}
cargo.toml:
[package]
name = "minimal_example"
version = "0.1.0"
authors = ["Yury"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = {version="3.0.0-beta.2", features=["wrap_help"]}
我做错了什么?
您已将 mod cli
添加到 lib.rs
和 main.rs
。
他们的立场不一样
Rust modules confusion when there is main.rs and lib.rs 可能有助于理解这一点。
错误就是这么说的。 std::convert::From<minimal_example::cli::Opts>
满意,但 std::convert::From<cli::Opts>
.
一个简单的修复:
main.rs
mod cli;
use clap::Clap;
use minimal_example::Config;
impl From<cli::Opts> for Config {
fn from(opts: cli::Opts) -> Self {
Config {
opt: opts.opt,
}
}
}
fn main() {
println!("Hello, world!");
let opts = cli::Opts::parse();
let config = Config::from(opts);
}
现在 std::convert::From<cli::Opts>
已为 Config
实施。
您实际想要放置所有这些的方式取决于您的包架构。