Compiler errors 无法克隆io::1.0.0测试版中的错误

Compiler errors 无法克隆io::1.0.0测试版中的错误,compiler-errors,rust,Compiler Errors,Rust,我刚开始学习Rust,但自从升级到beta版后,我面临许多以前没有的编译错误。 其中一个与克隆相关,以下是我的代码: use std::io::{BufReader, BufRead}; use std::clone::Clone; use std::env; use std::fs::File; pub fn main() { let ref path = match env::args().nth(1) { Some(path) => path,

我刚开始学习Rust,但自从升级到beta版后,我面临许多以前没有的编译错误。 其中一个与克隆相关,以下是我的代码:

use std::io::{BufReader, BufRead};
use std::clone::Clone;
use std::env;
use std::fs::File;

pub fn main() {

    let ref path = match env::args().nth(1) {
            Some(path) => path,
            None => panic!("file path is missing!"), };

    let file = match File::open(&path) {
            Ok(file) => file,
            Err(_) => panic!("could not open {}", path), };

    let mut iter = BufReader::new(file).lines();
    let mut opt = iter.next();

    let str = opt.clone().unwrap().unwrap();

    // some code omitted
}
这就是错误:

test.rs:19:19:19:26错误:type
core::option::option
未在名为
clone
test.rs:19让str=opt.clone().unwrap().unwrap()

我需要
克隆
,因为我在代码的其他部分使用了
opt


是我的代码出错了,还是我不知道的东西生锈了?

看来
std::io::Error
没有实现
Clone
,这就是问题所在。我看不出有什么真正的理由不能这样做,所以我认为这只是一个疏忽。也许值得提出一个问题来支持它

同时,我能想到的最简单的解决方法就是用你可以克隆的东西来替换
错误。最快的方法是将其转换为
字符串

let opt = opt.map(|r| r.map_err(|e| format!("{}", e)));

如果您想保留实际的
错误
值,可以尝试将其移动到
Rc
中,这样您就可以共享所有权。

错误消息似乎让您感到困惑,所以让我们来看看:

type `core::option::Option<core::result::Result<collections::string::String, std::io::error::Error>>` does not implement any method in scope named `clone`
我不确定
Clone+Clone
是怎么回事,但上面说“如果包含的类型实现
Clone
,我可以实现
Clone
。那么让我们看看包含的类型。它说的是类似的:

impl<T, E> Clone for Result<T, E> where E: Clone + Clone, T: Clone + Clone
这将克隆
字符串,而不是所有中间状态。由于克隆的数据较少,这可能会稍微更有效。

为了
.clone()
处理
结果
T
E
都必须实现
克隆
特性


在这种情况下,您将处理一个
结果
,但是
错误
没有实现
克隆

看起来克隆+克隆的事情是一个Rustdoc错误,复制也会发生。。
impl<T> Clone for Option<T> where T: Clone + Clone
impl<T, E> Clone for Result<T, E> where E: Clone + Clone, T: Clone + Clone
let str = opt.unwrap().unwrap().clone();