简体   繁体   English

从Rust书中引用和借用的解释

[英]Explanation on References and Borrowing from The Rust book

I'm reading the Rust book and References and Borrowing in Chapter 4 seems to be inconsistent to me. 我正在读Rust书,第4章中的References和Borrowing似乎与我不一致。 In Reference and Borrowing , figure 4-5 below shows that s points to s1 instead of the actual data on the heap. Reference和Borrowing中 ,下面的图4-5显示s指向s1而不是堆上的实际数据。

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

    let len = calculate_length(&s1);

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

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

图4-5:指向String s1的&String s图

But in String Slices , the diagram clearly shows that world references the actual data, though only part of it. 但在String Slices中 ,该图清楚地显示了world引用了实际数据,尽管只是其中的一部分。 It also says that the following code "is similar to taking a reference to the whole String but with the extra [0..5] bit. Rather than a reference to the entire String, it's a reference to a portion of the String." 它还说下面的代码“类似于引用整个String但带有额外的[0..5]位。而不是对整个String的引用,它是对String的一部分的引用。”

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

    let hello = &s[0..5];
    let world = &s[6..11];
}

And here is the diagram (figure4-6) that explains the above code: 以下是解释上述代码的图(图4-6): 字符串切片引用String的一部分

Now I guess my question is why isn't world referring to s in the String Slices case? 现在我想我的问题是为什么在String Slices案例中world没有引用s I understand that world can't just point to s because then it would not be able to get the last 5 letters. 我知道world不能仅仅指向s因为它无法获得最后5个字母。 But these two diagrams seem completely different to me, yet they are all "references". 但这两个图似乎与我完全不同,但它们都是“参考”。 Can someone please provide a logical way to understand it? 有人可以提供合理的方式来理解它吗?

Thanks! 谢谢!

Well, what does a &[mut] String guarantee in a safe context? 那么, &[mut] String在安全的上下文中保证什么?

  • That there is a String behind the pointer, which therefore supports your first diagram. 指针后面有一个String ,因此支持你的第一个图表。

Then, what does a &[mut] str guarantee in a safe context? 那么, &[mut] str在安全的环境中保证什么呢?

  • The same idea as a String , that there is a str behind the pointer. String相同的想法,指针后面有一个str But what does that mean? 但是,这是什么意思? What really is an str ? 什么是str

According to the docs page: 根据文档页面:

A &str is made up of two components: a pointer to some bytes, and a length. A&str由两个组件组成:指向某些字节的指针和一个长度。

Meaning, that the &str really is a pointer to some valid utf-8 and a length, so really, a str is the data itself, and not the String . 意思是, &str确实是指向某个有效的utf-8和一个长度的指针,所以实际上, str是数据本身,而不是 String The String can be regarded as a manipulable str . String可以被视为可操作的str So the &my_string[..] is really a pointer/reference to the data under the String . 所以&my_string[..]实际上是String下数据的指针/引用。

A reference to a string and a string slice are different things, so it is normal that they are represented differently. 对字符串和字符串切片的引用是不同的东西,因此以不同方式表示它们是正常的。

Most of the time a string slice is more useful, because you can refer to substrings, which makes it more flexible. 大多数情况下,字符串切片更有用,因为您可以引用子字符串,这使其更加灵活。 Generally a function parameter for reading some string data should be a string slice and not a reference to a string. 通常,用于读取某些字符串数据的函数参数应该是字符串切片而不是对字符串的引用。

Also, you can use an actual parameter of type &String for a string slice : 此外, 您可以为字符串切片使用类型&String的实际参数

Strings implement Deref, and so inherit all of str's methods. 字符串实现Deref,因此继承了str的所有方法。 In addition, this means that you can pass a String to a function which takes a &str by using an ampersand (&) 另外,这意味着你可以将一个String传递给一个函数,该函数使用&符号来获取&str(&)

One useful thing you can't do with a slice is increasing the size of a string, in that case you need a mutable reference to the string. 切片无法做到的一件有用的事情是增加字符串的大小,在这种情况下,您需要对字符串进行可变引用。 An example is the read_line function, which appends string data to buf : 一个例子是read_line函数,它将字符串数据附加到buf

pub fn read_line(&self, buf: &mut String) -> Result<usize>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM