参考资料和借用


示例 4-5 中元组代码的问题在于,我们必须返回 String 添加到调用函数中,因此我们仍然可以在调用 calculate_length 后使用 String,因为 String 已移动到 calculate_length。相反,我们可以提供对 String 值的引用。引用就像一个指针,因为它是我们可以访问存储在该地址的数据的地址;该数据由其他变量拥有。与指针不同,引用保证在该引用的生命周期内指向特定类型的有效值。


以下是定义和使用calculate_length函数的方法,该函数将对对象的引用作为参数,而不是获取值的所有权:


文件名: src/main.rs
fn main() {
    let s1 = String::from("hello");

    let len = calculate_length(&s1);

    println!("The length of '{s1}' is {len}.");
}

fn calculate_length(s: &String) -> usize {
    s.len()
}


首先,请注意,变量声明中的所有元组代码和函数返回值都消失了。其次,请注意,我们将 &s1 传入 calculate_length,在其定义中,我们采用 &String 而不是 字符串。这些 & 符号表示引用,它们允许您引用某个值,而无需获得它的所有权。图 4-6 描述了这个概念。

Three tables: the table for s contains only a pointer to the table
for s1. The table for s1 contains the stack data for s1 and points to the
string data on the heap.


图 4-6:指向字符串 s1&String 关系


注意:使用&引用的反面是取消引用,这是通过取消引用运算符*完成的。我们将在第 8 章中看到 dereference 运算符的一些用法,并在第 15 章讨论取消引用的细节。


让我们仔细看看这里的函数调用:

fn main() {
    let s1 = String::from("hello");

    let len = calculate_length(&s1);

    println!("The length of '{s1}' is {len}.");
}

fn calculate_length(s: &String) -> usize {
    s.len()
}


&s1 语法允许我们创建一个引用 s1 值的引用 但不拥有它。因为它不拥有它,所以它指向的值将 当引用停止使用时,不会被删除。


同样,函数的签名使用&来表示参数s的类型是一个引用。让我们添加一些解释性注释:

fn main() {
    let s1 = String::from("hello");

    let len = calculate_length(&s1);

    println!("The length of '{s1}' is {len}.");
}

fn calculate_length(s: &String) -> usize { // s is a reference to a String
    s.len()
} // Here, s goes out of scope. But because it does not have ownership of what
  // it refers to, it is not dropped.


变量 s 有效的范围与任何函数参数的范围相同,但是当 s 停止使用时,引用指向的值不会被删除,因为 s 没有所有权。当函数将引用作为参数而不是实际值时,我们不需要返回值来归还所有权,因为我们从未拥有所有权。


我们将创建引用的作称为 借用。就像在现实生活中一样,如果一个人拥有某样东西,你可以从他们那里借来。当你完成时,你必须归还它。你不拥有它。


那么,如果我们试图修改我们借用的东西会发生什么呢?试试示例 4-6 中的代码。剧透警告:它不起作用!


文件名: src/main.rs
fn main() {
    let s = String::from("hello");

    change(&s);
}

fn change(some_string: &String) {
    some_string.push_str(", world");
}

示例 4-6:尝试修改借用的值


错误如下:

