【翻译】Rust中String和str有什么不同?

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

Rust中String和str有什么不同?


String is the dynamic heap string type, like Vec: use it when you need to own or modify your string data.

String 是一种动态堆字符串类型,像Vec类型一样,当你需要所有权或者修改你的字符串数据时使用它。


str is an immutable1 sequence of UTF-8 bytes of dynamic length somewhere in memory. Since the size is unknown, one can only handle it behind a pointer. This means that str most commonly2 appears as &str: a reference to some UTF-8 data, normally called a "string slice" or just a "slice". A slice is just a view onto some data, and that data can be anywhere, e.g.

str是一个不可变的UTF-8字节动态长度的序列,位于内存的某处。因为大小未知,所以只能使用一个指针来处理它。这意味着str通常以&str方式出现,一个UTF-8数据的引用,通常叫做string slice 或者 slice。一个slice是一些数据的视图,这些数据能够在任何地方。比如:


In static storage: a string literal "foo" is a &'static str. The data is hardcoded into the executable and loaded into memory when the program runs.

静态存储中,一个字符串foo是一个&'static str类型。这个数据被硬编码到可执行文件中,并且当程序运行时加载进内存。


Inside a heap allocated String: String dereferences to a &str view of the String's data.

在堆中分配的String,String解引用为一个&str 指向String数据的视图。


On the stack: e.g. the following creates a stack-allocated byte array, and then gets a view of that data as a &str:

在栈上,如下创建一个栈上分配的字节数组,然后使用&str获取这个数据的视图。


use std::str;

let x: &[u8] = &[b'a', b'b', b'c'];

let stack_str: &str = str::from_utf8(x).unwrap();


In summary, use String if you need owned string data (like passing strings to other threads, or building them at runtime), and use &str if you only need a view of a string。

综上,如果你需要获得字符串数据所有权,比如传递字符串给其他线程或者是在运行时构建它们,那么使用String,如果你仅仅需要一个字符串的视图,那么使用&str.


This is identical to the relationship between a vector Vec and a slice &[T], and is similar to the relationship between by-value T and by-reference &T for general types.

这与Vec和&[T]切片的关系是相同的,与泛型中的通过值T和通过引用&T的关系是类似的。


1 A str is fixed-length; you cannot write bytes beyond the end, or leave trailing invalid bytes. Since UTF-8 is a variable-width encoding, this effectively forces all strs to be immutable in many cases. In general, mutation requires writing more or fewer bytes than there were before (e.g. replacing an a (1 byte) with an ä (2+ bytes) would require making more room in the str). There are specific methods that can modify a &mut str in place, mostly those that handle only ASCII characters, like make_ascii_uppercase.

1 str是固定长度,你不能再最后写入字节,或者去掉一些尾部的无效字节。UTF-8是一种变宽编码,在很多情况下这有效的强制了所有str为不可变。通常,可变要求比之前写入或多或少字节(比如,用ä(两个字节)替换a(一个字节),要求在str中创建更多空间)。有一种特定的方法,在适当的位置,能够修改为 &mut str,这些大多数是只能处理assii编码字符,比如make_ascii_uppercase。


2 Dynamically sized types allow things like Rc for a sequence of reference counted UTF-8 bytes since Rust 1.2. Rust 1.21 allows easily creating these types.

2动态大小的类型允许使用Rc引用序列计数这些UTF-8字节,在Rust1.2Rust1.2.1很容易创建这些类型。

你可能感兴趣的:(【翻译】Rust中String和str有什么不同?)