Rust Collection String
Collections are the most common form of data storage in data structures. The Rust standard library provides rich collection types to help developers handle data structure operations.
### Vector
A Vector is a single data structure that stores multiple values, which stores values of the same type linearly in memory.
Vector is a linear list, represented as Vec in Rust.
The usage of Vector is similar to List. We can create a Vector of a specified type in this way:
```rust
let vector: Vec = Vec::new(); // Create an empty vector of type i32
let vector = vec![1, 2, 4, 8]; // Create a vector from an array
We often use append operations with linear lists, but append and stack's push operation are essentially the same, so Vector only has a push method to append a single element:
## Example
```rust
fn main(){
let mut vector = vec![1,2,4,8];
vector.push(16);
vector.push(32);
vector.push(64);
println!("{:?}", vector);
}
Output:
[1, 2, 4, 8, 16, 32, 64]
The append method is used to splice one vector to the end of another vector:
## Example
```rust
fn main(){
let mut v1: Vec= vec![1,2,4,8];
let mut v2: Vec= vec![16,32,64];
v1.append(&mut v2);
println!("{:?}", v1);
}
Output:
[1, 2, 4, 8, 16, 32, 64]
The get method is used to retrieve values from a vector:
## Example
```rust
fn main(){
let mut v = vec![1,2,4,8];
println!("{}",match v.get(0){
Some(value)=> value.to_string(),
None =>"None".to_string()
});
}
Output:
1
Since the length of a vector cannot be logically inferred, the get method cannot guarantee that a value will be retrieved, so the return value of the get method is an Option enum, which may be empty.
This is a safe way to retrieve values, but it can be cumbersome to write. If you can guarantee that the index for retrieving values will not exceed the vector's index range, you can also use array indexing syntax:
## Example
```rust
fn main(){
let v = vec![1,2,4,8];
println!("{}", v);
}
Output:
2
However, if we try to get v, the vector will return an error.
Traversing a vector:
## Example
```rust
fn main(){
let v = vec![100,32,57];
for i in &v {
println!("{}", i);
}
}
Output:
100
32
57
If you need to modify the value of the variable during traversal:
## Example
```rust
fn main(){
let mut v = vec![100,32,57];
for i in &mut v {
*i += 50;
}
}
* * *
## String
The String class has been used many times up to this chapter, so many methods are already familiar to readers. This chapter mainly introduces the methods of String and its UTF-8 properties.
Creating a new string:
```rust
let string = String::new();
Converting basic types to string:
```rust
let one = 1.to_string(); // Integer to string
let float = 1.3.to_string(); // Float to string
let slice = "slice".to_string(); // String slice to string
Strings containing UTF-8 characters:
```rust
let hello = String::from("ุงูุณูุงู
ุนูููู
");
let hello = String::from("Dobrรฝ den");
let hello = String::from("Hello");
let hello = String::from("ืฉึธืืืึนื");
let hello = String::from("เคจเคฎเคธเฅเคคเฅ");
let hello = String::from("ใใใซใกใฏ");
let hello = String::from("์๋
ํ์ธ์");
let hello = String::from("Hello");
let hello = String::from("Olรก");
let hello = String::from("ะะดัะฐะฒััะฒัะนัะต");
let hello = String::from("Hola");
String append:
```rust
let mut s = String::from("run");
s.push_str("oob"); // Append string slice
s.push('!'); // Append character
Using + to concatenate strings:
```rust
let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = s1 + &s2;
This syntax can also include string slices:
```rust
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
let s = s1 + "-" + &s2 + "-" + &s3;
Using the format! macro:
```rust
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
let s = format!("{}-{}-{}", s1, s2, s3);
String length:
```rust
let s = "hello";
let len = s.len();
Here the value of len is 5.
```rust
let s = "Hello";
let len = s.len();
Here the value of len is 6. Because Chinese is encoded in UTF-8, each character is 3 bytes long, so the length is 6. However, Rust supports UTF-8 character objects, so if you want to count the number of characters, you can first convert the string to a character collection:
```rust
let s = "helloHello";
let len = s.chars().count();
Here the value of len is 7, because there are 7 characters in total. Counting characters is much slower than counting the length of bytes.
Traversing a string:
## Example
```rust
fn main(){
let s = String::from("helloinText");
for c in s.chars(){
println!("{}", c);
}
}
Output:
h
e
l
l
o
in
Text
Getting a single character from a string:
## Example
```rust
fn main(){
let s = String::from("ENinText");
let a = s.chars().nth(2
YouTip