Page 1 of 1

This definition and function are basically the same as string in java golang!

Posted: Tue Dec 10, 2024 4:06 am
In essence, the String type is a dynamic array of the u8 base type!

Uniquely, Rust requires utf8 encoding for the internal data of String, and some boundary checks of utf8 encoding will be performed during operations. We will explain the utf8 encoding requirements of String separately later!

&str
In addition to the dynamic String type above, Rust also defines a "static" string albania whatsapp number data 5 million type str. The "static" here mainly refers to immutability!

Why doesn't Rust use a unified string type like golang and java? My personal understanding is:

First, Rust clearly divides stack variables and heap variables in language design, which makes it easier to manage heap memory accurately without the traditional garbage collection mechanism.

Secondly, Rust clearly distinguishes between "mutable" and "immutable" in language design, which can solve the problem of data competition during the compilation stage.

So, when Rust is processing something like 'let hs = "hello world";', it doesn't really want to treat this string as a dynamically typed string.

Image

Rust provides another string type: str, which is often used as a "string literal" expression.

The str type is a basic type provided by the Rust language, and its essence is a sliceOn the other hand, Rust forbids us to use slices directly; instead, we use references to slices.