$ cargo run
   Compiling ownership v0.1.0 (file:///projects/ownership)
error[E0596]: cannot borrow `*some_string` as mutable, as it is behind a `&` reference
 --> src/main.rs:8:5
  |
8 |     some_string.push_str(", world");
  |     ^^^^^^^^^^^ `some_string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
  |
help: consider changing this to be a mutable reference
  |
7 | fn change(some_string: &mut String) {
  |                         +++

For more information about this error, try `rustc --explain E0596`.
error: could not compile `ownership` (bin "ownership") due to 1 previous error


正如变量默认是不可变的一样,引用也是如此。我们不允许修改我们引用的东西。


可变引用


我们可以修复示例 4-6 中的代码,允许我们通过一些小的调整来修改借用的值,而不是使用可变引用


文件名: src/main.rs
fn main() {
    let mut s = String::from("hello");

    change(&mut s);
}

fn change(some_string: &mut String) {
    some_string.push_str(", world");
}


首先,我们将 s 更改为 mut。然后,我们使用 &mut s 创建一个可变引用,在其中调用 change 函数,并更新函数签名以接受带有 some_string: &mut String 的可变引用。这非常清楚地表明 change 函数将改变它借用的值。


可变引用有一个很大的限制:如果你有一个对值的可变引用,则不能有对该值的其他引用。尝试创建两个对 s 的可变引用的代码将失败:


文件名: src/main.rs
fn main() {
    let mut s = String::from("hello");

    let r1 = &mut s;
    let r2 = &mut s;

    println!("{}, {}", r1, r2);
}


错误如下:

$ cargo run
   Compiling ownership v0.1.0 (file:///projects/ownership)
error[E0499]: cannot borrow `s` as mutable more than once at a time
 --> src/main.rs:5:14
  |
4 |     let r1 = &mut s;
  |              ------ first mutable borrow occurs here
5 |     let r2 = &mut s;
  |              ^^^^^^ second mutable borrow occurs here
6 |
7 |     println!("{}, {}", r1, r2);
  |                        -- first borrow later used here

For more information about this error, try `rustc --explain E0499`.
error: could not compile `ownership` (bin "ownership") due to 1 previous error


这个错误表示这段代码是无效的,因为我们不能一次多次借用 s 作为 mutable。第一个可变借用在 r1 中,并且必须持续到 println! 中使用它,但在创建该可变引用及其使用之间,我们尝试在 r2 中创建另一个可变引用,该引用借用与 r1 相同的数据。


阻止同时对同一数据的多个可变引用的限制允许更改,但以非常受控的方式进行。这是新 Rustacean 人挣扎的事情,因为大多数语言都允许你随时改变。具有此限制的好处是 Rust 可以防止编译时的数据竞争。数据争用类似于争用条件,当以下三种行为发生时发生:


  • 两个或多个指针同时访问相同的数据。

  • 至少有一个指针用于写入数据。

  • 没有使用任何机制来同步对数据的访问。


数据争用会导致未定义的行为,当您尝试在运行时跟踪它们时,可能难以诊断和修复;Rust 通过拒绝使用数据竞争编译代码来防止这个问题!


与往常一样,我们可以使用大括号来创建一个新的作用域,允许多个可变引用,但不能同时引用:

fn main() {
    let mut s = String::from("hello");

    {
        let r1 = &mut s;
    } // r1 goes out of scope here, so we can make a new reference with no problems.

    let r2 = &mut s;
}


Rust 对组合可变和不可变引用执行类似的规则。此代码会导致错误:

fn main() {
    let mut s = String::from("hello");

    let r1 = &s; // no problem
    let r2 = &s; // no problem
    let r3 = &mut s; // BIG PROBLEM

    println!("{}, {}, and {}", r1, r2, r3);
}


错误如下:

$ cargo run
   Compiling ownership v0.1.0 (file:///projects/ownership)
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
 --> src/main.rs:6:14
  |
4 |     let r1 = &s; // no problem
  |              -- immutable borrow occurs here
5 |     let r2 = &s; // no problem
6 |     let r3 = &mut s; // BIG PROBLEM
  |              ^^^^^^ mutable borrow occurs here
7 |
8 |     println!("{}, {}, and {}", r1, r2, r3);
  |                                -- immutable borrow later used here

For more information about this error, try `rustc --explain E0502`.
error: could not compile `ownership` (bin "ownership") due to 1 previous error


呼!我们也不能有一个可变的引用,而我们有一个对相同值的不可变引用。


不可变引用的用户不会期望该值突然从他们下面改变出来!但是,允许使用多个不可变引用,因为任何只是读取数据的人都无法影响其他人对数据的读取。


请注意,引用的范围从引入它的位置开始,一直持续到最后一次使用该引用。例如,这段代码将编译,因为不可变引用的最后一次使用 println! 发生在引入可变引用之前:

fn main() {
    let mut s = String::from("hello");

    let r1 = &s; // no problem
    let r2 = &s; // no problem
    println!("{r1} and {r2}");
    // variables r1 and r2 will not be used after this point

    let r3 = &mut s; // no problem
    println!("{r3}");
}


不可变引用 r1r2 的范围在 println! 它们最后一次使用的位置,即创建可变引用 R3 之前。这些范围不重叠,因此允许使用以下代码:编译器可以判断该引用在范围结束之前的某个点不再使用。


尽管借用错误有时可能令人沮丧,但请记住,这是 Rust 编译器尽早指出潜在的错误(在编译时而不是在运行时)并准确地告诉你问题所在。然后,您就不必追踪为什么您的数据不是您想象的那样。


悬空引用


在带有指针的语言中,很容易错误地创建一个 dangling pointer(指针)的指针,它引用内存中可能已提供给其他人的位置,方法是释放一些内存,同时保留指向该内存的指针。相比之下,在 Rust 中,编译器保证引用永远不会是悬空的引用:如果你有对某些数据的引用,编译器将确保数据不会在对数据的引用之前超出范围。


让我们尝试创建一个悬空引用,看看 Rust 如何通过编译时错误来防止它们:


文件名: src/main.rs
fn main() {
    let reference_to_nothing = dangle();
}

fn dangle() -> &String {
    let s = String::from("hello");

    &s
}


错误如下:

$ cargo run
   Compiling ownership v0.1.0 (file:///projects/ownership)
error[E0106]: missing lifetime specifier
 --> src/main.rs:5:16
  |
5 | fn dangle() -> &String {
  |                ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
  |
5 | fn dangle() -> &'static String {
  |                 +++++++
help: instead, you are more likely to want to return an owned value
  |
5 - fn dangle() -> &String {
5 + fn dangle() -> String {
  |

error[E0515]: cannot return reference to local variable `s`
 --> src/main.rs:8:5
  |
8 |     &s
  |     ^^ returns a reference to data owned by the current function

Some errors have detailed explanations: E0106, E0515.
For more information about an error, try `rustc --explain E0106`.
error: could not compile `ownership` (bin "ownership") due to 2 previous errors


此错误消息引用了我们尚未介绍的功能:lifetimes。我们将在第 10 章详细讨论生命周期。但是,如果你忽略有关生命周期的部分,该消息确实包含为什么此代码有问题的关键:

this function's return type contains a borrowed value, but there is no value
for it to be borrowed from


让我们仔细看看 吊坠代码:


文件名: src/main.rs
fn main() {
    let reference_to_nothing = dangle();
}

fn dangle() -> &String { // dangle returns a reference to a String

    let s = String::from("hello"); // s is a new String

    &s // we return a reference to the String, s
} // Here, s goes out of scope, and is dropped. Its memory goes away.
  // Danger!


因为 s 是在 dangle 内部创建的,所以当 dangle 的代码完成后, 将被解除分配。但我们试图返回对它的引用。这意味着此引用将指向无效的 String。那不好!Rust 不允许我们这样做。


这里的解决方案是直接返回 String

fn main() {
    let string = no_dangle();
}

fn no_dangle() -> String {
    let s = String::from("hello");

    s
}


这工作没有任何问题。所有权被移出,没有任何东西被解除分配。


参考规则


让我们回顾一下我们讨论过的关于引用的内容:


  • 在任何给定时间,您都可以有一个可变引用任意数量的不可变引用。

  • 引用必须始终有效。


接下来,我们将看看另一种引用:slices。