Rust “如何修复生锈错误”;“移动”后此处使用的值;?
这是我的货物Rust “如何修复生锈错误”;“移动”后此处使用的值;?,rust,clone,ownership,borrowing,Rust,Clone,Ownership,Borrowing,这是我的货物 [package] name = "test" version = "0.1.0" authors = ["test <test@gmail.com>"] edition = "2018" [dependencies] rand = "0.8.3" walkdir = "2.3.1" 这段代码可以毫无问题地运行。但是,当我修改如下代码时: use
[package]
name = "test"
version = "0.1.0"
authors = ["test <test@gmail.com>"]
edition = "2018"
[dependencies]
rand = "0.8.3"
walkdir = "2.3.1"
这段代码可以毫无问题地运行。但是,当我修改如下代码时:
use std::fs;
use std::io::Error;
use std::path::Path;
use walkdir::WalkDir;
fn main() {
for entry in WalkDir::new(".").max_depth(1) {
if &entry.unwrap().path().strip_prefix(".\\").unwrap().to_str().unwrap() == &"src" {
println!("{:#?}", "Yes, it's src!!!");
}
if &entry.unwrap().path().strip_prefix(".\\").unwrap().to_str().unwrap() == &"target" {
println!("{:#?}", "Yes, it's target!!!");
}
}
}
我得到了错误“移动后此处使用的值”,这意味着第二个
中的&entry.unwrap()。我的想法是在第一个中克隆条目
,如果
,然后展开
。但是它上面没有clone()
方法。如何使此代码正常工作?Result::unwrap
使用结果对象,将条目从结果移动到返回值中
展开一次并将结果对象存储在变量中(let entry=entry.unwrap();
),或者使用Result::as_ref
借用Result
中的对象(entry.as_ref().unwrap().path()…
)Result::unwrap
使用结果对象,将条目移出结果
并移到返回值中
展开一次并将结果对象存储在变量中(let entry=entry.unwrap();
),或者使用Result::as_ref
借用结果中的对象(entry.as_ref().unwrap().path()…
)
use std::fs;
use std::io::Error;
use std::path::Path;
use walkdir::WalkDir;
fn main() {
for entry in WalkDir::new(".").max_depth(1) {
if &entry.unwrap().path().strip_prefix(".\\").unwrap().to_str().unwrap() == &"src" {
println!("{:#?}", "Yes, it's src!!!");
}
if &entry.unwrap().path().strip_prefix(".\\").unwrap().to_str().unwrap() == &"target" {
println!("{:#?}", "Yes, it's target!!!");
}
}
}