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

Rust Slice Types

Rust Slice Types

A slice is a partial reference to a data value.

The name slice often appears in biology class; when making sample slides we take a slice from a biological body for observation under a microscope. In Rust, slice means roughly the same thing, except that it takes a reference from data.

String Slices

The simplest and most commonly used data slice type is the string slice (String Slice).

Example

fn

main

(

)

{

   

let

s

=

String

::

from

(

"broadcast"

)

;

   

let

part1

=

&

s

[

0

..

5

]

;

   

let

part2

=

&

s

[

5

..

9

]

;

    println

!

(

"{}={}+{}"

,

s

,

part1

,

part2

)

;

}

Running result:

broadcast=broad+cast

The figure above explains the principle of string slices (Note: the string type in Rust essentially records the starting position of characters in memory and their length; for now we understand this much).

The syntax of using .. to represent a range appeared in the loop chapter.x..y indicates [x, y) the mathematical meaning. .. can have no operands on either side:

..y 等价于 0..y

x.. 等价于位置 x 到数据结束

.. 等价于位置 0 到结束

**Note:** Up to now, try not to use non-English characters in strings because of encoding issues. The specific reason will be explained in the "String" chapter.

A string referenced by a slice is forbidden from changing its value:

Example

fn

 main

(

)

 

{

    

let

 

mut

 s 

=

 String

::

from

(

"runoob"

)

;

    

let

 slice 

=

 

&

s

[

0

..

3

]

;

    s.push_str

(

"yes!"

)

;

// Error

    println

!

(

"slice = {}"

,

 slice

)

;

}

This program is incorrect.

s is partially referenced, so its value cannot be changed.

In fact, by now you must be wondering why every time you use a string you write it as String::from("runoob"); can't you just write "runoob" it directly?

At this point we must distinguish between these two concepts. There are two commonly used string types in Rust: str and String. str is a core Rust language type, the string slice (String Slice) that this chapter has been discussing, often appearing in the form of a reference (&str).

Any string literal enclosed in double quotes has the type nature of &str:

let s = "hello";

Here s is a variable of type &str.

The String type is a data type provided by the Rust standard library; it has more complete functionality—it supports practical operations such as appending and clearing strings. Besides both having a character start position attribute and a string length attribute, String and str also have a capacity attribute.

Both String and str support slicing, and the result of a slice is data of type &str.

Note: the slice result must be a reference type, but the developer must make this explicit:

let slice = &s[0..3];

There is a quick way to convert a String into &str:



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

let s2 = &s1[..];

Non-String Slices

Besides strings, some other linear data structures also support slicing, for example arrays:

Example

fn

main

(

)

{

   

let

arr

=

[

1

,

3

,

5

,

7

,

9

]

;

   

let

part

=

&

arr

[

0

..

3

]

;

   

for

i

in

part.iter

(

)

{

        println

!

(

"{}"

,

i

)

;

   

}

}

Running result:



1

3

5