Rust 的 `String` 和 `str` 有什么区别? [英] What are the differences between Rust's `String` and `str`?

查看:38
本文介绍了Rust 的 `String` 和 `str` 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 Rust 有 Stringstr?Stringstr 有什么区别?什么时候使用 String 而不是 str 反之亦然?其中之一是否已被弃用?

Why does Rust have String and str? What are the differences between String and str? When does one use String instead of str and vice versa? Is one of them getting deprecated?

推荐答案

String 是动态堆字符串类型,和 Vec 一样:当你需要拥有或修改您的字符串数据.

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

str 是内存中某处动态长度的 UTF-8 字节的不可变1 序列.由于大小未知,只能在指针后面处理.这意味着 str 最常见的2 显示为 &str:对某些 UTF-8 数据的引用,通常称为字符串切片";或者只是一个切片".切片 只是一些数据的视图,而这些数据可以在任何地方,例如

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.

  • 在静态存储中:字符串文字 "foo" 是一个 &'static str.数据被硬编码到可执行文件中,并在程序运行时加载到内存中.

  • 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.

在分配的堆内String:String 取消对 String 数据的 &str 视图 的引用.

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

在堆栈上:例如下面创建一个堆栈分配的字节数组,然后获得一个 视图数据作为 &str:

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

use std::str;

let x: &[u8] = &[b'a', b'b', b'c'];
let stack_str: &str = str::from_utf8(x).unwrap();

总而言之,如果您需要拥有的字符串数据(例如将字符串传递给其他线程,或在运行时构建它们),请使用 String,如果您需要使用 &str只需要一个字符串的视图.

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.

这与向量Vec和切片&[T]的关系相同,类似于by-valueT 和按引用 &T 用于一般类型.

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

1 str 是固定长度的;您不能在末尾写入字节,也不能留下尾随无效字节.由于 UTF-8 是可变宽度编码,因此在许多情况下这有效地强制所有 str 是不可变的.一般来说,mutation 需要写入比以前更多或更少的字节(例如,用 ä(2 个以上字节)替换 a(1 个字节)需要腾出更多空间在 str 中).有一些特定的方法可以修改 &mut str,主要是那些只处理 ASCII 字符的方法,比如 make_ascii_uppercase.

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.

2 动态大小的类型 允许诸如 Rc 之类的东西用于自 Rust 1.2 以来的引用计数 UTF-8 字节序列.Rust 1.21 允许轻松创建这些类型.

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

这篇关于Rust 的 `String` 和 `str` 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