在 Rust 中重用变量

Reusing variables in Rust

我有以下代码:

let display_value = entry.path().display();
files_and_dirs.push(DiskEntry {
    path: display_value.to_string(),
    is_dir: is_dir(display_value.to_string()),
    name: display_value.to_string()
});

如果我这样写:

let display_value = entry.path().display();
let dir_name = display_value.to_string();
files_and_dirs.push(DiskEntry {
    path: dir_name,
    is_dir: is_dir(dir_name),
    name: dir_name
});

我收到以下错误:

move occurs because dir_name has type std::string::String, which does not implement the Copy trait

我了解到 Rust 中的值在分配时会四处移动。我想声明一个变量并在第二个代码块中多次使用它。我该怎么做?

I want to declare one variable and use it multiple times in the second block of code. How do I do this?

你不需要,因为 "in Rust values are moved around when assigning"。编译器不会自动复制大多数类型的值,因为不清楚何时可能,并且它会引入程序员可能希望避免的开销。

如果您想多次使用一个 Clone 变量的值 - 只需克隆它即可。

如果您想避免重复自己,可以使用闭包为计算命名。

let display_value = entry.path().display();
let dir_name = || display_value.to_string();
files_and_dirs.push(DiskEntry {
    path: dir_name(),
    is_dir: is_dir(dir_name()),
    name: dir_name()
});

这不会让您多次使用该值 - 每次调用都会产生一个新值。

你的DiskEntryis_dir大概是这样定义的:

struct DiskEntry {
    path: String,
    is_dir: bool,
    name: String,
}

fn is_dir(path: String) -> bool {
    // ...
}

每次出现String类型的变量时,变量都需要在内存中有自己的字符串副本。

如错误所示,您可以通过克隆来修复它:

let display_value = entry.path().display();
let dir_name = display_value.to_string();
files_and_dirs.push(DiskEntry {
    path: dir_name.clone(),
    is_dir: is_dir(dir_name.clone()),
    name: dir_name
});

但是您应该避免不必要地克隆数据,因为它效率低下。非常容易删除的克隆在 is_dir 中 - 很明显,此方法不需要永久获取其输入的所有权。它可以很容易地借用它:

fn is_dir(path: &str) -> bool {
    // ...
}

你会这样称呼它,所以 String 被借用为 &str:

is_dir(&dir_name),

另一种情况可能更棘手。为了避免克隆,您需要使 DiskEntry 结构借用字符串。它看起来像这样:

struct DiskEntry<'a> {
    path: &'a str,
    is_dir: bool,
    name: &'a str,
}

let display_value = entry.path().display();
let dir_name = display_value.to_string();
files_and_dirs.push(DiskEntry {
    path: &dir_name,
    is_dir: is_dir(&dir_name),
    name: &dir_name
});

但是,这将限制您可以使用 DiskEntry;特别是,它不能超过 dir_name 变量。在不了解代码的更广泛上下文的情况下,很难知道这可能会给您带来什么其他问题,但它可能会对其他数据结构产生更广泛的影响。

鉴于您似乎只是在学习 Rust,如果您对借用和生命周期还不熟悉,那么接受这个额外的克隆可能是最简单的,直到您有更好的理解并可以自己做出判断.


另请参阅:

  • What are the differences between Rust's String and str?