Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Rust Collections and Strings

Rust Collections and Strings

A collection is the most common form of data storage in data structures. The Rust standard library provides a rich variety of collection types to help developers handle data structure operations.

vector

A vector is a single data structure that stores multiple values, storing values of the same type linearly in memory.

A vector is a linear list, represented in Rust as Vec。

A vector is used similarly to a list. We can create a vector of a specified type in this way:

let vector: Vec = Vec::new(); // 创建类型为 i32 的空向量

let vector = vec![1, 2, 4, 8];     // 通过数组创建向量

We often use append operations with linear lists, but appending is essentially the same as the stack's push operation, so a vector only has the push method to append a single element:

Example

fn

main

(

)

{

   

let

mut

vector

=

vec

!

[

1

,

2

,

4

,

8

]

;

    vector.push

(

16

)

;

    vector.push

(

32

)

;

    vector.push

(

64

)

;

    println

!

(

"{:?}"

,

vector

)

;

}

Running result:



[1, 2, 4, 8, 16, 32, 64]

The append method is used to concatenate one vector to the end of another:

Example

fn

main

(

)

{

   

let

mut

v1

:

Vec

<

i32

=

vec

!

[

1

,

2

,

4

,

8

]

;

   

let

mut

v2

:

Vec

<

i32

=

vec

!

[

16

,

32

,

64

]

;

    v1.append

(

&

mut

v2

)

;

    println

!

(

"{:?}"

,

v1

)

;

}

Running result:



[1, 2, 4, 8, 16, 32, 64]

The get method is used to retrieve a value from a vector:

Example

fn

main

(

)

{

   

let

mut

v

=

vec

!

[

1

,

2

,

4

,

8

]

;

    println

!

(

"{}"

,

match

v.get

(

0

)

{

        Some

(

value

)

=>

value.to_string

(

)

,

        None

=>

"None"

.to_string

(

)

   

}

)

;

}

Running result:

1

Since a vector's length cannot be inferred logically, the get method cannot guarantee that a value will be retrieved, so the get method returns the Option enum, which may be empty.

This is a safe way to retrieve values, but it is a bit cumbersome to write. If you can guarantee that the index will not exceed the vector's index range, you can also use array indexing syntax:

Example

fn

main

(

)

{

   

let

v

=

vec

!

[

1

,

2

,

4

,

8

]

;

    println

!

(

"{}"

,

v

[

1

]

)

;

}

Running result:

2

But if we try to get v[4], the vector will return an error.

Iterating over a vector:

Example

fn

main

(

)

{

   

let

v

=

vec

!

[

100

,

32

,

57

]

;

   

for

i

in

&

v

{

            println

!

(

"{}"

,

i

)

;

   

}

}

Running result:

100

32

57

If you need to change a variable's value during iteration:

Example

fn

main

(

)

{

   

let

mut

v

=

vec

!

[

100

,

32

,

57

]

;

   

for

i

in

&

mut

v

{

       

i

+=

50

;

   

}

}


String

The String type has been used extensively up to this chapter, so many of its methods are already familiar to readers. This chapter mainly introduces string methods and UTF-8 properties.

Creating a new string:



let string = String::new();

Converting a basic type to a string:

let one = 1.to_string();         // 整数到字符串

let float = 1.3.to_string();     // 浮点数到字符串

let slice = "slice".to_string(); // 字符串切片到字符串

A string containing UTF-8 characters:

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("你好");

let hello = String::from("Olá");

let hello = String::from("Здравствуйте");

let hello = String::from("Hola");

String appending:

let mut s = String::from("run");

s.push_str("oob"); // 追加字符串切片

s.push('!');       // 追加字符

Concatenating strings with the + operator:

let s1 = String::from("Hello, ");

let s2 = String::from("world!");

let s3 = s1 + &s2;

This syntax can also include string slices:

let s1 = String::from("tic");

let s2 = String::from("tac");

let s3 = String::from("toe");



let s = s1 + "-" + &s2 + "-" + &s3;

Using the format! macro:

let s1 = String::from("tic");

let s2 = String::from("tac");

let s3 = String::from("toe");



let s = format!("{}-{}-{}", s1, s2, s3);

String length:

let s = "hello";

let len = s.len();

Here the value of len is 5.

let s = "Hello";

let len = s.len();

Here the value of len is 6. Because Chinese characters are UTF-8 encoded, each character is 3 bytes long, so the length is 6. But Rust supports UTF-8 character objects, so if you want to count the number of characters you can first take the string as a collection of characters:

let s = "hello你好";

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 data length.

Iterating over a string:

Example

fn

main

(

)

{

   

let

s

=

String

::

from

(

"hello Chinese"

)

;

   

for

c

in

s.chars

(

)

{

        println

!

(

"{}"

,

c

)

;

   

}

}

Running result:

h

e

l

l

o

中

文

Getting a single character from a string:

Example

fn

main

(

)

{

   

let

s

=

String

::

from

(

"EN Chinese"

)

;

   

let

a

=

s.chars

(

)

.nth

(

2

)

;

    println

!

(

"{:?}"

,

a

)

;

}

Running result:

Some('中')

Note: The nth function is a method for taking a value from an iterator. Please do not use it this way during iteration! Because the length of each UTF-8 character is not necessarily equal!

If you want to slice a substring:

Example

fn

main

(

)

{

   

let

s

=

String

::

from

(

"EN Chinese"

)

;

   

let

sub

=

&

s

[

0

..

2

]

;

    println

!

(

"{}"

,

sub

)

;

}

Running result:

EN

But please note that this usage may dismember a UTF-8 character! That will produce an error:

Example

fn

main

(

)

{

   

let

s

=

String

::

from

(

"EN Chinese"

)

;

   

let

sub

=

&

s

[

0

..

3

]

;

    println

!

(

"{}"

,

sub

)

;

}

Running result:

thread 'main' panicked at 'byte index 3 is not a char boundary; it is inside '中' (bytes 2..5) of `EN中文`', src\libcore\str\mod.rs:2069:5 

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.


Maps

A map is widely used in other languages. The most commonly used is the key-value hash map.

Creating a new hash map:

Example

use

std

::

collections

::

HashMap

;

fn

main

(

)

{

   

let

mut

map

=

HashMap

::

new

(

)

;

    map.insert

(

"color"

,

"red"

)

;

    map.insert

(

"size"

,

"10 m^2"

)

;

    println

!

(

"{}"

,

map.get

(

"color"

)

.unwrap

(

)

)

;

}

Note: The generic types of the hash map are not declared here because of Rust's automatic type inference mechanism.

Running result:

red

The insert method and get method are the two most commonly used methods of a map.

Maps support iterators:

Example

use

std

::

collections

::

HashMap

;

fn

main

(

)

{

   

let

mut

map

=

HashMap

::

new

(

)

;

    map.insert

(

"color"

,

"red"

)

;

    map.insert

(

"size"

,

"10 m^2"

)

;

   

for

p

in

map.iter

(

)

{

        println

!

(

"{:?}"

,

p

)

;

   

}

}

Running result:

("color", "red") 

("size", "10 m^2")

The iterated elements are tuples representing key-value pairs.

Rust's map is a very convenient data structure. When using the insert method to add a new key-value pair, if the same key already exists, the corresponding value will be directly overwritten. If you want to "insert safely", that is, to perform the insertion only after confirming that a key does not currently exist, you can do this:

map.entry("color").or_insert("red");

This means: if there is no key-value pair with the key "color", add it and set the value to "red"; otherwise, skip.

If you have already determined that a key exists and want to directly modify the corresponding value, there is a faster way:

Example

use

std

::

collections

::

HashMap

;

fn

main

(

)

{

   

let

mut

map

=

HashMap

::

new

(

)

;

    map.insert

(

1

,

"a"

)

;

   

   

if

let

Some

(

x

)

=

map.get_mut

(

&

1

)

{

       

x

=

"b"

;

   

}

}