Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 错误:使用移动的值:`path`。如何更正此代码?_Rust - Fatal编程技术网

Rust 错误:使用移动的值:`path`。如何更正此代码?

Rust 错误:使用移动的值:`path`。如何更正此代码?,rust,Rust,编译代码时出现此错误:请帮助我 use of moved value: `path` value used here after moverustc(E0382) main.rs(16, 9): move occurs because `path` has type `std::result::Result`, which does not implement the `Copy` trait main.rs(18, 32): `path` moved due to this method c

编译代码时出现此错误:请帮助我

use of moved value: `path` value used here after moverustc(E0382) main.rs(16, 9): move occurs because `path` has type `std::result::Result`, which does not implement the `Copy` trait main.rs(18, 32): `path` moved due to this method call main.rs(19, 29): value used here after move

fn main() -> std::io::Result<()>  {
    let paths = fs::read_dir("./").unwrap();

    let mut text = String::new();
    let mut idx = 0;
    for path in paths {
        // let path_str = path.unwrap().path().display().to_string();
        let path_str = if path.unwrap().path().is_dir()  {
                            path.unwrap().path().display().to_string() 
                        } 
                       else {
                           let mut path = path.unwrap().path().display().to_string();
                           path.push_str("[file]");
                           path
                        };
        
        let path_trimed = path_str.trim_start_matches("./");
        idx += 1;
        println!("{} -> file/folder: {}", idx + 1, path_trimed);
        text.push_str(&path_trimed);
        text.push_str("\n");
    }
    // println!("{}", text);

    // writing the string to file
    let mut file = fs::File::create("file_list.txt")?;
    file.write_all(&text.as_bytes())?;
    Ok(())
}
移动值的使用:`path` 此处在moverustc(E0382)之后使用的值 rs(16,9):之所以发生移动,是因为'path'具有'std::result::result'类型,而该类型没有实现'Copy'特性 rs(18,32):`path`由于此方法调用而移动 main.rs(19,29):移动后此处使用的值

fn main()->std::io::Result{
let path=fs::read_dir(“./”).unwrap();
让mut text=String::new();
设mut idx=0;
对于路径中的路径{
//让path_str=path.unwrap().path().display().to_string();
让path_str=如果path.unwrap().path()是_dir(){
path.unwrap().path().display().to_string()
} 
否则{
让mut path=path.unwrap().path().display().to_string();
path.push_str(“[file]”);
路径
};
让path_trimed=path_str.trim_start_匹配(“./”);
idx+=1;
println!(“{}->文件/文件夹:{}”,idx+1,路径修剪);
text.push\u str(&path\u修剪);
text.push_str(“\n”);
}
//println!(“{}”,文本);
//将字符串写入文件
让mut file=fs::file::create(“file_list.txt”)?;
file.write_all(&text.as_bytes())?;
好(())
}

我认为问题在于您多次展开路径,每次展开都借用变量路径,因此当您尝试第二次展开时,您会抱怨。 我建议你试着把它拆开一次:

使用std::fs;
使用std::io::Write;
fn main()->std::io::Result{
let path=fs::read_dir(“./”).unwrap();
让mut text=String::new();
设mut idx=0;
对于路径中的路径{
//让path_str=path.unwrap().path().display().to_string();
让path=path.unwrap().path();
设path_str=if path.is_dir(){
path.display()到_字符串()
}否则{
让mut path=path.display().to_string();
path.push_str(“[file]”);
路径
};
让path_trimed=path_str.trim_start_匹配(“./”);
idx+=1;
println!(“{}->文件/文件夹:{}”,idx+1,路径修剪);
text.push\u str(&path\u修剪);
text.push_str(“\n”);
}
//println!(“{}”,文本);
//将字符串写入文件
让mut file=fs::file::create(“file_list.txt”)?;
file.write_all(&text.as_bytes())?;
好(())
}
use std::fs;
use std::io::Write;

fn main() -> std::io::Result<()> {
    let paths = fs::read_dir("./").unwrap();
    let mut text = String::new();
    let mut idx = 0;
    for path in paths {
        // let path_str = path.unwrap().path().display().to_string();
        let path = path.unwrap().path();
        let path_str = if path.is_dir() {
            path.display().to_string()
        } else {
            let mut path = path.display().to_string();
            path.push_str("[file]");
            path
        };
        let path_trimed = path_str.trim_start_matches("./");
        idx += 1;
        println!("{} -> file/folder: {}", idx + 1, path_trimed);
        text.push_str(&path_trimed);
        text.push_str("\n");
    }
    // println!("{}", text);
    // writing the string to file
    let mut file = fs::File::create("file_list.txt")?;
    file.write_all(&text.as_bytes())?;
    Ok(())
}