- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用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`
这是代码:
mod cli;
use clap::Clap;
use minimal_example::Config;
fn main() {
println!("Hello, world!");
let opts = cli::Opts::parse();
let config = Config::from(opts);
}
客户:
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
。
关于rust - 特质 `std::convert::From<cli::Opts>`未实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64963625/
我浏览了该网站和其他网站,找不到关于这种特质的任何解释,也找不到任何解决方案。 尽管在这些站点上有各种说法,但是Javascript和HTML5 DOM音频似乎无法在所有浏览器上正常工作,正如在各个站
beginGeneratingDeviceOrientationNotifications是否将状态栏重置回纵向? 在我的应用程序中,在下面记录 o 和 p: UIInterfaceOrientati
我开始意识到这是为初学者准备的: package Bad; has 'arr' => ( is => 'rw', 'ArrayRef[Str]' ); package main; my $bad =
只是想知道如何将的特征与放入vec中?我以为这应该是一个常见的问题,但我从未搜索过答案。 这是代码: use tokio::time::{delay_for, Duration}; #[async_t
更新 我的真正问题是由我的IDE自动导入了use std::borrow::{Borrow, BorrowMut};引起的。 在此行中,接受的答案为also doesn't compile。 解决方案
我正在尝试为 P/Invoke 正确编码(marshal)一些结构,但在 64 位操作系统上测试时发现奇怪的行为。 我有一个结构定义为: /// http://msdn.microsoft.com/e
说我有: abstract class D[T] {} trait A[T] { self => D[T] without B } trait B[T] { self => D[T] without
我尝试使用clap库创建一个简单的应用程序来解析命令行参数,并将其转换为Config自定义结构。我为我的结构实现了From特征,但是,当我尝试调用from函数时,收到以下错误: the trait b
在 Laravel 5.0 中,trait AuthenticatesAndRegistersUsers 中的方法 redirectPath 检查属性 redirectPath 或 redirectT
虽然这个问题有点与语言无关(就支持 Traits 的 OOP 语言而言)我一直在修补 PHP 5.4a 的夜间构建,并遇到了一个奇怪的场景。我似乎无法再运行我的安装,但那是另一回事了。 给定以下代码段
我是一名优秀的程序员,十分优秀!