之前写代码的时候发现自己其实完全不懂String
和str
之间的区别,现在写篇文章来细 🔒
首先查看官方参考文档中对 String 的描述如下
The
String
type is the most common string type that has ownership over the contents of the string. It has a close relationship with its borrowed counterpart, the primitivestr
.
我们可以发现 String
是拥有字符串内容的所有权的,而 str
则被描述为“借用”。查看具体实现的源代码,有如下的实现内容:
1 | pub struct String { |