Rust: array, dynamic array, string -- thoroughly combing concepts and methods

Keywords: string array Rust

Arrays and strings are essentially the same. They all correspond to a continuous piece of memory. In C language, a string is an unsigned char ending in a number zero   Array, that is, Rust's u8 array.

C language array is a pointer. As for the size of the array, it is up to the programmer to manage it. The advantage of this is that the grammatical mechanism of the language is very simple and easy to understand. But after many years of use, the shortcomings found are also fatal, which are the so-called memory leakage and wild pointer problems. According to Microsoft statistics, 70% of the defects in software development are caused by this mechanism.

In that case, Rust came into being and was determined to solve this problem. The basic idea is as follows:

one   section

The pointer of Rust contains at least two parameters: the address and size of the data. This pointer is called   Slice, that is, slice.

  • Rust's array is slicing.
  • Rust's &str is also a slice -- a slice that points to a fixed length string.

Of course, we'll be curious. What kind of bird is STR? From the perspective of syntax, the string "ABCDEFG" in Rust includes the address and length pointing to the data. STR should be the data storage area itself represented by the seven letters ABCDEFG. However, the simple data storage area itself is expressed by an exact data type in Rust, so you can only use &str this slice to access the data area indirectly.

2. Dynamic array

The problem with the slice is that its size cannot be changed, so the data elements in the slice cannot be added or deleted. Therefore, the array mechanism of real practical value in Rust is dynamic array.

Dynamic array has one more data storage Capacity than slice. If the size of the dynamic array changes within the Capacity range, the location of the data storage area does not need to be changed, just adjust the data size or len. If the data storage area is not enough, apply for a larger space, transfer the existing data, and then continue the operation.

In Rust, for general data, dynamic array is the data type   Vec; For strings, dynamic size strings are data type strings.

3. Data type conversion

Source data-> [u8]-> Vec<u8>-> &str-> String
[u8]-Vec::from(x)

std::str::from_utf8(&x).unwrap()

String::from_utf8(x.to_vec()).unwrap()

Vec<u8>&x-

std::str::from_utf8(&x).unwrap()

String::from_utf8(x).unwrap()

&strx.as_bytes()

x.as_bytes().to_vec()

-

 x.to_string()

String

x.as_bytes()

x.as_bytes().to_vec()

&x-

All sample codes are as follows:

fn main() {
    let a: [u8; 3] = [65, 66, 67];
    let b: Vec<u8> = Vec::from(a);
    let c: &str = std::str::from_utf8(&a).unwrap();
    let d: String = String::from_utf8(a.to_vec()).unwrap();
    println!();
    println!("a={:?}", a);
    println!("b={:?}", b);
    println!("c={:?}", c);
    println!("d={:?}", d);

    let b: Vec<u8> = vec![65, 66, 67];
    let a = &b;
    let c: &str = std::str::from_utf8(&b).unwrap();
    let d: String = String::from_utf8(b.clone()).unwrap();
    println!();
    println!("a={:?}", a);
    println!("b={:?}", b);
    println!("c={:?}", c);
    println!("d={:?}", d);

    let c: &str = "ABC";
    let a = c.as_bytes();
    let b: Vec<u8> = c.as_bytes().to_vec();
    let d: String = c.to_string();
    println!();
    println!("a={:?}", a);
    println!("b={:?}", b);
    println!("c={:?}", c);
    println!("d={:?}", d);

    let d: String = String::from("ABC");
    let a = d.as_bytes();
    let b: Vec<u8> = d.as_bytes().to_vec();
    let c: &str = &d;
    println!();
    println!("a={:?}", a);
    println!("b={:?}", b);
    println!("c={:?}", c);
    println!("d={:?}", d);
}

run ...

a=[65, 66, 67]
b=[65, 66, 67]
c="ABC"
d="ABC"

a=[65, 66, 67]
b=[65, 66, 67]
c="ABC"
d="ABC"

a=[65, 66, 67]
b=[65, 66, 67]
c="ABC"
d="ABC"

a=[65, 66, 67]
b=[65, 66, 67]
c="ABC"
d="ABC"

Posted by storyteller on Thu, 09 Sep 2021 21:25:38 -0700