【发布时间】:2021-06-01 13:51:41
【问题描述】:
这是我的 Cargo.toml。
[package]
name = "test"
version = "0.1.0"
authors = ["test <test@gmail.com>"]
edition = "2018"
[dependencies]
rand = "0.8.3"
walkdir = "2.3.1"
这是我的 main.rs。
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!!!");
}
}
}
此代码可以毫无问题地运行。但是,当我修改如下代码时:
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!!!");
}
}
}
我收到错误“移动后在此处使用的值”,这意味着第二个if 中的&entry.unwrap() 正在使用移动值。我的想法是先克隆entry,然后是if,然后是unwrap。但是上面没有clone() 方法。我怎样才能使这段代码工作?
【问题讨论】:
标签: rust clone ownership borrowing